2020-08-24 19:52:52 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2022-11-06 12:11:44 +03:00
|
|
|
import {
|
2020-08-25 20:41:48 +03:00
|
|
|
CreateShortUrlAction,
|
2022-11-06 12:11:44 +03:00
|
|
|
shortUrlCreationReducerCreator,
|
2022-11-09 00:59:41 +03:00
|
|
|
createShortUrl as createShortUrlCreator,
|
2018-11-01 16:55:30 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlCreation';
|
2020-08-24 19:52:52 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
2022-05-28 11:47:39 +03:00
|
|
|
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
2020-08-24 19:52:52 +03:00
|
|
|
import { ShlinkState } from '../../../src/container/types';
|
2018-11-01 16:55:30 +03:00
|
|
|
|
|
|
|
describe('shortUrlCreationReducer', () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
const shortUrl = Mock.of<ShortUrl>();
|
|
|
|
const createShortUrlCall = jest.fn();
|
|
|
|
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ createShortUrl: createShortUrlCall });
|
2022-11-09 00:59:41 +03:00
|
|
|
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
|
|
|
|
const { reducer, resetCreateShortUrl } = shortUrlCreationReducerCreator(createShortUrl);
|
2022-11-06 12:11:44 +03:00
|
|
|
|
|
|
|
afterEach(jest.resetAllMocks);
|
2020-08-24 19:52:52 +03:00
|
|
|
|
2018-11-01 16:55:30 +03:00
|
|
|
describe('reducer', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
const action = (type: string, args: Partial<CreateShortUrlAction> = {}) => Mock.of<CreateShortUrlAction>(
|
|
|
|
{ type, ...args },
|
|
|
|
);
|
|
|
|
|
2018-11-01 16:55:30 +03:00
|
|
|
it('returns loading on CREATE_SHORT_URL_START', () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(reducer(undefined, action(createShortUrl.pending.toString()))).toEqual({
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: true,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: false,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on CREATE_SHORT_URL_ERROR', () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(reducer(undefined, action(createShortUrl.rejected.toString()))).toEqual({
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: false,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns result on CREATE_SHORT_URL', () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(reducer(undefined, action(createShortUrl.fulfilled.toString(), { payload: shortUrl }))).toEqual({
|
2020-08-24 19:52:52 +03:00
|
|
|
result: shortUrl,
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: true,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns default state on RESET_CREATE_SHORT_URL', () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(reducer(undefined, action(resetCreateShortUrl.toString()))).toEqual({
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
2022-11-07 20:24:26 +03:00
|
|
|
saved: false,
|
2018-11-01 16:55:30 +03:00
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resetCreateShortUrl', () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
it('returns proper action', () => expect(resetCreateShortUrl()).toEqual({ type: resetCreateShortUrl.toString() }));
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('createShortUrl', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const dispatch = jest.fn();
|
2020-08-24 19:52:52 +03:00
|
|
|
const getState = () => Mock.all<ShlinkState>();
|
2018-11-01 16:55:30 +03:00
|
|
|
|
|
|
|
it('calls API on success', async () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
createShortUrlCall.mockResolvedValue(shortUrl);
|
|
|
|
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
|
2018-11-01 16:55:30 +03:00
|
|
|
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
|
|
|
|
type: createShortUrl.pending.toString(),
|
|
|
|
}));
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
|
|
|
type: createShortUrl.fulfilled.toString(),
|
|
|
|
payload: shortUrl,
|
|
|
|
}));
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws on error', async () => {
|
2022-11-06 12:11:44 +03:00
|
|
|
const error = new Error('Error message');
|
|
|
|
createShortUrlCall.mockRejectedValue(error);
|
2020-04-10 19:42:08 +03:00
|
|
|
|
2022-11-06 12:11:44 +03:00
|
|
|
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
|
2018-11-01 16:55:30 +03:00
|
|
|
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
2022-11-06 12:11:44 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
|
|
|
|
type: createShortUrl.pending.toString(),
|
|
|
|
}));
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
|
|
|
type: createShortUrl.rejected.toString(),
|
|
|
|
error: expect.objectContaining({ message: 'Error message' }),
|
|
|
|
}));
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|