2023-04-13 23:47:13 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
|
2023-04-13 23:47:13 +03:00
|
|
|
import type { ShlinkShortUrlsResponse } from '../../../src/api/types';
|
|
|
|
import type { ShortUrl } from '../../../src/short-urls/data';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { createShortUrl as createShortUrlCreator } from '../../../src/short-urls/reducers/shortUrlCreation';
|
|
|
|
import { shortUrlDeleted } from '../../../src/short-urls/reducers/shortUrlDeletion';
|
|
|
|
import { editShortUrl as editShortUrlCreator } from '../../../src/short-urls/reducers/shortUrlEdition';
|
2022-11-09 21:13:44 +03:00
|
|
|
import {
|
|
|
|
listShortUrls as listShortUrlsCreator,
|
|
|
|
shortUrlsListReducerCreator,
|
2018-12-21 12:58:51 +03:00
|
|
|
} from '../../../src/short-urls/reducers/shortUrlsList';
|
2022-11-08 00:29:15 +03:00
|
|
|
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
|
2023-03-18 14:35:33 +03:00
|
|
|
import type { CreateVisit } from '../../../src/visits/types';
|
2018-12-21 12:58:51 +03:00
|
|
|
|
|
|
|
describe('shortUrlsListReducer', () => {
|
2022-01-08 12:51:34 +03:00
|
|
|
const shortCode = 'abc123';
|
2022-11-09 21:13:44 +03:00
|
|
|
const listShortUrlsMock = jest.fn();
|
2023-04-13 23:47:13 +03:00
|
|
|
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ listShortUrls: listShortUrlsMock });
|
2022-11-09 21:13:44 +03:00
|
|
|
const listShortUrls = listShortUrlsCreator(buildShlinkApiClient);
|
|
|
|
const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
|
|
|
|
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
|
2022-11-22 21:39:07 +03:00
|
|
|
const { reducer } = shortUrlsListReducerCreator(listShortUrls, editShortUrl, createShortUrl);
|
2022-11-09 21:13:44 +03:00
|
|
|
|
|
|
|
afterEach(jest.clearAllMocks);
|
2022-01-08 12:51:34 +03:00
|
|
|
|
2018-12-21 12:58:51 +03:00
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on LIST_SHORT_URLS_START', () =>
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, listShortUrls.pending(''))).toEqual({
|
2018-12-21 12:58:51 +03:00
|
|
|
loading: true,
|
|
|
|
error: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns short URLs on LIST_SHORT_URLS', () =>
|
2023-04-13 23:47:13 +03:00
|
|
|
expect(reducer(undefined, listShortUrls.fulfilled(fromPartial({ data: [] }), ''))).toEqual({
|
2020-08-27 19:31:56 +03:00
|
|
|
shortUrls: { data: [] },
|
2018-12-21 12:58:51 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns error on LIST_SHORT_URLS_ERROR', () =>
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(undefined, listShortUrls.rejected(null, ''))).toEqual({
|
2018-12-21 12:58:51 +03:00
|
|
|
loading: false,
|
|
|
|
error: true,
|
|
|
|
}));
|
|
|
|
|
2020-12-08 12:57:27 +03:00
|
|
|
it('removes matching URL and reduces total on SHORT_URL_DELETED', () => {
|
2018-12-21 12:58:51 +03:00
|
|
|
const state = {
|
2023-04-13 23:47:13 +03:00
|
|
|
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
|
2018-12-21 12:58:51 +03:00
|
|
|
data: [
|
2023-04-13 23:47:13 +03:00
|
|
|
{ shortCode },
|
|
|
|
{ shortCode, domain: 'example.com' },
|
|
|
|
{ shortCode: 'foo' },
|
2018-12-21 12:58:51 +03:00
|
|
|
],
|
2023-04-13 23:47:13 +03:00
|
|
|
pagination: { totalItems: 10 },
|
2020-08-30 20:45:17 +03:00
|
|
|
}),
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2018-12-21 12:58:51 +03:00
|
|
|
};
|
|
|
|
|
2023-04-13 23:47:13 +03:00
|
|
|
expect(reducer(state, shortUrlDeleted(fromPartial({ shortCode })))).toEqual({
|
2018-12-21 12:58:51 +03:00
|
|
|
shortUrls: {
|
2020-02-08 12:46:11 +03:00
|
|
|
data: [{ shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
|
2020-12-08 12:57:27 +03:00
|
|
|
pagination: { totalItems: 9 },
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-13 23:47:13 +03:00
|
|
|
const createNewShortUrlVisit = (visitsCount: number) => fromPartial<CreateVisit>({
|
2021-02-27 22:03:51 +03:00
|
|
|
shortUrl: { shortCode: 'abc123', visitsCount },
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[[createNewShortUrlVisit(11)], 11],
|
|
|
|
[[createNewShortUrlVisit(30)], 30],
|
|
|
|
[[createNewShortUrlVisit(20), createNewShortUrlVisit(40)], 40],
|
|
|
|
[[], 10],
|
2021-02-27 22:03:51 +03:00
|
|
|
])('updates visits count on CREATE_VISITS', (createdVisits, expectedCount) => {
|
2020-04-18 13:09:51 +03:00
|
|
|
const state = {
|
2023-04-13 23:47:13 +03:00
|
|
|
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
|
2020-04-18 13:09:51 +03:00
|
|
|
data: [
|
2023-04-13 23:47:13 +03:00
|
|
|
{ shortCode, domain: 'example.com', visitsCount: 5 },
|
|
|
|
{ shortCode, visitsCount: 10 },
|
|
|
|
{ shortCode: 'foo', visitsCount: 8 },
|
2020-04-18 13:09:51 +03:00
|
|
|
],
|
2020-08-30 20:45:17 +03:00
|
|
|
}),
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-04-18 13:09:51 +03:00
|
|
|
};
|
|
|
|
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(reducer(state, createNewVisits(createdVisits))).toEqual({
|
2020-04-18 13:09:51 +03:00
|
|
|
shortUrls: {
|
|
|
|
data: [
|
|
|
|
{ shortCode, domain: 'example.com', visitsCount: 5 },
|
2021-02-27 22:03:51 +03:00
|
|
|
{ shortCode, visitsCount: expectedCount },
|
2020-04-18 13:09:51 +03:00
|
|
|
{ shortCode: 'foo', visitsCount: 8 },
|
|
|
|
],
|
|
|
|
},
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
});
|
2020-12-08 12:57:27 +03:00
|
|
|
|
2022-01-08 12:51:34 +03:00
|
|
|
it.each([
|
|
|
|
[
|
|
|
|
[
|
2023-04-13 23:47:13 +03:00
|
|
|
fromPartial<ShortUrl>({ shortCode }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode, domain: 'example.com' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'foo' }),
|
2022-01-08 12:51:34 +03:00
|
|
|
],
|
|
|
|
[{ shortCode: 'newOne' }, { shortCode }, { shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2023-04-13 23:47:13 +03:00
|
|
|
fromPartial<ShortUrl>({ shortCode }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'code' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'foo' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'bar' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'baz' }),
|
2022-01-08 12:51:34 +03:00
|
|
|
],
|
|
|
|
[{ shortCode: 'newOne' }, { shortCode }, { shortCode: 'code' }, { shortCode: 'foo' }, { shortCode: 'bar' }],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
2023-04-13 23:47:13 +03:00
|
|
|
fromPartial<ShortUrl>({ shortCode }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'code' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'foo' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'bar' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'baz1' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'baz2' }),
|
|
|
|
fromPartial<ShortUrl>({ shortCode: 'baz3' }),
|
2022-01-08 12:51:34 +03:00
|
|
|
],
|
|
|
|
[{ shortCode: 'newOne' }, { shortCode }, { shortCode: 'code' }, { shortCode: 'foo' }, { shortCode: 'bar' }],
|
|
|
|
],
|
|
|
|
])('prepends new short URL and increases total on CREATE_SHORT_URL', (data, expectedData) => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const newShortUrl = fromPartial<ShortUrl>({ shortCode: 'newOne' });
|
2020-12-08 12:57:27 +03:00
|
|
|
const state = {
|
2023-04-13 23:47:13 +03:00
|
|
|
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
|
2022-01-08 12:51:34 +03:00
|
|
|
data,
|
2023-04-13 23:47:13 +03:00
|
|
|
pagination: { totalItems: 15 },
|
2020-12-08 12:57:27 +03:00
|
|
|
}),
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2023-04-13 23:47:13 +03:00
|
|
|
expect(reducer(state, createShortUrl.fulfilled(newShortUrl, '', fromPartial({})))).toEqual({
|
2020-12-08 12:57:27 +03:00
|
|
|
shortUrls: {
|
2022-01-08 12:51:34 +03:00
|
|
|
data: expectedData,
|
2020-12-08 12:57:27 +03:00
|
|
|
pagination: { totalItems: 16 },
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
2021-04-24 18:58:37 +03:00
|
|
|
|
|
|
|
it.each([
|
|
|
|
((): [ShortUrl, ShortUrl[], ShortUrl[]] => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const editedShortUrl = fromPartial<ShortUrl>({ shortCode: 'notMatching' });
|
|
|
|
const list: ShortUrl[] = [fromPartial({ shortCode: 'foo' }), fromPartial({ shortCode: 'bar' })];
|
2021-04-24 18:58:37 +03:00
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
return [editedShortUrl, list, list];
|
2021-04-24 18:58:37 +03:00
|
|
|
})(),
|
|
|
|
((): [ShortUrl, ShortUrl[], ShortUrl[]] => {
|
2023-04-13 23:47:13 +03:00
|
|
|
const editedShortUrl = fromPartial<ShortUrl>({ shortCode: 'matching', longUrl: 'new_one' });
|
|
|
|
const list: ShortUrl[] = [
|
|
|
|
fromPartial({ shortCode: 'matching', longUrl: 'old_one' }),
|
|
|
|
fromPartial({ shortCode: 'bar' }),
|
2021-04-24 18:58:37 +03:00
|
|
|
];
|
2022-03-26 14:17:42 +03:00
|
|
|
const expectedList = [editedShortUrl, list[1]];
|
2021-04-24 18:58:37 +03:00
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
return [editedShortUrl, list, expectedList];
|
2021-04-24 18:58:37 +03:00
|
|
|
})(),
|
|
|
|
])('updates matching short URL on SHORT_URL_EDITED', (editedShortUrl, initialList, expectedList) => {
|
|
|
|
const state = {
|
2023-04-13 23:47:13 +03:00
|
|
|
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
|
2021-04-24 18:58:37 +03:00
|
|
|
data: initialList,
|
2023-04-13 23:47:13 +03:00
|
|
|
pagination: { totalItems: 15 },
|
2021-04-24 18:58:37 +03:00
|
|
|
}),
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2023-04-13 23:47:13 +03:00
|
|
|
const result = reducer(state, editShortUrl.fulfilled(editedShortUrl, '', fromPartial({})));
|
2021-04-24 18:58:37 +03:00
|
|
|
|
|
|
|
expect(result.shortUrls?.data).toEqual(expectedList);
|
|
|
|
});
|
2018-12-21 12:58:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('listShortUrls', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const dispatch = jest.fn();
|
|
|
|
const getState = jest.fn().mockReturnValue({ selectedServer: {} });
|
2018-12-21 12:58:51 +03:00
|
|
|
|
|
|
|
it('dispatches proper actions if API client request succeeds', async () => {
|
2022-11-09 21:13:44 +03:00
|
|
|
listShortUrlsMock.mockResolvedValue({});
|
2018-12-21 12:58:51 +03:00
|
|
|
|
2022-11-09 21:13:44 +03:00
|
|
|
await listShortUrls()(dispatch, getState, {});
|
2018-12-21 12:58:51 +03:00
|
|
|
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: {} }));
|
2018-12-21 12:58:51 +03:00
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
|
2018-12-21 12:58:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|