2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-06 18:19:59 +03:00
|
|
|
import type { ProblemDetailsError, ShlinkApiClient } from '../../../src/api-contract';
|
2022-11-09 21:13:44 +03:00
|
|
|
import {
|
2023-04-13 23:47:13 +03:00
|
|
|
deleteShortUrl as deleteShortUrlCreator,
|
2023-02-18 13:11:01 +03:00
|
|
|
shortUrlDeletionReducerCreator,
|
2023-08-02 10:01:44 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlDeletion';
|
2018-12-21 12:02:42 +03:00
|
|
|
|
|
|
|
describe('shortUrlDeletionReducer', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const deleteShortUrlCall = vi.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ deleteShortUrl: deleteShortUrlCall });
|
|
|
|
const deleteShortUrl = deleteShortUrlCreator(buildShlinkApiClient);
|
2022-11-09 21:13:44 +03:00
|
|
|
const { reducer, resetDeleteShortUrl } = shortUrlDeletionReducerCreator(deleteShortUrl);
|
2022-11-06 15:06:55 +03:00
|
|
|
|
2018-12-21 12:02:42 +03:00
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on DELETE_SHORT_URL_START', () =>
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, deleteShortUrl.pending('', { shortCode: '' }))).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: '',
|
|
|
|
loading: true,
|
|
|
|
error: false,
|
2022-11-06 15:06:55 +03:00
|
|
|
deleted: false,
|
2018-12-21 12:02:42 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns default on RESET_DELETE_SHORT_URL', () =>
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, resetDeleteShortUrl())).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: '',
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
2022-11-06 15:06:55 +03:00
|
|
|
deleted: false,
|
2018-12-21 12:02:42 +03:00
|
|
|
}));
|
|
|
|
|
2020-01-31 22:12:22 +03:00
|
|
|
it('returns shortCode on SHORT_URL_DELETED', () =>
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, deleteShortUrl.fulfilled({ shortCode: 'foo' }, '', { shortCode: 'foo' }))).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: 'foo',
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
2022-11-06 15:06:55 +03:00
|
|
|
deleted: true,
|
2018-12-21 12:02:42 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns errorData on DELETE_SHORT_URL_ERROR', () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const errorData = fromPartial<ProblemDetailsError>(
|
|
|
|
{ type: 'bar', detail: 'detail', title: 'title', status: 400 },
|
|
|
|
);
|
2023-03-18 14:35:33 +03:00
|
|
|
const error = errorData as unknown as Error;
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, deleteShortUrl.rejected(error, '', { shortCode: '' }))).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: '',
|
|
|
|
loading: false,
|
|
|
|
error: true,
|
2022-11-06 15:06:55 +03:00
|
|
|
deleted: false,
|
2018-12-21 12:02:42 +03:00
|
|
|
errorData,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deleteShortUrl', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const dispatch = vi.fn();
|
|
|
|
const getState = vi.fn().mockReturnValue({ selectedServer: {} });
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each(
|
2022-03-26 14:17:42 +03:00
|
|
|
[[undefined], [null], ['example.com']],
|
2020-02-17 20:21:52 +03:00
|
|
|
)('dispatches proper actions if API client request succeeds', async (domain) => {
|
2018-12-21 12:02:42 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
|
2022-11-06 15:06:55 +03:00
|
|
|
await deleteShortUrl({ shortCode, domain })(dispatch, getState, {});
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
2022-11-06 14:46:29 +03:00
|
|
|
payload: { shortCode, domain },
|
2022-11-06 15:06:55 +03:00
|
|
|
}));
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2022-11-06 15:06:55 +03:00
|
|
|
expect(deleteShortUrlCall).toHaveBeenCalledTimes(1);
|
|
|
|
expect(deleteShortUrlCall).toHaveBeenCalledWith(shortCode, domain);
|
2018-12-21 12:02:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|