From 8e6b9c5afb82564bb16fcfc8ef02d03ca1d9903d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 12 Nov 2022 09:32:52 +0100 Subject: [PATCH] Updated getOrphanVisits action so that it expects a signle DTO param --- src/visits/OrphanVisits.tsx | 15 ++++++--------- src/visits/reducers/orphanVisits.ts | 10 ++++++---- test/visits/reducers/orphanVisits.test.ts | 6 +++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/visits/OrphanVisits.tsx b/src/visits/OrphanVisits.tsx index 14af9c31..b35ed6d3 100644 --- a/src/visits/OrphanVisits.tsx +++ b/src/visits/OrphanVisits.tsx @@ -1,21 +1,17 @@ import { boundToMercureHub } from '../mercure/helpers/boundToMercureHub'; -import { ShlinkVisitsParams } from '../api/types'; import { Topics } from '../mercure/helpers/Topics'; import { useGoBack } from '../utils/helpers/hooks'; import { ReportExporter } from '../common/services/ReportExporter'; import { VisitsStats } from './VisitsStats'; -import { NormalizedVisit, OrphanVisitType, VisitsParams } from './types'; +import { NormalizedVisit, VisitsParams } from './types'; import { CommonVisitsProps } from './types/CommonVisitsProps'; import { toApiParams } from './types/helpers'; import { VisitsHeader } from './VisitsHeader'; import { VisitsInfo } from './reducers/types'; +import { LoadOrphanVisits } from './reducers/orphanVisits'; export interface OrphanVisitsProps extends CommonVisitsProps { - getOrphanVisits: ( - params?: ShlinkVisitsParams, - orphanVisitsType?: OrphanVisitType, - doIntervalFallback?: boolean, - ) => void; + getOrphanVisits: (params: LoadOrphanVisits) => void; orphanVisits: VisitsInfo; cancelGetOrphanVisits: () => void; } @@ -29,8 +25,9 @@ export const OrphanVisits = ({ exportVisits }: ReportExporter) => boundToMercure }: OrphanVisitsProps) => { const goBack = useGoBack(); const exportCsv = (visits: NormalizedVisit[]) => exportVisits('orphan_visits.csv', visits); - const loadVisits = (params: VisitsParams, doIntervalFallback?: boolean) => - getOrphanVisits(toApiParams(params), params.filter?.orphanVisitsType, doIntervalFallback); + const loadVisits = (params: VisitsParams, doIntervalFallback?: boolean) => getOrphanVisits( + { query: toApiParams(params), orphanVisitsType: params.filter?.orphanVisitsType, doIntervalFallback }, + ); return ( { query?: ShlinkVisitsParams; } +export interface LoadOrphanVisits extends LoadVisits { + orphanVisitsType?: OrphanVisitType; +} + type OrphanVisitsCombinedAction = OrphanVisitsAction & VisitsLoadProgressChangedAction & VisitsFallbackIntervalAction @@ -68,9 +72,7 @@ const matchesType = (visit: OrphanVisit, orphanVisitsType?: OrphanVisitType) => !orphanVisitsType || orphanVisitsType === visit.type; export const getOrphanVisits = (buildShlinkApiClient: ShlinkApiClientBuilder) => ( - query: ShlinkVisitsParams = {}, - orphanVisitsType?: OrphanVisitType, - doIntervalFallback = false, + { orphanVisitsType, query = {}, doIntervalFallback = false }: LoadOrphanVisits, ) => async (dispatch: Dispatch, getState: GetState) => { const { getOrphanVisits: getVisits } = buildShlinkApiClient(getState); const visitsLoader = async (page: number, itemsPerPage: number) => getVisits({ ...query, page, itemsPerPage }) diff --git a/test/visits/reducers/orphanVisits.test.ts b/test/visits/reducers/orphanVisits.test.ts index 663d1e14..9ef91e74 100644 --- a/test/visits/reducers/orphanVisits.test.ts +++ b/test/visits/reducers/orphanVisits.test.ts @@ -147,7 +147,7 @@ describe('orphanVisitsReducer', () => { it('dispatches start and error when promise is rejected', async () => { const ShlinkApiClient = buildApiClientMock(Promise.reject({})); - await getOrphanVisits(() => ShlinkApiClient)()(dispatchMock, getState); + await getOrphanVisits(() => ShlinkApiClient)({})(dispatchMock, getState); expect(dispatchMock).toHaveBeenCalledTimes(2); expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_ORPHAN_VISITS_START }); @@ -169,7 +169,7 @@ describe('orphanVisitsReducer', () => { }, })); - await getOrphanVisits(() => ShlinkApiClient)(query)(dispatchMock, getState); + await getOrphanVisits(() => ShlinkApiClient)({ query })(dispatchMock, getState); expect(dispatchMock).toHaveBeenCalledTimes(2); expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_ORPHAN_VISITS_START }); @@ -201,7 +201,7 @@ describe('orphanVisitsReducer', () => { .mockResolvedValueOnce(buildVisitsResult(lastVisits)); const ShlinkApiClient = Mock.of({ getOrphanVisits: getShlinkOrphanVisits }); - await getOrphanVisits(() => ShlinkApiClient)({}, undefined, true)(dispatchMock, getState); + await getOrphanVisits(() => ShlinkApiClient)({ doIntervalFallback: true })(dispatchMock, getState); expect(dispatchMock).toHaveBeenCalledTimes(2); expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_ORPHAN_VISITS_START });