2020-08-26 19:55:40 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
|
|
|
import type { ProblemDetailsError } from '../../../src/api/types/errors';
|
2022-11-09 21:13:44 +03:00
|
|
|
import {
|
|
|
|
deleteShortUrl as deleteShortUrlCretor,
|
2023-02-18 13:11:01 +03:00
|
|
|
shortUrlDeletionReducerCreator,
|
2022-11-09 21:13:44 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlDeletion';
|
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 });
|
2022-11-09 21:13:44 +03:00
|
|
|
const deleteShortUrl = deleteShortUrlCretor(buildShlinkApiClient);
|
|
|
|
const { reducer, resetDeleteShortUrl } = shortUrlDeletionReducerCreator(deleteShortUrl);
|
2022-11-06 15:06:55 +03:00
|
|
|
|
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
|
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', () => {
|
2022-11-15 13:50:04 +03:00
|
|
|
const errorData = Mock.of<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', () => {
|
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);
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|