shlink-web-client/test/short-urls/reducers/shortUrlCreation.test.ts

100 lines
3.5 KiB
TypeScript
Raw Normal View History

import { Mock } from 'ts-mockery';
import {
CreateShortUrlAction,
shortUrlCreationReducerCreator,
createShortUrl as createShortUrlCreator,
2018-11-01 16:55:30 +03:00
} from '../../../src/short-urls/reducers/shortUrlCreation';
import { ShortUrl } from '../../../src/short-urls/data';
2022-05-28 11:47:39 +03:00
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import { ShlinkState } from '../../../src/container/types';
2018-11-01 16:55:30 +03:00
describe('shortUrlCreationReducer', () => {
const shortUrl = Mock.of<ShortUrl>();
const createShortUrlCall = jest.fn();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ createShortUrl: createShortUrlCall });
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
const { reducer, resetCreateShortUrl } = shortUrlCreationReducerCreator(createShortUrl);
afterEach(jest.resetAllMocks);
2018-11-01 16:55:30 +03:00
describe('reducer', () => {
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', () => {
expect(reducer(undefined, action(createShortUrl.pending.toString()))).toEqual({
2018-11-01 16:55:30 +03:00
saving: true,
saved: false,
2018-11-01 16:55:30 +03:00
error: false,
});
});
it('returns error on CREATE_SHORT_URL_ERROR', () => {
expect(reducer(undefined, action(createShortUrl.rejected.toString()))).toEqual({
2018-11-01 16:55:30 +03:00
saving: false,
saved: false,
2018-11-01 16:55:30 +03:00
error: true,
});
});
it('returns result on CREATE_SHORT_URL', () => {
expect(reducer(undefined, action(createShortUrl.fulfilled.toString(), { payload: shortUrl }))).toEqual({
result: shortUrl,
2018-11-01 16:55:30 +03:00
saving: false,
saved: true,
2018-11-01 16:55:30 +03:00
error: false,
});
});
it('returns default state on RESET_CREATE_SHORT_URL', () => {
expect(reducer(undefined, action(resetCreateShortUrl.toString()))).toEqual({
2018-11-01 16:55:30 +03:00
saving: false,
saved: false,
2018-11-01 16:55:30 +03:00
error: false,
});
});
});
describe('resetCreateShortUrl', () => {
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();
const getState = () => Mock.all<ShlinkState>();
2018-11-01 16:55:30 +03:00
it('calls API on success', async () => {
createShortUrlCall.mockResolvedValue(shortUrl);
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
2018-11-01 16:55:30 +03:00
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
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 () => {
const error = new Error('Error message');
createShortUrlCall.mockRejectedValue(error);
2020-04-10 19:42:08 +03:00
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
2018-11-01 16:55:30 +03:00
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
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
});
});
});