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';
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
describe('shortUrlEditionReducer', () => {
|
|
|
|
const longUrl = 'https://shlink.io';
|
|
|
|
const shortCode = 'abc123';
|
|
|
|
|
|
|
|
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({
|
|
|
|
longUrl: null,
|
|
|
|
shortCode: null,
|
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({
|
|
|
|
longUrl: null,
|
|
|
|
shortCode: null,
|
2020-03-30 22:26:30 +03:00
|
|
|
saving: false,
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
|
2020-08-26 19:55:40 +03:00
|
|
|
expect(reducer(undefined, { type: SHORT_URL_EDITED, longUrl, shortCode, domain: null })).toEqual({
|
2020-03-30 22:26:30 +03:00
|
|
|
longUrl,
|
|
|
|
shortCode,
|
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('editShortUrl', () => {
|
|
|
|
const updateShortUrlMeta = jest.fn().mockResolvedValue({});
|
|
|
|
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlMeta });
|
|
|
|
const dispatch = jest.fn();
|
2020-08-26 19:55:40 +03:00
|
|
|
const getState = () => Mock.of<ShlinkState>();
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
|
|
|
it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches long URL on success', async (domain) => {
|
2020-08-26 19:55:40 +03:00
|
|
|
await editShortUrl(buildShlinkApiClient)(shortCode, domain, longUrl)(dispatch, getState);
|
2020-03-30 22:26:30 +03:00
|
|
|
|
|
|
|
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateShortUrlMeta).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateShortUrlMeta).toHaveBeenCalledWith(shortCode, domain, { longUrl });
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_EDITED, longUrl, shortCode, domain });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches error on failure', async () => {
|
|
|
|
const error = new Error();
|
|
|
|
|
|
|
|
updateShortUrlMeta.mockRejectedValue(error);
|
|
|
|
|
|
|
|
try {
|
2020-08-26 19:55:40 +03:00
|
|
|
await editShortUrl(buildShlinkApiClient)(shortCode, undefined, longUrl)(dispatch, getState);
|
2020-03-30 22:26:30 +03:00
|
|
|
} catch (e) {
|
|
|
|
expect(e).toBe(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateShortUrlMeta).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateShortUrlMeta).toHaveBeenCalledWith(shortCode, undefined, { longUrl });
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_SHORT_URL_ERROR });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|