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

87 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-08-26 19:55:40 +03:00
import { Mock } from 'ts-mockery';
import {
ShortUrlEditedAction,
shortUrlEditionReducerCreator,
editShortUrl as editShortUrlCreator,
} from '../../../src/short-urls/reducers/shortUrlEdition';
2020-08-26 19:55:40 +03:00
import { ShlinkState } from '../../../src/container/types';
import { ShortUrl } from '../../../src/short-urls/data';
import { SelectedServer } from '../../../src/servers/data';
describe('shortUrlEditionReducer', () => {
const longUrl = 'https://shlink.io';
const shortCode = 'abc123';
const shortUrl = Mock.of<ShortUrl>({ longUrl, shortCode });
2022-11-06 14:32:55 +03:00
const updateShortUrl = jest.fn().mockResolvedValue(shortUrl);
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrl });
const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
const reducer = shortUrlEditionReducerCreator(editShortUrl);
2022-11-06 14:32:55 +03:00
afterEach(jest.clearAllMocks);
describe('reducer', () => {
it('returns loading on EDIT_SHORT_URL_START', () => {
2022-11-06 14:32:55 +03:00
expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: editShortUrl.pending.toString() }))).toEqual({
saving: true,
2022-11-06 14:32:55 +03:00
saved: false,
error: false,
});
});
it('returns error on EDIT_SHORT_URL_ERROR', () => {
2022-11-06 14:32:55 +03:00
expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: editShortUrl.rejected.toString() }))).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', () => {
2022-11-06 14:32:55 +03:00
expect(reducer(undefined, { type: editShortUrl.fulfilled.toString(), payload: shortUrl })).toEqual({
shortUrl,
saving: false,
2022-11-06 14:32:55 +03:00
saved: true,
error: false,
});
});
});
describe('editShortUrl', () => {
const dispatch = jest.fn();
const createGetState = (selectedServer: SelectedServer = null) => () => Mock.of<ShlinkState>({ selectedServer });
afterEach(jest.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);
2022-11-06 14:32:55 +03:00
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: editShortUrl.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: editShortUrl.fulfilled.toString(),
payload: shortUrl,
}));
});
it('dispatches error on failure', async () => {
const error = new Error();
updateShortUrl.mockRejectedValue(error);
2022-11-06 14:32:55 +03:00
await editShortUrl({ shortCode, data: { longUrl } })(dispatch, createGetState(), {});
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, undefined, { longUrl });
expect(dispatch).toHaveBeenCalledTimes(2);
2022-11-06 14:32:55 +03:00
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: editShortUrl.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ type: editShortUrl.rejected.toString() }));
});
});
});