Migrated visitsOverview reducer to RTK

This commit is contained in:
Alejandro Celaya
2022-11-11 20:23:19 +01:00
parent 002d2ba8e6
commit 5095a2c59e
4 changed files with 78 additions and 63 deletions

View File

@@ -1,11 +1,9 @@
import { Mock } from 'ts-mockery';
import reducer, {
GET_OVERVIEW_START,
GET_OVERVIEW_ERROR,
GET_OVERVIEW,
import {
GetVisitsOverviewAction,
VisitsOverview,
loadVisitsOverview,
loadVisitsOverview as loadVisitsOverviewCreator,
visitsOverviewReducerCreator,
} from '../../../src/visits/reducers/visitsOverview';
import { createNewVisits, CreateVisitsAction } from '../../../src/visits/reducers/visitCreation';
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
@@ -14,29 +12,42 @@ import { ShlinkState } from '../../../src/container/types';
import { CreateVisit, OrphanVisit, Visit } from '../../../src/visits/types';
describe('visitsOverviewReducer', () => {
const getVisitsOverview = jest.fn();
const buildApiClientMock = () => Mock.of<ShlinkApiClient>({ getVisitsOverview });
const loadVisitsOverview = loadVisitsOverviewCreator(buildApiClientMock);
const { reducer } = visitsOverviewReducerCreator(loadVisitsOverview);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const action = (type: string) =>
Mock.of<GetVisitsOverviewAction>({ type }) as GetVisitsOverviewAction & CreateVisitsAction;
const state = (payload: Partial<VisitsOverview> = {}) => Mock.of<VisitsOverview>(payload);
it('returns loading on GET_OVERVIEW_START', () => {
const { loading } = reducer(state({ loading: false, error: false }), action(GET_OVERVIEW_START));
const { loading } = reducer(
state({ loading: false, error: false }),
action(loadVisitsOverview.pending.toString()),
);
expect(loading).toEqual(true);
});
it('stops loading and returns error on GET_OVERVIEW_ERROR', () => {
const { loading, error } = reducer(state({ loading: true, error: false }), action(GET_OVERVIEW_ERROR));
const { loading, error } = reducer(
state({ loading: true, error: false }),
action(loadVisitsOverview.rejected.toString()),
);
expect(loading).toEqual(false);
expect(error).toEqual(true);
});
it('return visits overview on GET_OVERVIEW', () => {
const { loading, error, visitsCount } = reducer(
state({ loading: true, error: false }),
{ type: GET_OVERVIEW, visitsCount: 100 } as unknown as GetVisitsOverviewAction & CreateVisitsAction,
);
const { loading, error, visitsCount } = reducer(state({ loading: true, error: false }), {
type: loadVisitsOverview.fulfilled.toString(),
payload: { visitsCount: 100 },
});
expect(loading).toEqual(false);
expect(error).toEqual(false);
@@ -76,35 +87,41 @@ describe('visitsOverviewReducer', () => {
});
describe('loadVisitsOverview', () => {
const buildApiClientMock = (returned: Promise<ShlinkVisitsOverview>) => Mock.of<ShlinkApiClient>({
getVisitsOverview: jest.fn(async () => returned),
});
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>();
beforeEach(() => dispatchMock.mockReset());
it('dispatches start and error when promise is rejected', async () => {
const ShlinkApiClient = buildApiClientMock(Promise.reject());
getVisitsOverview.mockRejectedValue(undefined);
await loadVisitsOverview(() => ShlinkApiClient)()(dispatchMock, getState);
await loadVisitsOverview()(dispatchMock, getState, {});
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_OVERVIEW_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_OVERVIEW_ERROR });
expect(ShlinkApiClient.getVisitsOverview).toHaveBeenCalledTimes(1);
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadVisitsOverview.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadVisitsOverview.rejected.toString(),
}));
expect(getVisitsOverview).toHaveBeenCalledTimes(1);
});
it('dispatches start and success when promise is resolved', async () => {
const resolvedOverview = Mock.of<ShlinkVisitsOverview>({ visitsCount: 50 });
const shlinkApiClient = buildApiClientMock(Promise.resolve(resolvedOverview));
getVisitsOverview.mockResolvedValue(resolvedOverview);
await loadVisitsOverview(() => shlinkApiClient)()(dispatchMock, getState);
await loadVisitsOverview()(dispatchMock, getState, {});
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_OVERVIEW_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_OVERVIEW, visitsCount: 50 });
expect(shlinkApiClient.getVisitsOverview).toHaveBeenCalledTimes(1);
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadVisitsOverview.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadVisitsOverview.fulfilled.toString(),
payload: { visitsCount: 50 },
}));
expect(getVisitsOverview).toHaveBeenCalledTimes(1);
});
});
});