shlink-web-client/test/visits/reducers/visitsOverview.test.ts

128 lines
4.7 KiB
TypeScript
Raw Normal View History

2020-12-07 21:19:37 +03:00
import { Mock } from 'ts-mockery';
2022-11-11 22:23:19 +03:00
import {
2020-12-07 21:19:37 +03:00
GetVisitsOverviewAction,
VisitsOverview,
2022-11-11 22:23:19 +03:00
loadVisitsOverview as loadVisitsOverviewCreator,
visitsOverviewReducerCreator,
2020-12-07 21:19:37 +03:00
} from '../../../src/visits/reducers/visitsOverview';
import { createNewVisits, CreateVisitsAction } from '../../../src/visits/reducers/visitCreation';
2022-05-28 11:47:39 +03:00
import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import { ShlinkVisitsOverview } from '../../../src/api/types';
2020-12-07 21:19:37 +03:00
import { ShlinkState } from '../../../src/container/types';
import { CreateVisit, OrphanVisit, Visit } from '../../../src/visits/types';
2020-12-07 21:19:37 +03:00
describe('visitsOverviewReducer', () => {
2022-11-11 22:23:19 +03:00
const getVisitsOverview = jest.fn();
const buildApiClientMock = () => Mock.of<ShlinkApiClient>({ getVisitsOverview });
const loadVisitsOverview = loadVisitsOverviewCreator(buildApiClientMock);
const { reducer } = visitsOverviewReducerCreator(loadVisitsOverview);
beforeEach(jest.clearAllMocks);
2020-12-07 21:19:37 +03:00
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', () => {
2022-11-11 22:23:19 +03:00
const { loading } = reducer(
state({ loading: false, error: false }),
action(loadVisitsOverview.pending.toString()),
);
2020-12-07 21:19:37 +03:00
expect(loading).toEqual(true);
});
it('stops loading and returns error on GET_OVERVIEW_ERROR', () => {
2022-11-11 22:23:19 +03:00
const { loading, error } = reducer(
state({ loading: true, error: false }),
action(loadVisitsOverview.rejected.toString()),
);
2020-12-07 21:19:37 +03:00
expect(loading).toEqual(false);
expect(error).toEqual(true);
});
it('return visits overview on GET_OVERVIEW', () => {
2022-11-11 22:23:19 +03:00
const { loading, error, visitsCount } = reducer(state({ loading: true, error: false }), {
type: loadVisitsOverview.fulfilled.toString(),
payload: { visitsCount: 100 },
});
2020-12-07 21:19:37 +03:00
expect(loading).toEqual(false);
expect(error).toEqual(false);
expect(visitsCount).toEqual(100);
});
it.each([
2022-03-26 14:17:42 +03:00
[50, 53],
[0, 3],
[undefined, 3],
])('returns updated amounts on CREATE_VISITS', (providedOrphanVisitsCount, expectedOrphanVisitsCount) => {
const { visitsCount, orphanVisitsCount } = reducer(
state({ visitsCount: 100, orphanVisitsCount: providedOrphanVisitsCount }),
{
type: createNewVisits.toString(),
payload: {
createdVisits: [
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
],
},
} as unknown as GetVisitsOverviewAction & CreateVisitsAction,
);
expect(visitsCount).toEqual(102);
expect(orphanVisitsCount).toEqual(expectedOrphanVisitsCount);
});
2020-12-07 21:19:37 +03:00
});
describe('loadVisitsOverview', () => {
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>();
beforeEach(() => dispatchMock.mockReset());
it('dispatches start and error when promise is rejected', async () => {
2022-11-11 22:23:19 +03:00
getVisitsOverview.mockRejectedValue(undefined);
2020-12-07 21:19:37 +03:00
2022-11-11 22:23:19 +03:00
await loadVisitsOverview()(dispatchMock, getState, {});
2020-12-07 21:19:37 +03:00
expect(dispatchMock).toHaveBeenCalledTimes(2);
2022-11-11 22:23:19 +03:00
expect(dispatchMock).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: loadVisitsOverview.pending.toString(),
}));
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: loadVisitsOverview.rejected.toString(),
}));
expect(getVisitsOverview).toHaveBeenCalledTimes(1);
2020-12-07 21:19:37 +03:00
});
it('dispatches start and success when promise is resolved', async () => {
const resolvedOverview = Mock.of<ShlinkVisitsOverview>({ visitsCount: 50 });
2022-11-11 22:23:19 +03:00
getVisitsOverview.mockResolvedValue(resolvedOverview);
2020-12-07 21:19:37 +03:00
2022-11-11 22:23:19 +03:00
await loadVisitsOverview()(dispatchMock, getState, {});
2020-12-07 21:19:37 +03:00
expect(dispatchMock).toHaveBeenCalledTimes(2);
2022-11-11 22:23:19 +03:00
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);
2020-12-07 21:19:37 +03:00
});
});
});