shlink-web-client/test/short-urls/reducers/shortUrlCreation.test.ts

66 lines
2.3 KiB
TypeScript
Raw Normal View History

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';
import {
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>({});
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 });
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
const { reducer, resetCreateShortUrl } = shortUrlCreationReducerCreator(createShortUrl);
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,
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,
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({
result: shortUrl,
2018-11-01 16:55:30 +03:00
saving: false,
saved: true,
2018-11-01 16:55:30 +03:00
error: false,
});
});
it('returns default state on RESET_CREATE_SHORT_URL', () => {
expect(reducer(undefined, resetCreateShortUrl())).toEqual({
2018-11-01 16:55:30 +03:00
saving: false,
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();
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 () => {
createShortUrlCall.mockResolvedValue(shortUrl);
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
2018-11-01 16:55:30 +03:00
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
2018-11-01 16:55:30 +03:00
});
});
});