2020-08-24 19:52:52 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2018-11-01 16:55:30 +03:00
|
|
|
import reducer, {
|
|
|
|
CREATE_SHORT_URL_START,
|
|
|
|
CREATE_SHORT_URL_ERROR,
|
|
|
|
CREATE_SHORT_URL,
|
|
|
|
RESET_CREATE_SHORT_URL,
|
2018-12-18 21:59:50 +03:00
|
|
|
createShortUrl,
|
2018-11-01 16:55:30 +03:00
|
|
|
resetCreateShortUrl,
|
2020-08-25 20:41:48 +03:00
|
|
|
CreateShortUrlAction,
|
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';
|
2020-12-22 11:55: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', () => {
|
2020-08-24 19:52:52 +03:00
|
|
|
const shortUrl = Mock.all<ShortUrl>();
|
|
|
|
|
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', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(reducer(undefined, action(CREATE_SHORT_URL_START))).toEqual({
|
2020-08-24 19:52:52 +03:00
|
|
|
result: null,
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: true,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns error on CREATE_SHORT_URL_ERROR', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(reducer(undefined, action(CREATE_SHORT_URL_ERROR))).toEqual({
|
2020-08-24 19:52:52 +03:00
|
|
|
result: null,
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns result on CREATE_SHORT_URL', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(reducer(undefined, action(CREATE_SHORT_URL, { result: shortUrl }))).toEqual({
|
2020-08-24 19:52:52 +03:00
|
|
|
result: shortUrl,
|
2018-11-01 16:55:30 +03:00
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns default state on RESET_CREATE_SHORT_URL', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
expect(reducer(undefined, action(RESET_CREATE_SHORT_URL))).toEqual({
|
2018-11-01 16:55:30 +03:00
|
|
|
result: null,
|
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resetCreateShortUrl', () => {
|
2020-08-25 20:41:48 +03:00
|
|
|
it('returns proper action', () => expect(resetCreateShortUrl()).toEqual({ type: RESET_CREATE_SHORT_URL }));
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('createShortUrl', () => {
|
2020-08-24 19:52:52 +03:00
|
|
|
const createApiClientMock = (result: Promise<ShortUrl>) => Mock.of<ShlinkApiClient>({
|
|
|
|
createShortUrl: jest.fn().mockReturnValue(result),
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
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
|
|
|
|
2020-04-18 13:09:51 +03:00
|
|
|
afterEach(jest.resetAllMocks);
|
2018-11-01 16:55:30 +03:00
|
|
|
|
|
|
|
it('calls API on success', async () => {
|
2020-08-24 19:52:52 +03:00
|
|
|
const apiClientMock = createApiClientMock(Promise.resolve(shortUrl));
|
|
|
|
const dispatchable = createShortUrl(() => apiClientMock)({ longUrl: 'foo' });
|
2018-11-01 16:55:30 +03:00
|
|
|
|
2018-12-18 12:23:09 +03:00
|
|
|
await dispatchable(dispatch, getState);
|
2018-11-01 16:55:30 +03:00
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
|
2020-08-24 19:52:52 +03:00
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL, result: shortUrl });
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws on error', async () => {
|
|
|
|
const error = 'Error';
|
|
|
|
const apiClientMock = createApiClientMock(Promise.reject(error));
|
2020-08-24 19:52:52 +03:00
|
|
|
const dispatchable = createShortUrl(() => apiClientMock)({ longUrl: 'foo' });
|
2018-11-01 16:55:30 +03:00
|
|
|
|
2020-04-10 19:42:08 +03:00
|
|
|
expect.assertions(5);
|
|
|
|
|
2018-11-01 16:55:30 +03:00
|
|
|
try {
|
2018-12-18 12:23:09 +03:00
|
|
|
await dispatchable(dispatch, getState);
|
2018-11-01 16:55:30 +03:00
|
|
|
} catch (e) {
|
|
|
|
expect(e).toEqual(error);
|
|
|
|
}
|
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL_ERROR });
|
2018-11-01 16:55:30 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|