Refactor of redux tests to avoid covering RTK implementation details

This commit is contained in:
Alejandro Celaya
2023-03-18 12:35:33 +01:00
parent 9cefdb7977
commit 4e8e16f16d
21 changed files with 222 additions and 730 deletions

View File

@@ -1,9 +1,7 @@
import { Mock } from 'ts-mockery';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkState } from '../../../src/container/types';
import type { ShortUrl } from '../../../src/short-urls/data';
import type {
CreateShortUrlAction } from '../../../src/short-urls/reducers/shortUrlCreation';
import type { ShortUrl, ShortUrlData } from '../../../src/short-urls/data';
import {
createShortUrl as createShortUrlCreator,
shortUrlCreationReducerCreator,
@@ -19,12 +17,8 @@ describe('shortUrlCreationReducer', () => {
afterEach(jest.resetAllMocks);
describe('reducer', () => {
const action = (type: string, args: Partial<CreateShortUrlAction> = {}) => Mock.of<CreateShortUrlAction>(
{ type, ...args },
);
it('returns loading on CREATE_SHORT_URL_START', () => {
expect(reducer(undefined, action(createShortUrl.pending.toString()))).toEqual({
expect(reducer(undefined, createShortUrl.pending('', Mock.all<ShortUrlData>()))).toEqual({
saving: true,
saved: false,
error: false,
@@ -32,7 +26,7 @@ describe('shortUrlCreationReducer', () => {
});
it('returns error on CREATE_SHORT_URL_ERROR', () => {
expect(reducer(undefined, action(createShortUrl.rejected.toString()))).toEqual({
expect(reducer(undefined, createShortUrl.rejected(null, '', Mock.all<ShortUrlData>()))).toEqual({
saving: false,
saved: false,
error: true,
@@ -40,7 +34,7 @@ describe('shortUrlCreationReducer', () => {
});
it('returns result on CREATE_SHORT_URL', () => {
expect(reducer(undefined, action(createShortUrl.fulfilled.toString(), { payload: shortUrl }))).toEqual({
expect(reducer(undefined, createShortUrl.fulfilled(shortUrl, '', Mock.all<ShortUrlData>()))).toEqual({
result: shortUrl,
saving: false,
saved: true,
@@ -49,7 +43,7 @@ describe('shortUrlCreationReducer', () => {
});
it('returns default state on RESET_CREATE_SHORT_URL', () => {
expect(reducer(undefined, action(resetCreateShortUrl.toString()))).toEqual({
expect(reducer(undefined, resetCreateShortUrl())).toEqual({
saving: false,
saved: false,
error: false,
@@ -57,10 +51,6 @@ describe('shortUrlCreationReducer', () => {
});
});
describe('resetCreateShortUrl', () => {
it('returns proper action', () => expect(resetCreateShortUrl()).toEqual({ type: resetCreateShortUrl.toString() }));
});
describe('createShortUrl', () => {
const dispatch = jest.fn();
const getState = () => Mock.all<ShlinkState>();
@@ -71,30 +61,7 @@ describe('shortUrlCreationReducer', () => {
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: createShortUrl.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: createShortUrl.fulfilled.toString(),
payload: shortUrl,
}));
});
it('throws on error', async () => {
const error = new Error('Error message');
createShortUrlCall.mockRejectedValue(error);
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: createShortUrl.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: createShortUrl.rejected.toString(),
error: expect.objectContaining({ message: 'Error message' }),
}));
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
});
});
});

View File

@@ -16,7 +16,7 @@ describe('shortUrlDeletionReducer', () => {
describe('reducer', () => {
it('returns loading on DELETE_SHORT_URL_START', () =>
expect(reducer(undefined, { type: deleteShortUrl.pending.toString() })).toEqual({
expect(reducer(undefined, deleteShortUrl.pending('', { shortCode: '' }))).toEqual({
shortCode: '',
loading: true,
error: false,
@@ -24,7 +24,7 @@ describe('shortUrlDeletionReducer', () => {
}));
it('returns default on RESET_DELETE_SHORT_URL', () =>
expect(reducer(undefined, { type: resetDeleteShortUrl.toString() })).toEqual({
expect(reducer(undefined, resetDeleteShortUrl())).toEqual({
shortCode: '',
loading: false,
error: false,
@@ -32,10 +32,7 @@ describe('shortUrlDeletionReducer', () => {
}));
it('returns shortCode on SHORT_URL_DELETED', () =>
expect(reducer(undefined, {
type: deleteShortUrl.fulfilled.toString(),
payload: { shortCode: 'foo' },
})).toEqual({
expect(reducer(undefined, deleteShortUrl.fulfilled({ shortCode: 'foo' }, '', { shortCode: 'foo' }))).toEqual({
shortCode: 'foo',
loading: false,
error: false,
@@ -44,9 +41,9 @@ describe('shortUrlDeletionReducer', () => {
it('returns errorData on DELETE_SHORT_URL_ERROR', () => {
const errorData = Mock.of<ProblemDetailsError>({ type: 'bar', detail: 'detail', title: 'title', status: 400 });
const error = errorData;
const error = errorData as unknown as Error;
expect(reducer(undefined, { type: deleteShortUrl.rejected.toString(), error })).toEqual({
expect(reducer(undefined, deleteShortUrl.rejected(error, '', { shortCode: '' }))).toEqual({
shortCode: '',
loading: false,
error: true,
@@ -56,11 +53,6 @@ describe('shortUrlDeletionReducer', () => {
});
});
describe('resetDeleteShortUrl', () => {
it('returns expected action', () =>
expect(resetDeleteShortUrl()).toEqual({ type: resetDeleteShortUrl.toString() }));
});
describe('deleteShortUrl', () => {
const dispatch = jest.fn();
const getState = jest.fn().mockReturnValue({ selectedServer: {} });
@@ -73,32 +65,12 @@ describe('shortUrlDeletionReducer', () => {
await deleteShortUrl({ shortCode, domain })(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: deleteShortUrl.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: deleteShortUrl.fulfilled.toString(),
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
payload: { shortCode, domain },
}));
expect(deleteShortUrlCall).toHaveBeenCalledTimes(1);
expect(deleteShortUrlCall).toHaveBeenCalledWith(shortCode, domain);
});
it('dispatches proper actions if API client request fails', async () => {
const data = { foo: 'bar' };
const shortCode = 'abc123';
deleteShortUrlCall.mockRejectedValue({ response: { data } });
await deleteShortUrl({ shortCode })(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: deleteShortUrl.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: deleteShortUrl.rejected.toString(),
}));
expect(deleteShortUrlCall).toHaveBeenCalledTimes(1);
expect(deleteShortUrlCall).toHaveBeenCalledWith(shortCode, undefined);
});
});
});

View File

@@ -2,7 +2,6 @@ import { Mock } from 'ts-mockery';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkState } from '../../../src/container/types';
import type { ShortUrl } from '../../../src/short-urls/data';
import type { ShortUrlDetailAction } from '../../../src/short-urls/reducers/shortUrlDetail';
import { shortUrlDetailReducerCreator } from '../../../src/short-urls/reducers/shortUrlDetail';
import type { ShortUrlsList } from '../../../src/short-urls/reducers/shortUrlsList';
@@ -14,17 +13,13 @@ describe('shortUrlDetailReducer', () => {
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const action = (type: string) => Mock.of<ShortUrlDetailAction>({ type });
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
const state = reducer({ loading: false, error: false }, action(getShortUrlDetail.pending.toString()));
const { loading } = state;
const { loading } = reducer({ loading: false, error: false }, getShortUrlDetail.pending('', { shortCode: '' }));
expect(loading).toEqual(true);
});
it('stops loading and returns error on GET_SHORT_URL_DETAIL_ERROR', () => {
const state = reducer({ loading: true, error: false }, action(getShortUrlDetail.rejected.toString()));
const state = reducer({ loading: true, error: false }, getShortUrlDetail.rejected(null, '', { shortCode: '' }));
const { loading, error } = state;
expect(loading).toEqual(false);
@@ -35,7 +30,7 @@ describe('shortUrlDetailReducer', () => {
const actionShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
const state = reducer(
{ loading: true, error: false },
{ type: getShortUrlDetail.fulfilled.toString(), payload: actionShortUrl },
getShortUrlDetail.fulfilled(actionShortUrl, '', { shortCode: '' }),
);
const { loading, error, shortUrl } = state;
@@ -49,21 +44,6 @@ describe('shortUrlDetailReducer', () => {
const dispatchMock = jest.fn();
const buildGetState = (shortUrlsList?: ShortUrlsList) => () => Mock.of<ShlinkState>({ shortUrlsList });
it('dispatches start and error when promise is rejected', async () => {
getShortUrlCall.mockRejectedValue({});
await getShortUrlDetail({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState(), {});
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: getShortUrlDetail.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: getShortUrlDetail.rejected.toString(),
}));
expect(getShortUrlCall).toHaveBeenCalledTimes(1);
});
it.each([
[undefined],
[Mock.all<ShortUrlsList>()],
@@ -86,13 +66,7 @@ describe('shortUrlDetailReducer', () => {
await getShortUrlDetail({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState(shortUrlsList), {});
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: getShortUrlDetail.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: getShortUrlDetail.fulfilled.toString(),
payload: resolvedShortUrl,
}));
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: resolvedShortUrl }));
expect(getShortUrlCall).toHaveBeenCalledTimes(1);
});
@@ -111,13 +85,7 @@ describe('shortUrlDetailReducer', () => {
);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: getShortUrlDetail.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: getShortUrlDetail.fulfilled.toString(),
payload: foundShortUrl,
}));
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: foundShortUrl }));
expect(getShortUrlCall).not.toHaveBeenCalled();
});
});

View File

@@ -2,8 +2,7 @@ import { Mock } from 'ts-mockery';
import type { ShlinkState } from '../../../src/container/types';
import type { SelectedServer } from '../../../src/servers/data';
import type { ShortUrl } from '../../../src/short-urls/data';
import type {
ShortUrlEditedAction } from '../../../src/short-urls/reducers/shortUrlEdition';
import type { EditShortUrl } from '../../../src/short-urls/reducers/shortUrlEdition';
import {
editShortUrl as editShortUrlCreator,
shortUrlEditionReducerCreator,
@@ -22,7 +21,7 @@ describe('shortUrlEditionReducer', () => {
describe('reducer', () => {
it('returns loading on EDIT_SHORT_URL_START', () => {
expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: editShortUrl.pending.toString() }))).toEqual({
expect(reducer(undefined, editShortUrl.pending('', Mock.all<EditShortUrl>()))).toEqual({
saving: true,
saved: false,
error: false,
@@ -30,7 +29,7 @@ describe('shortUrlEditionReducer', () => {
});
it('returns error on EDIT_SHORT_URL_ERROR', () => {
expect(reducer(undefined, Mock.of<ShortUrlEditedAction>({ type: editShortUrl.rejected.toString() }))).toEqual({
expect(reducer(undefined, editShortUrl.rejected(null, '', Mock.all<EditShortUrl>()))).toEqual({
saving: false,
saved: false,
error: true,
@@ -38,7 +37,7 @@ describe('shortUrlEditionReducer', () => {
});
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
expect(reducer(undefined, { type: editShortUrl.fulfilled.toString(), payload: shortUrl })).toEqual({
expect(reducer(undefined, editShortUrl.fulfilled(shortUrl, '', Mock.all<EditShortUrl>()))).toEqual({
shortUrl,
saving: false,
saved: true,
@@ -60,28 +59,7 @@ describe('shortUrlEditionReducer', () => {
expect(updateShortUrl).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, { longUrl });
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: editShortUrl.pending.toString(),
}));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: editShortUrl.fulfilled.toString(),
payload: shortUrl,
}));
});
it('dispatches error on failure', async () => {
const error = new Error();
updateShortUrl.mockRejectedValue(error);
await editShortUrl({ shortCode, data: { longUrl } })(dispatch, createGetState(), {});
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, undefined, { longUrl });
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: editShortUrl.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ type: editShortUrl.rejected.toString() }));
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
});
});
});

View File

@@ -1,15 +1,17 @@
import { Mock } from 'ts-mockery';
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 type { ShortUrl, ShortUrlData } from '../../../src/short-urls/data';
import { createShortUrl as createShortUrlCreator } from '../../../src/short-urls/reducers/shortUrlCreation';
import { shortUrlDeleted } from '../../../src/short-urls/reducers/shortUrlDeletion';
import type { EditShortUrl } from '../../../src/short-urls/reducers/shortUrlEdition';
import { editShortUrl as editShortUrlCreator } from '../../../src/short-urls/reducers/shortUrlEdition';
import {
listShortUrls as listShortUrlsCreator,
shortUrlsListReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlsList';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
import type { CreateVisit } from '../../../src/visits/types';
describe('shortUrlsListReducer', () => {
const shortCode = 'abc123';
@@ -24,20 +26,20 @@ describe('shortUrlsListReducer', () => {
describe('reducer', () => {
it('returns loading on LIST_SHORT_URLS_START', () =>
expect(reducer(undefined, { type: listShortUrls.pending.toString() })).toEqual({
expect(reducer(undefined, listShortUrls.pending(''))).toEqual({
loading: true,
error: false,
}));
it('returns short URLs on LIST_SHORT_URLS', () =>
expect(reducer(undefined, { type: listShortUrls.fulfilled.toString(), payload: { data: [] } })).toEqual({
expect(reducer(undefined, listShortUrls.fulfilled(Mock.of<ShlinkShortUrlsResponse>({ data: [] }), ''))).toEqual({
shortUrls: { data: [] },
loading: false,
error: false,
}));
it('returns error on LIST_SHORT_URLS_ERROR', () =>
expect(reducer(undefined, { type: listShortUrls.rejected.toString() })).toEqual({
expect(reducer(undefined, listShortUrls.rejected(null, ''))).toEqual({
loading: false,
error: true,
}));
@@ -58,7 +60,7 @@ describe('shortUrlsListReducer', () => {
error: false,
};
expect(reducer(state, { type: shortUrlDeleted.toString(), payload: { shortCode } })).toEqual({
expect(reducer(state, shortUrlDeleted(Mock.of<ShortUrl>({ shortCode })))).toEqual({
shortUrls: {
data: [{ shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
pagination: { totalItems: 9 },
@@ -68,7 +70,7 @@ describe('shortUrlsListReducer', () => {
});
});
const createNewShortUrlVisit = (visitsCount: number) => ({
const createNewShortUrlVisit = (visitsCount: number) => Mock.of<CreateVisit>({
shortUrl: { shortCode: 'abc123', visitsCount },
});
@@ -76,7 +78,6 @@ describe('shortUrlsListReducer', () => {
[[createNewShortUrlVisit(11)], 11],
[[createNewShortUrlVisit(30)], 30],
[[createNewShortUrlVisit(20), createNewShortUrlVisit(40)], 40],
[[{}], 10],
[[], 10],
])('updates visits count on CREATE_VISITS', (createdVisits, expectedCount) => {
const state = {
@@ -91,7 +92,7 @@ describe('shortUrlsListReducer', () => {
error: false,
};
expect(reducer(state, { type: createNewVisits.toString(), payload: { createdVisits } })).toEqual({
expect(reducer(state, createNewVisits(createdVisits))).toEqual({
shortUrls: {
data: [
{ shortCode, domain: 'example.com', visitsCount: 5 },
@@ -148,7 +149,7 @@ describe('shortUrlsListReducer', () => {
error: false,
};
expect(reducer(state, { type: createShortUrl.fulfilled.toString(), payload: newShortUrl })).toEqual({
expect(reducer(state, createShortUrl.fulfilled(newShortUrl, '', Mock.all<ShortUrlData>()))).toEqual({
shortUrls: {
data: expectedData,
pagination: { totalItems: 16 },
@@ -187,7 +188,7 @@ describe('shortUrlsListReducer', () => {
error: false,
};
const result = reducer(state, { type: editShortUrl.fulfilled.toString(), payload: editedShortUrl });
const result = reducer(state, editShortUrl.fulfilled(editedShortUrl, '', Mock.of<EditShortUrl>()));
expect(result.shortUrls?.data).toEqual(expectedList);
});
@@ -203,23 +204,7 @@ describe('shortUrlsListReducer', () => {
await listShortUrls()(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: listShortUrls.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: listShortUrls.fulfilled.toString(),
payload: {},
}));
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});
it('dispatches proper actions if API client request fails', async () => {
listShortUrlsMock.mockRejectedValue(undefined);
await listShortUrls()(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, expect.objectContaining({ type: listShortUrls.pending.toString() }));
expect(dispatch).toHaveBeenNthCalledWith(2, expect.objectContaining({ type: listShortUrls.rejected.toString() }));
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: {} }));
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});