2020-01-19 22:21:59 +03:00
|
|
|
import moment from 'moment';
|
2020-08-24 19:52:52 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2020-01-19 22:21:59 +03:00
|
|
|
import reducer, {
|
|
|
|
EDIT_SHORT_URL_META_START,
|
|
|
|
EDIT_SHORT_URL_META_ERROR,
|
|
|
|
SHORT_URL_META_EDITED,
|
2020-01-19 22:37:12 +03:00
|
|
|
RESET_EDIT_SHORT_URL_META,
|
2020-01-19 22:21:59 +03:00
|
|
|
editShortUrlMeta,
|
2020-01-19 22:37:12 +03:00
|
|
|
resetShortUrlMeta,
|
2020-01-19 22:21:59 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlMeta';
|
2020-08-24 19:52:52 +03:00
|
|
|
import { ShlinkState } from '../../../src/container/types';
|
2020-01-19 22:21:59 +03:00
|
|
|
|
|
|
|
describe('shortUrlMetaReducer', () => {
|
|
|
|
const meta = {
|
|
|
|
maxVisits: 50,
|
|
|
|
startDate: moment('2020-01-01').format(),
|
|
|
|
};
|
|
|
|
const shortCode = 'abc123';
|
|
|
|
|
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on EDIT_SHORT_URL_META_START', () => {
|
2020-08-24 19:52:52 +03:00
|
|
|
expect(reducer(undefined, { type: EDIT_SHORT_URL_META_START } as any)).toEqual({
|
|
|
|
meta: {},
|
|
|
|
shortCode: null,
|
2020-01-19 22:21:59 +03:00
|
|
|
saving: true,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on EDIT_SHORT_URL_META_ERROR', () => {
|
2020-08-24 19:52:52 +03:00
|
|
|
expect(reducer(undefined, { type: EDIT_SHORT_URL_META_ERROR } as any)).toEqual({
|
|
|
|
meta: {},
|
|
|
|
shortCode: null,
|
2020-01-19 22:21:59 +03:00
|
|
|
saving: false,
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns provided tags and shortCode on SHORT_URL_META_EDITED', () => {
|
2020-11-14 01:06:03 +03:00
|
|
|
expect(reducer(undefined, { type: SHORT_URL_META_EDITED, meta, shortCode } as any)).toEqual({
|
2020-01-19 22:21:59 +03:00
|
|
|
meta,
|
|
|
|
shortCode,
|
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
2020-01-19 22:37:12 +03:00
|
|
|
|
|
|
|
it('goes back to initial state on RESET_EDIT_SHORT_URL_META', () => {
|
2020-08-24 19:52:52 +03:00
|
|
|
expect(reducer(undefined, { type: RESET_EDIT_SHORT_URL_META } as any)).toEqual({
|
2020-01-19 22:37:12 +03:00
|
|
|
meta: {},
|
|
|
|
shortCode: null,
|
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
2020-01-19 22:21:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('editShortUrlMeta', () => {
|
2021-03-27 11:49:47 +03:00
|
|
|
const updateShortUrl = jest.fn().mockResolvedValue({});
|
|
|
|
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrl });
|
2020-01-19 22:21:59 +03:00
|
|
|
const dispatch = jest.fn();
|
2020-08-24 19:52:52 +03:00
|
|
|
const getState = () => Mock.all<ShlinkState>();
|
2020-01-19 22:21:59 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
|
|
|
|
2020-02-17 20:21:52 +03:00
|
|
|
it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches metadata on success', async (domain) => {
|
2020-08-24 19:52:52 +03:00
|
|
|
await editShortUrlMeta(buildShlinkApiClient)(shortCode, domain, meta)(dispatch, getState);
|
2020-01-19 22:21:59 +03:00
|
|
|
|
|
|
|
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
2021-03-27 11:49:47 +03:00
|
|
|
expect(updateShortUrl).toHaveBeenCalledTimes(1);
|
|
|
|
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, meta);
|
2020-01-19 22:21:59 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_META_START });
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_META_EDITED, meta, shortCode, domain });
|
2020-01-19 22:21:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches error on failure', async () => {
|
|
|
|
const error = new Error();
|
|
|
|
|
2021-03-27 11:49:47 +03:00
|
|
|
updateShortUrl.mockRejectedValue(error);
|
2020-01-19 22:21:59 +03:00
|
|
|
|
|
|
|
try {
|
2020-08-24 19:52:52 +03:00
|
|
|
await editShortUrlMeta(buildShlinkApiClient)(shortCode, undefined, meta)(dispatch, getState);
|
2020-01-19 22:21:59 +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, meta);
|
2020-01-19 22:21:59 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_META_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_SHORT_URL_META_ERROR });
|
|
|
|
});
|
|
|
|
});
|
2020-01-19 22:37:12 +03:00
|
|
|
|
|
|
|
describe('resetShortUrlMeta', () => {
|
|
|
|
it('creates expected action', () => expect(resetShortUrlMeta()).toEqual({ type: RESET_EDIT_SHORT_URL_META }));
|
|
|
|
});
|
2020-01-19 22:21:59 +03:00
|
|
|
});
|