2020-08-26 19:55:40 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2020-03-30 22:26:30 +03:00
|
|
|
import reducer, {
|
|
|
|
EDIT_SHORT_URL_START,
|
|
|
|
EDIT_SHORT_URL_ERROR,
|
|
|
|
SHORT_URL_EDITED,
|
|
|
|
editShortUrl,
|
2020-08-26 19:55:40 +03:00
|
|
|
ShortUrlEditedAction,
|
2020-03-30 22:26:30 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlEdition';
|
2020-08-26 19:55:40 +03:00
|
|
|
import { ShlinkState } from '../../../src/container/types';
|
2021-03-27 15:56:44 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
2022-05-01 11:30:51 +03:00
|
|
|
import { SelectedServer } from '../../../src/servers/data';
|
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 });
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on EDIT_SHORT_URL_START', () => {
|
2020-08-26 19:55:40 +03:00
|
|
|
expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: EDIT_SHORT_URL_START }))).toEqual({
|
2020-03-30 22:26:30 +03:00
|
|
|
saving: true,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on EDIT_SHORT_URL_ERROR', () => {
|
2020-08-26 19:55:40 +03:00
|
|
|
expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: EDIT_SHORT_URL_ERROR }))).toEqual({
|
2020-03-30 22:26:30 +03:00
|
|
|
saving: false,
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
|
2021-03-27 15:56:44 +03:00
|
|
|
expect(reducer(undefined, { type: SHORT_URL_EDITED, shortUrl })).toEqual({
|
|
|
|
shortUrl,
|
2020-03-30 22:26:30 +03:00
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('editShortUrl', () => {
|
2021-03-27 15:56:44 +03:00
|
|
|
const updateShortUrl = jest.fn().mockResolvedValue(shortUrl);
|
2022-05-01 11:30:51 +03:00
|
|
|
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrl });
|
2020-03-30 22:26:30 +03:00
|
|
|
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) => {
|
2021-03-27 15:56:44 +03:00
|
|
|
await editShortUrl(buildShlinkApiClient)(shortCode, domain, { 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);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
2021-03-27 15:56:44 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_EDITED, shortUrl });
|
2020-03-30 22:26:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches error on failure', async () => {
|
|
|
|
const error = new Error();
|
|
|
|
|
2021-03-27 11:49:47 +03:00
|
|
|
updateShortUrl.mockRejectedValue(error);
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
try {
|
2021-03-27 15:56:44 +03:00
|
|
|
await editShortUrl(buildShlinkApiClient)(shortCode, undefined, { longUrl })(dispatch, createGetState());
|
2020-03-30 22:26:30 +03:00
|
|
|
} catch (e) {
|
|
|
|
expect(e).toBe(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
2021-03-27 11:49:47 +03:00
|
|
|
expect(updateShortUrl).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, undefined, { longUrl });
|
2020-03-30 22:26:30 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_SHORT_URL_ERROR });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|