2020-08-26 19:55:40 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2018-12-21 12:02:42 +03:00
|
|
|
import reducer, {
|
2020-01-31 22:12:22 +03:00
|
|
|
DELETE_SHORT_URL_ERROR,
|
2018-12-21 12:02:42 +03:00
|
|
|
DELETE_SHORT_URL_START,
|
|
|
|
RESET_DELETE_SHORT_URL,
|
|
|
|
SHORT_URL_DELETED,
|
|
|
|
resetDeleteShortUrl,
|
|
|
|
deleteShortUrl,
|
|
|
|
} from '../../../src/short-urls/reducers/shortUrlDeletion';
|
2022-05-28 11:47:39 +03:00
|
|
|
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
2022-10-12 11:35:16 +03:00
|
|
|
import { ProblemDetailsError } from '../../../src/api/types/errors';
|
2018-12-21 12:02:42 +03:00
|
|
|
|
|
|
|
describe('shortUrlDeletionReducer', () => {
|
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on DELETE_SHORT_URL_START', () =>
|
2020-09-02 21:27:50 +03:00
|
|
|
expect(reducer(undefined, { type: DELETE_SHORT_URL_START } as any)).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: '',
|
|
|
|
loading: true,
|
|
|
|
error: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns default on RESET_DELETE_SHORT_URL', () =>
|
2020-09-02 21:27:50 +03:00
|
|
|
expect(reducer(undefined, { type: RESET_DELETE_SHORT_URL } as any)).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: '',
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
}));
|
|
|
|
|
2020-01-31 22:12:22 +03:00
|
|
|
it('returns shortCode on SHORT_URL_DELETED', () =>
|
2020-09-02 21:27:50 +03:00
|
|
|
expect(reducer(undefined, { type: SHORT_URL_DELETED, shortCode: 'foo' } as any)).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: 'foo',
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns errorData on DELETE_SHORT_URL_ERROR', () => {
|
2020-08-26 19:55:40 +03:00
|
|
|
const errorData = Mock.of<ProblemDetailsError>({ type: 'bar' });
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2020-09-02 21:27:50 +03:00
|
|
|
expect(reducer(undefined, { type: DELETE_SHORT_URL_ERROR, errorData } as any)).toEqual({
|
2018-12-21 12:02:42 +03:00
|
|
|
shortCode: '',
|
|
|
|
loading: false,
|
|
|
|
error: true,
|
|
|
|
errorData,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resetDeleteShortUrl', () => {
|
|
|
|
it('returns expected action', () =>
|
|
|
|
expect(resetDeleteShortUrl()).toEqual({ type: RESET_DELETE_SHORT_URL }));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deleteShortUrl', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const dispatch = jest.fn();
|
|
|
|
const getState = jest.fn().mockReturnValue({ selectedServer: {} });
|
2018-12-21 12:02:42 +03:00
|
|
|
|
|
|
|
afterEach(() => {
|
2019-04-19 13:41:59 +03:00
|
|
|
dispatch.mockReset();
|
|
|
|
getState.mockClear();
|
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) => {
|
2020-09-02 21:27:50 +03:00
|
|
|
const apiClientMock = Mock.of<ShlinkApiClient>({
|
2019-04-19 13:41:59 +03:00
|
|
|
deleteShortUrl: jest.fn(() => ''),
|
2020-09-02 21:27:50 +03:00
|
|
|
});
|
2018-12-21 12:02:42 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
|
2020-02-08 11:57:18 +03:00
|
|
|
await deleteShortUrl(() => apiClientMock)(shortCode, domain)(dispatch, getState);
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
|
2020-02-08 12:46:11 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_DELETED, shortCode, domain });
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
|
2020-02-08 11:57:18 +03:00
|
|
|
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledWith(shortCode, domain);
|
2018-12-21 12:02:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches proper actions if API client request fails', async () => {
|
|
|
|
const data = { foo: 'bar' };
|
|
|
|
const error = { response: { data } };
|
2020-09-02 21:27:50 +03:00
|
|
|
const apiClientMock = Mock.of<ShlinkApiClient>({
|
|
|
|
deleteShortUrl: jest.fn(async () => Promise.reject(error)),
|
|
|
|
});
|
2018-12-21 12:02:42 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
|
|
|
|
try {
|
|
|
|
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
|
|
|
|
} catch (e) {
|
|
|
|
expect(e).toEqual(error);
|
|
|
|
}
|
|
|
|
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_SHORT_URL_ERROR, errorData: data });
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
|
2020-02-08 11:57:18 +03:00
|
|
|
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledWith(shortCode, undefined);
|
2018-12-21 12:02:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|