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

64 lines
2.1 KiB
TypeScript
Raw Normal View History

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';
import type { ShlinkShortUrl } from '../../../src/short-urls/data';
import {
createShortUrl as createShortUrlCreator,
2023-02-18 13:11:01 +03:00
shortUrlCreationReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlCreation';
2018-11-01 16:55:30 +03:00
describe('shortUrlCreationReducer', () => {
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 });
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();
2018-11-01 16:55:30 +03:00
it('calls API on success', async () => {
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
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
2018-11-01 16:55:30 +03:00
});
});
});