2020-08-26 19:55:40 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-11-06 15:06:55 +03:00
|
|
|
import { shortUrlDeletionReducerCreator } 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', () => {
|
2022-11-06 15:06:55 +03:00
|
|
|
const deleteShortUrlCall = jest.fn();
|
|
|
|
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ deleteShortUrl: deleteShortUrlCall });
|
|
|
|
const { reducer, resetDeleteShortUrl, deleteShortUrl } = shortUrlDeletionReducerCreator(buildShlinkApiClient);
|
|
|
|
|
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
|
2018-12-21 12:02:42 +03:00
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on DELETE_SHORT_URL_START', () =>
|
2022-11-06 15:21:46 +03:00
|
|
|
expect(reducer(undefined, { type: deleteShortUrl.pending.toString() })).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', () =>
|
2022-11-06 15:21:46 +03:00
|
|
|
expect(reducer(undefined, { type: resetDeleteShortUrl.toString() })).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', () =>
|
2022-11-06 14:46:29 +03:00
|
|
|
expect(reducer(undefined, {
|
2022-11-06 15:06:55 +03:00
|
|
|
type: deleteShortUrl.fulfilled.toString(),
|
2022-11-06 14:46:29 +03:00
|
|
|
payload: { shortCode: 'foo' },
|
2022-11-06 15:21:46 +03:00
|
|
|
})).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', () => {
|
2020-08-26 19:55:40 +03:00
|
|
|
const errorData = Mock.of<ProblemDetailsError>({ type: 'bar' });
|
2022-11-06 15:06:55 +03:00
|
|
|
const error = { response: { data: errorData } };
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2022-11-06 15:21:46 +03:00
|
|
|
expect(reducer(undefined, { type: deleteShortUrl.rejected.toString(), error })).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('resetDeleteShortUrl', () => {
|
|
|
|
it('returns expected action', () =>
|
2022-11-06 15:06:55 +03:00
|
|
|
expect(resetDeleteShortUrl()).toEqual({ type: resetDeleteShortUrl.toString() }));
|
2018-12-21 12:02:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
2022-11-06 15:06:55 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: deleteShortUrl.pending.toString() }));
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
|
|
|
type: deleteShortUrl.fulfilled.toString(),
|
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
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches proper actions if API client request fails', async () => {
|
|
|
|
const data = { foo: 'bar' };
|
|
|
|
const shortCode = 'abc123';
|
|
|
|
|
2022-11-06 15:06:55 +03:00
|
|
|
deleteShortUrlCall.mockRejectedValue({ response: { data } });
|
|
|
|
|
|
|
|
await deleteShortUrl({ shortCode })(dispatch, getState, {});
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
2022-11-06 15:06:55 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: deleteShortUrl.pending.toString() }));
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
|
|
|
type: deleteShortUrl.rejected.toString(),
|
|
|
|
}));
|
2018-12-21 12:02:42 +03:00
|
|
|
|
2022-11-06 15:06:55 +03:00
|
|
|
expect(deleteShortUrlCall).toHaveBeenCalledTimes(1);
|
|
|
|
expect(deleteShortUrlCall).toHaveBeenCalledWith(shortCode, undefined);
|
2018-12-21 12:02:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|