2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-04 12:16:01 +03:00
|
|
|
import type { ShlinkApiClient } from '../../../src/api-contract';
|
2023-08-06 22:27:57 +03:00
|
|
|
import type { ShlinkShortUrl } 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,
|
2023-08-02 10:01:44 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlCreation';
|
2018-11-01 16:55:30 +03:00
|
|
|
|
|
|
|
describe('shortUrlCreationReducer', () => {
|
2023-08-06 22:27:57 +03:00
|
|
|
const shortUrl = fromPartial<ShlinkShortUrl>({});
|
2023-05-27 12:57:26 +03:00
|
|
|
const createShortUrlCall = vi.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
|
|
|
|
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', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const dispatch = vi.fn();
|
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);
|
2023-08-04 12:16:01 +03:00
|
|
|
await createShortUrl({ longUrl: 'foo' })(dispatch, vi.fn(), {});
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|