2020-08-26 19:55:40 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
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';
|
2023-03-18 14:35:33 +03:00
|
|
|
import type { EditShortUrl } from '../../../src/short-urls/reducers/shortUrlEdition';
|
2022-11-09 20:40:51 +03:00
|
|
|
import {
|
|
|
|
editShortUrl as editShortUrlCreator,
|
2023-02-18 13:11:01 +03:00
|
|
|
shortUrlEditionReducerCreator,
|
2022-11-09 20:40:51 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlEdition';
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
describe('shortUrlEditionReducer', () => {
|
|
|
|
const longUrl = 'https://shlink.io';
|
|
|
|
const shortCode = 'abc123';
|
2021-03-27 15:56:44 +03:00
|
|
|
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 });
|
2022-11-09 20:40:51 +03:00
|
|
|
const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
|
2022-11-09 21:13:44 +03:00
|
|
|
const { reducer } = shortUrlEditionReducerCreator(editShortUrl);
|
2022-11-06 14:32:55 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on EDIT_SHORT_URL_START', () => {
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, editShortUrl.pending('', Mock.all<EditShortUrl>()))).toEqual({
|
2020-03-30 22:26:30 +03:00
|
|
|
saving: true,
|
2022-11-06 14:32:55 +03:00
|
|
|
saved: false,
|
2020-03-30 22:26:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on EDIT_SHORT_URL_ERROR', () => {
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, editShortUrl.rejected(null, '', Mock.all<EditShortUrl>()))).toEqual({
|
2020-03-30 22:26:30 +03:00
|
|
|
saving: false,
|
2022-11-06 14:32:55 +03:00
|
|
|
saved: false,
|
2020-03-30 22:26:30 +03:00
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, editShortUrl.fulfilled(shortUrl, '', Mock.all<EditShortUrl>()))).toEqual({
|
2021-03-27 15:56:44 +03:00
|
|
|
shortUrl,
|
2020-03-30 22:26:30 +03:00
|
|
|
saving: false,
|
2022-11-06 14:32:55 +03:00
|
|
|
saved: true,
|
2020-03-30 22:26:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('editShortUrl', () => {
|
|
|
|
const dispatch = jest.fn();
|
2021-03-27 15:56:44 +03:00
|
|
|
const createGetState = (selectedServer: SelectedServer = null) => () => Mock.of<ShlinkState>({ selectedServer });
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
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(), {});
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
2021-03-27 11:49:47 +03:00
|
|
|
expect(updateShortUrl).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, { longUrl });
|
2020-03-30 22:26:30 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
|
2020-03-30 22:26:30 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|