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

228 lines
8.4 KiB
TypeScript
Raw Normal View History

import { Mock } from 'ts-mockery';
2023-02-18 13:11:01 +03:00
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkPaginator, ShlinkShortUrlsResponse } from '../../../src/api/types';
import type { ShortUrl } from '../../../src/short-urls/data';
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';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
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();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ listShortUrls: listShortUrlsMock });
const listShortUrls = listShortUrlsCreator(buildShlinkApiClient);
const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
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', () =>
2022-11-09 21:13:44 +03:00
expect(reducer(undefined, { type: listShortUrls.pending.toString() })).toEqual({
2018-12-21 12:58:51 +03:00
loading: true,
error: false,
}));
it('returns short URLs on LIST_SHORT_URLS', () =>
2022-11-09 21:13:44 +03:00
expect(reducer(undefined, { type: listShortUrls.fulfilled.toString(), payload: { data: [] } })).toEqual({
shortUrls: { data: [] },
2018-12-21 12:58:51 +03:00
loading: false,
error: false,
}));
it('returns error on LIST_SHORT_URLS_ERROR', () =>
2022-11-09 21:13:44 +03:00
expect(reducer(undefined, { type: listShortUrls.rejected.toString() })).toEqual({
2018-12-21 12:58:51 +03:00
loading: false,
error: true,
}));
it('removes matching URL and reduces total on SHORT_URL_DELETED', () => {
2018-12-21 12:58:51 +03:00
const state = {
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
2018-12-21 12:58:51 +03:00
data: [
Mock.of<ShortUrl>({ shortCode }),
Mock.of<ShortUrl>({ shortCode, domain: 'example.com' }),
Mock.of<ShortUrl>({ shortCode: 'foo' }),
2018-12-21 12:58:51 +03:00
],
pagination: Mock.of<ShlinkPaginator>({
totalItems: 10,
}),
}),
loading: false,
error: false,
2018-12-21 12:58:51 +03:00
};
expect(reducer(state, { type: shortUrlDeleted.toString(), payload: { shortCode } })).toEqual({
2018-12-21 12:58:51 +03:00
shortUrls: {
data: [{ shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
pagination: { totalItems: 9 },
},
loading: false,
error: false,
});
});
const createNewShortUrlVisit = (visitsCount: number) => ({
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],
[[], 10],
])('updates visits count on CREATE_VISITS', (createdVisits, expectedCount) => {
const state = {
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
data: [
Mock.of<ShortUrl>({ shortCode, domain: 'example.com', visitsCount: 5 }),
Mock.of<ShortUrl>({ shortCode, visitsCount: 10 }),
Mock.of<ShortUrl>({ shortCode: 'foo', visitsCount: 8 }),
],
}),
loading: false,
error: false,
};
2022-11-09 21:13:44 +03:00
expect(reducer(state, { type: createNewVisits.toString(), payload: { createdVisits } })).toEqual({
shortUrls: {
data: [
{ shortCode, domain: 'example.com', visitsCount: 5 },
{ shortCode, visitsCount: expectedCount },
{ shortCode: 'foo', visitsCount: 8 },
],
},
loading: false,
error: false,
});
});
2022-01-08 12:51:34 +03:00
it.each([
[
[
Mock.of<ShortUrl>({ shortCode }),
Mock.of<ShortUrl>({ shortCode, domain: 'example.com' }),
Mock.of<ShortUrl>({ shortCode: 'foo' }),
],
[{ shortCode: 'newOne' }, { shortCode }, { shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
],
[
[
Mock.of<ShortUrl>({ shortCode }),
Mock.of<ShortUrl>({ shortCode: 'code' }),
Mock.of<ShortUrl>({ shortCode: 'foo' }),
Mock.of<ShortUrl>({ shortCode: 'bar' }),
Mock.of<ShortUrl>({ shortCode: 'baz' }),
],
[{ shortCode: 'newOne' }, { shortCode }, { shortCode: 'code' }, { shortCode: 'foo' }, { shortCode: 'bar' }],
],
[
[
Mock.of<ShortUrl>({ shortCode }),
Mock.of<ShortUrl>({ shortCode: 'code' }),
Mock.of<ShortUrl>({ shortCode: 'foo' }),
Mock.of<ShortUrl>({ shortCode: 'bar' }),
Mock.of<ShortUrl>({ shortCode: 'baz1' }),
Mock.of<ShortUrl>({ shortCode: 'baz2' }),
Mock.of<ShortUrl>({ shortCode: 'baz3' }),
],
[{ shortCode: 'newOne' }, { shortCode }, { shortCode: 'code' }, { shortCode: 'foo' }, { shortCode: 'bar' }],
],
])('prepends new short URL and increases total on CREATE_SHORT_URL', (data, expectedData) => {
const newShortUrl = Mock.of<ShortUrl>({ shortCode: 'newOne' });
const state = {
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
2022-01-08 12:51:34 +03:00
data,
pagination: Mock.of<ShlinkPaginator>({
totalItems: 15,
}),
}),
loading: false,
error: false,
};
2022-11-09 21:13:44 +03:00
expect(reducer(state, { type: createShortUrl.fulfilled.toString(), payload: newShortUrl })).toEqual({
shortUrls: {
2022-01-08 12:51:34 +03:00
data: expectedData,
pagination: { totalItems: 16 },
},
loading: false,
error: false,
});
});
it.each([
((): [ShortUrl, ShortUrl[], ShortUrl[]] => {
const editedShortUrl = Mock.of<ShortUrl>({ shortCode: 'notMatching' });
2022-03-26 14:17:42 +03:00
const list = [Mock.of<ShortUrl>({ shortCode: 'foo' }), Mock.of<ShortUrl>({ shortCode: 'bar' })];
2022-03-26 14:17:42 +03:00
return [editedShortUrl, list, list];
})(),
((): [ShortUrl, ShortUrl[], ShortUrl[]] => {
const editedShortUrl = Mock.of<ShortUrl>({ shortCode: 'matching', longUrl: 'new_one' });
const list = [
Mock.of<ShortUrl>({ shortCode: 'matching', longUrl: 'old_one' }),
Mock.of<ShortUrl>({ shortCode: 'bar' }),
];
2022-03-26 14:17:42 +03:00
const expectedList = [editedShortUrl, list[1]];
2022-03-26 14:17:42 +03:00
return [editedShortUrl, list, expectedList];
})(),
])('updates matching short URL on SHORT_URL_EDITED', (editedShortUrl, initialList, expectedList) => {
const state = {
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
data: initialList,
pagination: Mock.of<ShlinkPaginator>({
totalItems: 15,
}),
}),
loading: false,
error: false,
};
2022-11-09 21:13:44 +03:00
const result = reducer(state, { type: editShortUrl.fulfilled.toString(), payload: editedShortUrl });
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
expect(dispatch).toHaveBeenCalledTimes(2);
2022-11-09 21:13:44 +03:00
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: listShortUrls.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: listShortUrls.fulfilled.toString(),
payload: {},
}));
2018-12-21 12:58:51 +03:00
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
2018-12-21 12:58:51 +03:00
});
it('dispatches proper actions if API client request fails', async () => {
2022-11-09 21:13:44 +03:00
listShortUrlsMock.mockRejectedValue(undefined);
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
expect(dispatch).toHaveBeenCalledTimes(2);
2022-11-09 21:13:44 +03:00
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: listShortUrls.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ type: listShortUrls.rejected.toString() }));
2018-12-21 12:58:51 +03:00
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
2018-12-21 12:58:51 +03:00
});
});
});