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

67 lines
2.4 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 { ShlinkState } from '../../../src/container/types';
import type { SelectedServer } from '../../../src/servers/data';
import type { ShortUrl } from '../../../src/short-urls/data';
import {
editShortUrl as editShortUrlCreator,
2023-02-18 13:11:01 +03:00
shortUrlEditionReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlEdition';
describe('shortUrlEditionReducer', () => {
const longUrl = 'https://shlink.io';
const shortCode = 'abc123';
2023-04-13 23:47:13 +03:00
const shortUrl = fromPartial<ShortUrl>({ longUrl, shortCode });
2023-05-27 12:57:26 +03:00
const updateShortUrl = vi.fn().mockResolvedValue(shortUrl);
const buildShlinkApiClient = vi.fn().mockReturnValue({ updateShortUrl });
const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
2022-11-09 21:13:44 +03:00
const { reducer } = shortUrlEditionReducerCreator(editShortUrl);
2022-11-06 14:32:55 +03:00
2023-05-27 12:57:26 +03:00
afterEach(vi.clearAllMocks);
describe('reducer', () => {
it('returns loading on EDIT_SHORT_URL_START', () => {
2023-04-13 23:47:13 +03:00
expect(reducer(undefined, editShortUrl.pending('', fromPartial({})))).toEqual({
saving: true,
2022-11-06 14:32:55 +03:00
saved: false,
error: false,
});
});
it('returns error on EDIT_SHORT_URL_ERROR', () => {
2023-04-13 23:47:13 +03:00
expect(reducer(undefined, editShortUrl.rejected(null, '', fromPartial({})))).toEqual({
saving: false,
2022-11-06 14:32:55 +03:00
saved: false,
error: true,
});
});
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
2023-04-13 23:47:13 +03:00
expect(reducer(undefined, editShortUrl.fulfilled(shortUrl, '', fromPartial({})))).toEqual({
shortUrl,
saving: false,
2022-11-06 14:32:55 +03:00
saved: true,
error: false,
});
});
});
describe('editShortUrl', () => {
2023-05-27 12:57:26 +03:00
const dispatch = vi.fn();
2023-04-13 23:47:13 +03:00
const createGetState = (selectedServer: SelectedServer = null) => () => fromPartial<ShlinkState>({
selectedServer,
});
2023-05-27 12:57:26 +03:00
afterEach(vi.clearAllMocks);
2022-03-26 14:17:42 +03:00
it.each([[undefined], [null], ['example.com']])('dispatches short URL on success', async (domain) => {
2022-11-06 14:32:55 +03:00
await editShortUrl({ shortCode, domain, data: { longUrl } })(dispatch, createGetState(), {});
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, { longUrl });
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
});
});
});