2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
|
|
|
import type { ShlinkState } from '../../../src/container/types';
|
2023-04-13 23:47:13 +03:00
|
|
|
import type { ShortUrl } from '../../../src/short-urls/data';
|
2022-11-06 12:11:44 +03:00
|
|
|
import {
|
2022-11-09 00:59:41 +03:00
|
|
|
createShortUrl as createShortUrlCreator,
|
2023-02-18 13:11:01 +03:00
|
|
|
shortUrlCreationReducerCreator,
|
2018-11-01 16:55:30 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlCreation';
|
|
|
|
|
|
|
|
describe('shortUrlCreationReducer', () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const shortUrl = fromPartial<ShortUrl>({});
|
2022-11-06 12:11:44 +03:00
|
|
|
const createShortUrlCall = jest.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ createShortUrl: createShortUrlCall });
|
2022-11-09 00:59:41 +03:00
|
|
|
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
|
|
|
|
const { reducer, resetCreateShortUrl } = shortUrlCreationReducerCreator(createShortUrl);
|
2022-11-06 12:11:44 +03:00
|
|
|
|
|
|
|
afterEach(jest.resetAllMocks);
|
2020-08-24 19:52:52 +03:00
|
|
|
|
2018-11-01 16:55:30 +03:00
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on CREATE_SHORT_URL_START', () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
expect(reducer(undefined, createShortUrl.pending('', fromPartial({})))).toEqual({
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: true,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: false,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on CREATE_SHORT_URL_ERROR', () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
expect(reducer(undefined, createShortUrl.rejected(null, '', fromPartial({})))).toEqual({
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: false,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns result on CREATE_SHORT_URL', () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
expect(reducer(undefined, createShortUrl.fulfilled(shortUrl, '', fromPartial({})))).toEqual({
|
2020-08-24 19:52:52 +03:00
|
|
|
result: shortUrl,
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: true,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns default state on RESET_CREATE_SHORT_URL', () => {
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, resetCreateShortUrl())).toEqual({
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: false,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('createShortUrl', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const dispatch = jest.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const getState = () => fromPartial<ShlinkState>({});
|
2018-11-01 16:55:30 +03:00
|
|
|
|
|
|
|
it('calls API on success', async () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
createShortUrlCall.mockResolvedValue(shortUrl);
|
|
|
|
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
|
2018-11-01 16:55:30 +03:00
|
|
|
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
|
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({ payload: shortUrl }));
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|