2020-08-28 19:33:37 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2018-09-07 21:41:21 +03:00
|
|
|
import reducer, {
|
2018-12-18 16:32:02 +03:00
|
|
|
getShortUrlDetail,
|
2018-09-07 21:41:21 +03:00
|
|
|
GET_SHORT_URL_DETAIL_START,
|
|
|
|
GET_SHORT_URL_DETAIL_ERROR,
|
|
|
|
GET_SHORT_URL_DETAIL,
|
2020-08-28 19:33:37 +03:00
|
|
|
ShortUrlDetailAction,
|
2021-03-05 18:04:02 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlDetail';
|
2020-08-28 19:33:37 +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-28 19:33:37 +03:00
|
|
|
import { ShlinkState } from '../../../src/container/types';
|
2021-03-05 18:25:20 +03:00
|
|
|
import { ShortUrlsList } from '../../../src/short-urls/reducers/shortUrlsList';
|
2018-09-07 21:41:21 +03:00
|
|
|
|
|
|
|
describe('shortUrlDetailReducer', () => {
|
2021-03-05 18:25:20 +03:00
|
|
|
beforeEach(jest.clearAllMocks);
|
|
|
|
|
2018-09-07 21:41:21 +03:00
|
|
|
describe('reducer', () => {
|
2020-08-28 19:33:37 +03:00
|
|
|
const action = (type: string) => Mock.of<ShortUrlDetailAction>({ type });
|
|
|
|
|
2018-09-07 21:41:21 +03:00
|
|
|
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
|
2020-08-28 19:33:37 +03:00
|
|
|
const state = reducer({ loading: false, error: false }, action(GET_SHORT_URL_DETAIL_START));
|
2018-09-07 21:41:21 +03:00
|
|
|
const { loading } = state;
|
|
|
|
|
|
|
|
expect(loading).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stops loading and returns error on GET_SHORT_URL_DETAIL_ERROR', () => {
|
2020-08-28 19:33:37 +03:00
|
|
|
const state = reducer({ loading: true, error: false }, action(GET_SHORT_URL_DETAIL_ERROR));
|
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', () => {
|
2020-08-28 19:33:37 +03:00
|
|
|
const actionShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
|
2018-09-07 21:41:21 +03:00
|
|
|
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL, shortUrl: actionShortUrl });
|
|
|
|
const { loading, error, shortUrl } = state;
|
|
|
|
|
|
|
|
expect(loading).toEqual(false);
|
|
|
|
expect(error).toEqual(false);
|
|
|
|
expect(shortUrl).toEqual(actionShortUrl);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getShortUrlDetail', () => {
|
2020-08-28 19:33:37 +03:00
|
|
|
const buildApiClientMock = (returned: Promise<ShortUrl>) => Mock.of<ShlinkApiClient>({
|
|
|
|
getShortUrl: jest.fn(async () => returned),
|
2018-09-07 21:41:21 +03:00
|
|
|
});
|
2019-04-19 13:41:59 +03:00
|
|
|
const dispatchMock = jest.fn();
|
2021-03-05 18:25:20 +03:00
|
|
|
const buildGetState = (shortUrlsList?: ShortUrlsList) => () => Mock.of<ShlinkState>({ shortUrlsList });
|
2018-09-07 21:41:21 +03:00
|
|
|
|
|
|
|
it('dispatches start and error when promise is rejected', async () => {
|
2021-03-20 18:32:12 +03:00
|
|
|
const ShlinkApiClient = buildApiClientMock(Promise.reject({}));
|
2018-09-07 21:41:21 +03:00
|
|
|
|
2021-03-05 18:25:20 +03:00
|
|
|
await getShortUrlDetail(() => ShlinkApiClient)('abc123', '')(dispatchMock, buildGetState());
|
2018-09-07 21:41:21 +03:00
|
|
|
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
|
|
|
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL_ERROR });
|
|
|
|
expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
|
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],
|
|
|
|
[Mock.all<ShortUrlsList>()],
|
2021-03-05 18:25:20 +03:00
|
|
|
[
|
|
|
|
Mock.of<ShortUrlsList>({
|
|
|
|
shortUrls: { data: [] },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
Mock.of<ShortUrlsList>({
|
|
|
|
shortUrls: {
|
2022-03-26 14:17:42 +03:00
|
|
|
data: [Mock.of<ShortUrl>({ 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) => {
|
|
|
|
const resolvedShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
|
2018-09-07 21:41:21 +03:00
|
|
|
const ShlinkApiClient = buildApiClientMock(Promise.resolve(resolvedShortUrl));
|
|
|
|
|
2021-03-05 18:25:20 +03:00
|
|
|
await getShortUrlDetail(() => ShlinkApiClient)('abc123', '')(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).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
|
|
|
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, shortUrl: resolvedShortUrl });
|
|
|
|
expect(ShlinkApiClient.getShortUrl).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 () => {
|
|
|
|
const foundShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
|
|
|
|
const ShlinkApiClient = buildApiClientMock(Promise.resolve(Mock.all<ShortUrl>()));
|
|
|
|
|
|
|
|
await getShortUrlDetail(() => ShlinkApiClient)(foundShortUrl.shortCode, foundShortUrl.domain)(
|
|
|
|
dispatchMock,
|
|
|
|
buildGetState(Mock.of<ShortUrlsList>({
|
|
|
|
shortUrls: {
|
2022-03-26 14:17:42 +03:00
|
|
|
data: [foundShortUrl],
|
2021-03-05 18:25:20 +03:00
|
|
|
},
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
|
|
|
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, shortUrl: foundShortUrl });
|
|
|
|
expect(ShlinkApiClient.getShortUrl).not.toHaveBeenCalled();
|
|
|
|
});
|
2018-09-07 21:41:21 +03:00
|
|
|
});
|
|
|
|
});
|