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

91 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-04-13 23:47:13 +03:00
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShortUrl } from '../../../shlink-web-component/short-urls/data';
import { shortUrlDetailReducerCreator } from '../../../shlink-web-component/short-urls/reducers/shortUrlDetail';
import type { ShortUrlsList } from '../../../shlink-web-component/short-urls/reducers/shortUrlsList';
2023-02-18 12:40:37 +03:00
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkState } from '../../../src/container/types';
2018-09-07 21:41:21 +03:00
describe('shortUrlDetailReducer', () => {
2023-05-27 12:57:26 +03:00
const getShortUrlCall = vi.fn();
2023-04-13 23:47:13 +03:00
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ getShortUrl: getShortUrlCall });
const { reducer, getShortUrlDetail } = shortUrlDetailReducerCreator(buildShlinkApiClient);
2018-09-07 21:41:21 +03:00
describe('reducer', () => {
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
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', () => {
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' });
const state = reducer(
{ loading: true, error: false },
getShortUrlDetail.fulfilled(actionShortUrl, '', { shortCode: '' }),
);
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', () => {
2023-05-27 12:57:26 +03:00
const dispatchMock = vi.fn();
2023-04-13 23:47:13 +03:00
const buildGetState = (shortUrlsList?: ShortUrlsList) => () => fromPartial<ShlinkState>({ shortUrlsList });
2018-09-07 21:41:21 +03:00
it.each([
2022-03-26 14:17:42 +03:00
[undefined],
2023-04-13 23:47:13 +03:00
[fromPartial<ShortUrlsList>({})],
[
2023-04-13 23:47:13 +03:00
fromPartial<ShortUrlsList>({
shortUrls: { data: [] },
}),
],
[
2023-04-13 23:47:13 +03:00
fromPartial<ShortUrlsList>({
shortUrls: {
2023-04-13 23:47:13 +03:00
data: [{ shortCode: 'this_will_not_match' }],
},
}),
],
])('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' });
getShortUrlCall.mockResolvedValue(resolvedShortUrl);
2018-09-07 21:41:21 +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);
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: resolvedShortUrl }));
expect(getShortUrlCall).toHaveBeenCalledTimes(1);
2018-09-07 21:41:21 +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>({}));
await getShortUrlDetail(foundShortUrl)(
dispatchMock,
2023-04-13 23:47:13 +03:00
buildGetState(fromPartial({
shortUrls: {
2022-03-26 14:17:42 +03:00
data: [foundShortUrl],
},
})),
{},
);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: foundShortUrl }));
expect(getShortUrlCall).not.toHaveBeenCalled();
});
2018-09-07 21:41:21 +03:00
});
});