improve code coverage
This commit is contained in:
parent
65f9ed4172
commit
22565eb253
|
@ -28,7 +28,6 @@ import { DateTime, TimeZone } from 'timezonecomplete';
|
|||
export class AdMapper
|
||||
implements Mapper<AdEntity, AdReadModel, AdWriteModel, AdResponseDto>
|
||||
{
|
||||
private timezone: string;
|
||||
private readonly defaultParams: DefaultParams;
|
||||
|
||||
constructor(
|
||||
|
|
|
@ -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>(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);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue