2023-04-14 10:28:53 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2023-08-02 10:01:44 +03:00
|
|
|
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
|
|
|
|
import type { ShlinkState } from '../../../../src/container/types';
|
|
|
|
import type { ShlinkVisitsOverview } from '../../../src/api/types';
|
|
|
|
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type {
|
2023-03-18 14:35:33 +03:00
|
|
|
PartialVisitsSummary,
|
|
|
|
VisitsOverview,
|
2023-08-02 10:01:44 +03:00
|
|
|
} from '../../../src/visits/reducers/visitsOverview';
|
2023-02-18 12:40:37 +03:00
|
|
|
import {
|
2022-11-11 22:23:19 +03:00
|
|
|
loadVisitsOverview as loadVisitsOverviewCreator,
|
|
|
|
visitsOverviewReducerCreator,
|
2023-08-02 10:01:44 +03:00
|
|
|
} from '../../../src/visits/reducers/visitsOverview';
|
|
|
|
import type { OrphanVisit } from '../../../src/visits/types';
|
2020-12-07 21:19:37 +03:00
|
|
|
|
2021-02-27 22:03:51 +03:00
|
|
|
describe('visitsOverviewReducer', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const getVisitsOverview = vi.fn();
|
2023-04-14 10:28:53 +03:00
|
|
|
const buildApiClientMock = () => fromPartial<ShlinkApiClient>({ getVisitsOverview });
|
2022-11-11 22:23:19 +03:00
|
|
|
const loadVisitsOverview = loadVisitsOverviewCreator(buildApiClientMock);
|
|
|
|
const { reducer } = visitsOverviewReducerCreator(loadVisitsOverview);
|
|
|
|
|
2020-12-07 21:19:37 +03:00
|
|
|
describe('reducer', () => {
|
2023-04-14 10:28:53 +03:00
|
|
|
const state = (payload: Partial<VisitsOverview> = {}) => fromPartial<VisitsOverview>(payload);
|
2020-12-07 21:19:37 +03:00
|
|
|
|
|
|
|
it('returns loading on GET_OVERVIEW_START', () => {
|
2022-11-11 22:23:19 +03:00
|
|
|
const { loading } = reducer(
|
|
|
|
state({ loading: false, error: false }),
|
2023-03-18 14:35:33 +03:00
|
|
|
loadVisitsOverview.pending(''),
|
2022-11-11 22:23:19 +03:00
|
|
|
);
|
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 }),
|
2023-03-18 14:35:33 +03:00
|
|
|
loadVisitsOverview.rejected(null, ''),
|
2022-11-11 22:23:19 +03:00
|
|
|
);
|
2020-12-07 21:19:37 +03:00
|
|
|
|
|
|
|
expect(loading).toEqual(false);
|
|
|
|
expect(error).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('return visits overview on GET_OVERVIEW', () => {
|
2023-04-14 10:28:53 +03:00
|
|
|
const action = loadVisitsOverview.fulfilled(fromPartial({
|
2023-03-18 12:54:14 +03:00
|
|
|
nonOrphanVisits: { total: 100 },
|
|
|
|
}), 'requestId');
|
|
|
|
const { loading, error, nonOrphanVisits } = reducer(state({ loading: true, error: false }), action);
|
2020-12-07 21:19:37 +03:00
|
|
|
|
|
|
|
expect(loading).toEqual(false);
|
|
|
|
expect(error).toEqual(false);
|
2023-03-18 12:54:14 +03:00
|
|
|
expect(nonOrphanVisits.total).toEqual(100);
|
2020-12-07 21:19:37 +03:00
|
|
|
});
|
2021-02-27 22:03:51 +03:00
|
|
|
|
2021-09-20 22:51:51 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[50, 53],
|
|
|
|
[0, 3],
|
2021-09-20 22:51:51 +03:00
|
|
|
])('returns updated amounts on CREATE_VISITS', (providedOrphanVisitsCount, expectedOrphanVisitsCount) => {
|
2023-03-18 12:54:14 +03:00
|
|
|
const { nonOrphanVisits, orphanVisits } = reducer(
|
|
|
|
state({
|
|
|
|
nonOrphanVisits: { total: 100 },
|
|
|
|
orphanVisits: { total: providedOrphanVisitsCount },
|
|
|
|
}),
|
|
|
|
createNewVisits([
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({ visit: {} }),
|
|
|
|
fromPartial({ visit: {} }),
|
|
|
|
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
|
|
|
|
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
|
|
|
|
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
|
2023-03-18 12:54:14 +03:00
|
|
|
]),
|
2021-02-27 22:03:51 +03:00
|
|
|
);
|
|
|
|
|
2023-03-18 12:54:14 +03:00
|
|
|
expect(nonOrphanVisits.total).toEqual(102);
|
|
|
|
expect(orphanVisits.total).toEqual(expectedOrphanVisitsCount);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
[
|
|
|
|
{} satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{} satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{ total: 103 } satisfies PartialVisitsSummary,
|
|
|
|
{ total: 203 } satisfies PartialVisitsSummary,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ bots: 35 } satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{ bots: 35 } satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{ total: 103, bots: 37 } satisfies PartialVisitsSummary,
|
|
|
|
{ total: 203, bots: 36 } satisfies PartialVisitsSummary,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ nonBots: 41, bots: 85 } satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{ nonBots: 63, bots: 27 } satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{ total: 103, nonBots: 42, bots: 87 } satisfies PartialVisitsSummary,
|
|
|
|
{ total: 203, nonBots: 65, bots: 28 } satisfies PartialVisitsSummary,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ nonBots: 56 } satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{ nonBots: 99 } satisfies Omit<PartialVisitsSummary, 'total'>,
|
|
|
|
{ total: 103, nonBots: 57 } satisfies PartialVisitsSummary,
|
|
|
|
{ total: 203, nonBots: 101 } satisfies PartialVisitsSummary,
|
|
|
|
],
|
|
|
|
])('takes bots and non-bots into consideration when creating visits', (
|
|
|
|
initialNonOrphanVisits,
|
|
|
|
initialOrphanVisits,
|
|
|
|
expectedNonOrphanVisits,
|
|
|
|
expectedOrphanVisits,
|
|
|
|
) => {
|
|
|
|
const { nonOrphanVisits, orphanVisits } = reducer(
|
|
|
|
state({
|
|
|
|
nonOrphanVisits: { total: 100, ...initialNonOrphanVisits },
|
|
|
|
orphanVisits: { total: 200, ...initialOrphanVisits },
|
|
|
|
}),
|
|
|
|
createNewVisits([
|
2023-04-14 10:28:53 +03:00
|
|
|
fromPartial({ visit: {} }),
|
|
|
|
fromPartial({ visit: { potentialBot: true } }),
|
|
|
|
fromPartial({ visit: { potentialBot: true } }),
|
|
|
|
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
|
|
|
|
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
|
|
|
|
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '', potentialBot: true }) }),
|
2023-03-18 12:54:14 +03:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(nonOrphanVisits).toEqual(expectedNonOrphanVisits);
|
|
|
|
expect(orphanVisits).toEqual(expectedOrphanVisits);
|
2021-02-27 22:03:51 +03:00
|
|
|
});
|
2020-12-07 21:19:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('loadVisitsOverview', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const dispatchMock = vi.fn();
|
2023-04-14 10:28:53 +03:00
|
|
|
const getState = () => fromPartial<ShlinkState>({});
|
2020-12-07 21:19:37 +03:00
|
|
|
|
2023-03-18 12:54:14 +03:00
|
|
|
it.each([
|
|
|
|
[
|
|
|
|
// Shlink <3.5.0
|
|
|
|
{ visitsCount: 50, orphanVisitsCount: 20 } satisfies ShlinkVisitsOverview,
|
|
|
|
{
|
|
|
|
nonOrphanVisits: { total: 50, nonBots: undefined, bots: undefined },
|
|
|
|
orphanVisits: { total: 20, nonBots: undefined, bots: undefined },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
// Shlink >=3.5.0
|
|
|
|
{
|
|
|
|
nonOrphanVisits: { total: 50, nonBots: 20, bots: 30 },
|
|
|
|
orphanVisits: { total: 50, nonBots: 20, bots: 30 },
|
|
|
|
visitsCount: 3,
|
|
|
|
orphanVisitsCount: 3,
|
|
|
|
} satisfies ShlinkVisitsOverview,
|
|
|
|
{
|
|
|
|
nonOrphanVisits: { total: 50, nonBots: 20, bots: 30 },
|
|
|
|
orphanVisits: { total: 50, nonBots: 20, bots: 30 },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
])('dispatches start and success when promise is resolved', async (serverResult, dispatchedPayload) => {
|
2023-04-14 10:28:53 +03:00
|
|
|
const resolvedOverview = fromPartial<ShlinkVisitsOverview>(serverResult);
|
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);
|
2023-03-18 14:35:33 +03:00
|
|
|
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({ payload: dispatchedPayload }));
|
2022-11-11 22:23:19 +03:00
|
|
|
expect(getVisitsOverview).toHaveBeenCalledTimes(1);
|
2020-12-07 21:19:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|