diff --git a/test/visits/types/helpers.test.ts b/test/visits/types/helpers.test.ts index 77cca16f..e145fdc7 100644 --- a/test/visits/types/helpers.test.ts +++ b/test/visits/types/helpers.test.ts @@ -1,6 +1,8 @@ import { Mock } from 'ts-mockery'; -import { GroupedNewVisits, groupNewVisitsByType } from '../../../src/visits/types/helpers'; -import { CreateVisit, OrphanVisit, Visit } from '../../../src/visits/types'; +import { GroupedNewVisits, groupNewVisitsByType, toApiParams } from '../../../src/visits/types/helpers'; +import { CreateVisit, OrphanVisit, Visit, VisitsParams } from '../../../src/visits/types'; +import { ShlinkVisitsParams } from '../../../src/api/types'; +import { formatIsoDate, parseDate } from '../../../src/utils/helpers/date'; describe('visitsTypeHelpers', () => { describe('groupNewVisitsByType', () => { @@ -56,4 +58,51 @@ describe('visitsTypeHelpers', () => { expect(groupNewVisitsByType(createdVisits)).toEqual(expectedResult); }); }); + + describe('toApiParams', () => { + test.each([ + [ { page: 5, itemsPerPage: 100 } as VisitsParams, { page: 5, itemsPerPage: 100 } as ShlinkVisitsParams ], + [ + { + page: 1, + itemsPerPage: 30, + filter: { excludeBots: true }, + } as VisitsParams, + { page: 1, itemsPerPage: 30, excludeBots: true } as ShlinkVisitsParams, + ], + (() => { + const endDate = parseDate('2020-05-05', 'yyyy-MM-dd'); + + return [ + { + page: 20, + itemsPerPage: 1, + dateRange: { endDate }, + } as VisitsParams, + { page: 20, itemsPerPage: 1, endDate: formatIsoDate(endDate) } as ShlinkVisitsParams, + ]; + })(), + (() => { + const startDate = parseDate('2020-05-05', 'yyyy-MM-dd'); + const endDate = parseDate('2021-10-30', 'yyyy-MM-dd'); + + return [ + { + page: 20, + itemsPerPage: 1, + dateRange: { startDate, endDate }, + filter: { excludeBots: false }, + } as VisitsParams, + { + page: 20, + itemsPerPage: 1, + startDate: formatIsoDate(startDate), + endDate: formatIsoDate(endDate), + } as ShlinkVisitsParams, + ]; + })(), + ])('converts param as expected', (visitsParams, expectedResult) => { + expect(toApiParams(visitsParams)).toEqual(expectedResult); + }); + }); });