matcher/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts

177 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-05-02 15:26:04 +00:00
import { CreateAdRequest } from '../../../domain/dtos/create-ad.request';
import { CreateAdUseCase } from '../../../domain/usecases/create-ad.usecase';
import { Test, TestingModule } from '@nestjs/testing';
import { AutomapperModule } from '@automapper/nestjs';
import { classes } from '@automapper/classes';
import { AdRepository } from '../../../adapters/secondaries/ad.repository';
import { CreateAdCommand } from '../../../commands/create-ad.command';
import { Ad } from '../../../domain/entities/ad';
import { AdProfile } from '../../../mappers/ad.profile';
2023-05-11 15:47:55 +00:00
import { Frequency } from '../../../domain/types/frequency.enum';
2023-05-12 14:23:42 +00:00
import { RouteKey } from '../../../domain/entities/geography';
2023-05-22 14:38:34 +00:00
import { DatabaseException } from '../../../../database/exceptions/database.exception';
import { Route } from '../../../../geography/domain/entities/route';
2023-05-02 15:26:04 +00:00
2023-05-22 14:38:34 +00:00
const mockAdRepository = {
createAd: jest.fn().mockImplementation((ad) => {
if (ad.uuid == '00000000-0000-0000-0000-000000000000')
throw new DatabaseException();
return new Ad();
}),
};
2023-05-11 15:47:55 +00:00
const mockGeorouterCreator = {
2023-05-12 14:23:42 +00:00
create: jest.fn().mockImplementation(() => ({
route: jest.fn().mockImplementation(() => [
2023-05-22 14:38:34 +00:00
{
key: RouteKey.DRIVER,
route: <Route>{
points: [],
fwdAzimuth: 0,
backAzimuth: 180,
distance: 20000,
duration: 1800,
},
},
{
key: RouteKey.PASSENGER,
route: <Route>{
points: [],
fwdAzimuth: 0,
backAzimuth: 180,
distance: 20000,
duration: 1800,
},
},
2023-05-12 14:23:42 +00:00
{
key: RouteKey.COMMON,
2023-05-22 14:38:34 +00:00
route: <Route>{
points: [],
fwdAzimuth: 0,
backAzimuth: 180,
distance: 20000,
duration: 1800,
},
2023-05-12 14:23:42 +00:00
},
]),
})),
2023-05-11 15:47:55 +00:00
};
2023-05-12 14:23:42 +00:00
const mockParamsProvider = {
getParams: jest.fn().mockImplementation(() => ({
DEFAULT_TIMEZONE: 'Europe/Paris',
GEOROUTER_TYPE: 'graphhopper',
GEOROUTER_URL: 'localhost',
})),
2023-05-11 15:47:55 +00:00
};
2023-05-12 14:23:42 +00:00
const mockTimezoneFinder = {
timezones: jest.fn().mockImplementation(() => ['Europe/Paris']),
};
2023-05-22 14:38:34 +00:00
const mockDirectionEncoder = {
encode: jest.fn(),
};
2023-05-11 15:47:55 +00:00
2023-05-02 15:26:04 +00:00
const createAdRequest: CreateAdRequest = {
uuid: '77c55dfc-c28b-4026-942e-f94e95401fb1',
2023-05-11 15:47:55 +00:00
userUuid: 'dfd993f6-7889-4876-9570-5e1d7b6e3f42',
2023-05-02 15:26:04 +00:00
driver: true,
passenger: false,
2023-05-11 15:47:55 +00:00
frequency: Frequency.RECURRENT,
fromDate: new Date('2023-04-26'),
toDate: new Date('2024-04-25'),
2023-05-02 15:26:04 +00:00
monTime: '07:00',
tueTime: '07:00',
wedTime: '07:00',
thuTime: '07:00',
friTime: '07:00',
satTime: null,
sunTime: null,
monMargin: 900,
tueMargin: 900,
wedMargin: 900,
thuMargin: 900,
friMargin: 900,
satMargin: 900,
sunMargin: 900,
seatsDriver: 3,
seatsPassenger: 1,
2023-05-11 15:47:55 +00:00
strict: false,
2023-05-02 15:26:04 +00:00
waypoints: [
{ lon: 6, lat: 45 },
{ lon: 6.5, lat: 45.5 },
],
};
2023-05-22 14:38:34 +00:00
const setUuid = async (uuid: string): Promise<void> => {
createAdRequest.uuid = uuid;
};
const setIsDriver = async (isDriver: boolean): Promise<void> => {
createAdRequest.driver = isDriver;
};
const setIsPassenger = async (isPassenger: boolean): Promise<void> => {
createAdRequest.passenger = isPassenger;
};
2023-05-02 15:26:04 +00:00
describe('CreateAdUseCase', () => {
let createAdUseCase: CreateAdUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })],
providers: [
{
provide: AdRepository,
useValue: mockAdRepository,
},
2023-05-12 14:23:42 +00:00
{
provide: 'GeorouterCreator',
useValue: mockGeorouterCreator,
},
{
provide: 'ParamsProvider',
useValue: mockParamsProvider,
},
{
provide: 'TimezoneFinder',
useValue: mockTimezoneFinder,
},
{
provide: 'DirectionEncoder',
useValue: mockDirectionEncoder,
},
2023-05-02 15:26:04 +00:00
AdProfile,
CreateAdUseCase,
],
}).compile();
createAdUseCase = module.get<CreateAdUseCase>(CreateAdUseCase);
});
it('should be defined', () => {
expect(createAdUseCase).toBeDefined();
});
2023-05-22 14:38:34 +00:00
describe('execute', () => {
it('should create an ad as driver', async () => {
const ad = await createAdUseCase.execute(
new CreateAdCommand(createAdRequest),
);
expect(ad).toBeInstanceOf(Ad);
});
it('should create an ad as passenger', async () => {
await setIsDriver(false);
await setIsPassenger(true);
const ad = await createAdUseCase.execute(
new CreateAdCommand(createAdRequest),
);
expect(ad).toBeInstanceOf(Ad);
});
it('should throw an exception if repository fails', async () => {
await setUuid('00000000-0000-0000-0000-000000000000');
await expect(
createAdUseCase.execute(new CreateAdCommand(createAdRequest)),
).rejects.toBeInstanceOf(DatabaseException);
});
});
2023-05-02 15:26:04 +00:00
});