2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
|
|
|
import type { ShlinkState } from '../../../src/container/types';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrl } from '../../../src/short-urls/data';
|
|
|
|
import { shortUrlDetailReducerCreator } from '../../../src/short-urls/reducers/shortUrlDetail';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { ShortUrlsList } from '../../../src/short-urls/reducers/shortUrlsList';
|
2018-09-07 21:41:21 +03:00
|
|
|
|
|
|
|
describe('shortUrlDetailReducer', () => {
|
2022-11-06 21:32:02 +03:00
|
|
|
const getShortUrlCall = jest.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ getShortUrl: getShortUrlCall });
|
2022-11-06 21:32:02 +03:00
|
|
|
const { reducer, getShortUrlDetail } = shortUrlDetailReducerCreator(buildShlinkApiClient);
|
|
|
|
|
2021-03-05 18:25:20 +03:00
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
|
2018-09-07 21:41:21 +03:00
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
|
2023-03-18 14:35:33 +03:00
|
|
|
const { loading } = reducer({ loading: false, error: false }, getShortUrlDetail.pending('', { shortCode: '' }));
|
2018-09-07 21:41:21 +03:00
|
|
|
expect(loading).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stops loading and returns error on GET_SHORT_URL_DETAIL_ERROR', () => {
|
2023-03-18 14:35:33 +03:00
|
|
|
const state = reducer({ loading: true, error: false }, getShortUrlDetail.rejected(null, '', { shortCode: '' }));
|
2018-09-07 21:41:21 +03:00
|
|
|
const { loading, error } = state;
|
|
|
|
|
|
|
|
expect(loading).toEqual(false);
|
|
|
|
expect(error).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return short URL on GET_SHORT_URL_DETAIL', () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const actionShortUrl = fromPartial<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
|
2022-11-06 21:32:02 +03:00
|
|
|
const state = reducer(
|
|
|
|
{ loading: true, error: false },
|
2023-03-18 14:35:33 +03:00
|
|
|
getShortUrlDetail.fulfilled(actionShortUrl, '', { shortCode: '' }),
|
2022-11-06 21:32:02 +03:00
|
|
|
);
|
2018-09-07 21:41:21 +03:00
|
|
|
const { loading, error, shortUrl } = state;
|
|
|
|
|
|
|
|
expect(loading).toEqual(false);
|
|
|
|
expect(error).toEqual(false);
|
|
|
|
expect(shortUrl).toEqual(actionShortUrl);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getShortUrlDetail', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const dispatchMock = jest.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const buildGetState = (shortUrlsList?: ShortUrlsList) => () => fromPartial<ShlinkState>({ shortUrlsList });
|
2018-09-07 21:41:21 +03:00
|
|
|
|
2021-03-05 18:25:20 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined],
|
2023-04-13 23:47:13 +03:00
|
|
|
[fromPartial<ShortUrlsList>({})],
|
2021-03-05 18:25:20 +03:00
|
|
|
[
|
2023-04-13 23:47:13 +03:00
|
|
|
fromPartial<ShortUrlsList>({
|
2021-03-05 18:25:20 +03:00
|
|
|
shortUrls: { data: [] },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
[
|
2023-04-13 23:47:13 +03:00
|
|
|
fromPartial<ShortUrlsList>({
|
2021-03-05 18:25:20 +03:00
|
|
|
shortUrls: {
|
2023-04-13 23:47:13 +03:00
|
|
|
data: [{ shortCode: 'this_will_not_match' }],
|
2021-03-05 18:25:20 +03:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
])('performs API call when short URL is not found in local state', async (shortUrlsList?: ShortUrlsList) => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const resolvedShortUrl = fromPartial<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
|
2022-11-06 21:32:02 +03:00
|
|
|
getShortUrlCall.mockResolvedValue(resolvedShortUrl);
|
2018-09-07 21:41:21 +03:00
|
|
|
|
2022-11-06 21:32:02 +03:00
|
|
|
await getShortUrlDetail({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState(shortUrlsList), {});
|
2018-09-07 21:41:21 +03:00
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: resolvedShortUrl }));
|
2022-11-06 21:32:02 +03:00
|
|
|
expect(getShortUrlCall).toHaveBeenCalledTimes(1);
|
2018-09-07 21:41:21 +03:00
|
|
|
});
|
2021-03-05 18:25:20 +03:00
|
|
|
|
|
|
|
it('avoids API calls when short URL is found in local state', async () => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const foundShortUrl = fromPartial<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
|
|
|
|
getShortUrlCall.mockResolvedValue(fromPartial<ShortUrl>({}));
|
2021-03-05 18:25:20 +03:00
|
|
|
|
2022-11-06 21:32:02 +03:00
|
|
|
await getShortUrlDetail(foundShortUrl)(
|
2021-03-05 18:25:20 +03:00
|
|
|
dispatchMock,
|
2023-04-13 23:47:13 +03:00
|
|
|
buildGetState(fromPartial({
|
2021-03-05 18:25:20 +03:00
|
|
|
shortUrls: {
|
2022-03-26 14:17:42 +03:00
|
|
|
data: [foundShortUrl],
|
2021-03-05 18:25:20 +03:00
|
|
|
},
|
|
|
|
})),
|
2022-11-06 21:32:02 +03:00
|
|
|
{},
|
2021-03-05 18:25:20 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: foundShortUrl }));
|
2022-11-06 21:32:02 +03:00
|
|
|
expect(getShortUrlCall).not.toHaveBeenCalled();
|
2021-03-05 18:25:20 +03:00
|
|
|
});
|
2018-09-07 21:41:21 +03:00
|
|
|
});
|
|
|
|
});
|