From 22565eb253c6d7ae215802767c83514d703a0271 Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 21 Jun 2023 17:00:02 +0200 Subject: [PATCH] improve code coverage --- src/modules/ad/ad.mapper.ts | 1 - src/modules/ad/tests/unit/ad.mapper.spec.ts | 186 ++++++++++++++++++++ 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/modules/ad/tests/unit/ad.mapper.spec.ts diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index 2b98e79..5cc8ad0 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -28,7 +28,6 @@ import { DateTime, TimeZone } from 'timezonecomplete'; export class AdMapper implements Mapper { - private timezone: string; private readonly defaultParams: DefaultParams; constructor( diff --git a/src/modules/ad/tests/unit/ad.mapper.spec.ts b/src/modules/ad/tests/unit/ad.mapper.spec.ts new file mode 100644 index 0000000..f7e76ed --- /dev/null +++ b/src/modules/ad/tests/unit/ad.mapper.spec.ts @@ -0,0 +1,186 @@ +import { PARAMS_PROVIDER, TIMEZONE_FINDER } from '@modules/ad/ad.di-tokens'; +import { AdMapper } from '@modules/ad/ad.mapper'; +import { AdEntity } from '@modules/ad/core/ad.entity'; +import { Frequency } from '@modules/ad/core/ad.types'; +import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; +import { TimezoneFinderPort } from '@modules/ad/core/ports/timezone-finder.port'; +import { + AdReadModel, + AdWriteModel, +} from '@modules/ad/infrastructure/ad.repository'; +import { Test } from '@nestjs/testing'; + +const now = new Date('2023-06-21 06:00:00'); +const adEntity: AdEntity = new AdEntity({ + id: 'c160cf8c-f057-4962-841f-3ad68346df44', + props: { + userId: '7ca2490b-d04d-4ac5-8d6c-5c416fab922e', + driver: false, + passenger: true, + frequency: Frequency.PUNCTUAL, + fromDate: '2023-06-21', + toDate: '2023-06-21', + schedule: { + wed: '07:15', + }, + waypoints: [ + { + position: 0, + address: { + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + coordinates: { + lon: 48.68944505415954, + lat: 6.176510296462267, + }, + }, + }, + { + position: 1, + address: { + locality: 'Paris', + postalCode: '75000', + country: 'France', + coordinates: { + lon: 48.8566, + lat: 2.3522, + }, + }, + }, + ], + marginDurations: { + mon: 600, + tue: 600, + wed: 600, + thu: 600, + fri: 600, + sat: 600, + sun: 600, + }, + strict: false, + seatsProposed: 3, + seatsRequested: 1, + }, + createdAt: now, + updatedAt: now, +}); +const adReadModel: AdReadModel = { + uuid: 'c160cf8c-f057-4962-841f-3ad68346df44', + userUuid: '7ca2490b-d04d-4ac5-8d6c-5c416fab922e', + driver: false, + passenger: true, + frequency: Frequency.PUNCTUAL, + fromDate: new Date('2023-06-21'), + toDate: new Date('2023-06-21'), + monTime: undefined, + tueTime: undefined, + wedTime: new Date('2023-06-21T07:15:00Z'), + thuTime: undefined, + friTime: undefined, + satTime: undefined, + sunTime: undefined, + waypoints: [ + { + uuid: '6f53f55e-2bdb-4c23-b6a9-6d7b498e47b9', + position: 0, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + lon: 48.68944505415954, + lat: 6.176510296462267, + createdAt: now, + updatedAt: now, + }, + { + uuid: 'e18c6a84-0ab7-4e44-af1d-829d0b0d0573', + position: 1, + locality: 'Paris', + postalCode: '75000', + country: 'France', + lon: 48.8566, + lat: 2.3522, + createdAt: now, + updatedAt: now, + }, + ], + monMargin: 600, + tueMargin: 600, + wedMargin: 600, + thuMargin: 600, + friMargin: 600, + satMargin: 600, + sunMargin: 600, + strict: false, + seatsProposed: 3, + seatsRequested: 1, + createdAt: now, + updatedAt: now, +}; + +const mockDefaultParamsProvider: DefaultParamsProviderPort = { + getParams: () => { + return { + MON_MARGIN: 900, + TUE_MARGIN: 900, + WED_MARGIN: 900, + THU_MARGIN: 900, + FRI_MARGIN: 900, + SAT_MARGIN: 900, + SUN_MARGIN: 900, + DRIVER: false, + SEATS_PROPOSED: 3, + PASSENGER: true, + SEATS_REQUESTED: 1, + STRICT: false, + DEFAULT_TIMEZONE: 'Europe/Paris', + }; + }, +}; + +const mockTimezoneFinder: TimezoneFinderPort = { + timezones: jest.fn(), +}; + +describe('Ad Mapper', () => { + let adMapper: AdMapper; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + providers: [ + AdMapper, + { + provide: PARAMS_PROVIDER, + useValue: mockDefaultParamsProvider, + }, + { + provide: TIMEZONE_FINDER, + useValue: mockTimezoneFinder, + }, + ], + }).compile(); + adMapper = module.get(AdMapper); + }); + + it('should be defined', () => { + expect(adMapper).toBeDefined(); + }); + + it('should map domain entity to persistence data', async () => { + const mapped: AdWriteModel = adMapper.toPersistence(adEntity); + expect(mapped.waypoints.create[0].uuid.length).toBe(36); + expect(mapped.waypoints.create[1].uuid.length).toBe(36); + }); + + it('should map persisted data to domain entity', async () => { + const mapped: AdEntity = adMapper.toDomain(adReadModel); + expect(mapped.getProps().waypoints[0].address.coordinates.lon).toBe( + 48.68944505415954, + ); + expect(mapped.getProps().waypoints[1].address.coordinates.lat).toBe(2.3522); + }); +});