get route controller tests

This commit is contained in:
sbriat 2023-08-22 11:58:31 +02:00
parent 14d2486c1b
commit 52a1a815e6
2 changed files with 75 additions and 9 deletions

View File

@ -3,7 +3,7 @@ import { RouteResponseDto } from '../dtos/route.response.dto';
import { GetRouteRequestDto } from './dtos/get-route.request.dto';
import { RouteEntity } from '@modules/geography/core/domain/route.entity';
import { GetRouteQuery } from '@modules/geography/core/application/queries/get-route/get-route.query';
import { RouteMapper } from '@modules/geography/geography.mapper';
import { RouteMapper } from '@modules/geography/route.mapper';
import { Injectable } from '@nestjs/common';
@Injectable()
@ -14,13 +14,9 @@ export class GetRouteController {
) {}
async get(data: GetRouteRequestDto): Promise<RouteResponseDto> {
try {
const route: RouteEntity = await this.queryBus.execute(
new GetRouteQuery(data.roles, data.waypoints, data.georouterSettings),
);
return this.mapper.toResponse(route);
} catch (e) {
throw e;
}
const route: RouteEntity = await this.queryBus.execute(
new GetRouteQuery(data.roles, data.waypoints, data.georouterSettings),
);
return this.mapper.toResponse(route);
}
}

View File

@ -0,0 +1,70 @@
import { Role } from '@modules/geography/core/domain/route.types';
import { GetRouteController } from '@modules/geography/interface/controllers/get-route.controller';
import { RouteMapper } from '@modules/geography/route.mapper';
import { QueryBus } from '@nestjs/cqrs';
import { Test, TestingModule } from '@nestjs/testing';
const mockQueryBus = {
execute: jest.fn(),
};
const mockRouteMapper = {
toPersistence: jest.fn(),
toDomain: jest.fn(),
toResponse: jest.fn(),
};
describe('Get Route Controller', () => {
let getRouteController: GetRouteController;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{
provide: QueryBus,
useValue: mockQueryBus,
},
{
provide: RouteMapper,
useValue: mockRouteMapper,
},
GetRouteController,
],
}).compile();
getRouteController = module.get<GetRouteController>(GetRouteController);
});
afterEach(async () => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(getRouteController).toBeDefined();
});
it('should get a route', async () => {
jest.spyOn(mockQueryBus, 'execute');
await getRouteController.get({
roles: [Role.DRIVER],
waypoints: [
{
position: 0,
lon: 48.689445,
lat: 6.17651,
},
{
position: 1,
lon: 48.8566,
lat: 2.3522,
},
],
georouterSettings: {
points: true,
detailedDistance: false,
detailedDuration: false,
},
});
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
});
});