From 52a1a815e6478e9c22c996cc79e8b032b2587dec Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 22 Aug 2023 11:58:31 +0200 Subject: [PATCH] get route controller tests --- .../controllers/get-route.controller.ts | 14 ++-- .../interface/get-route.controller.spec.ts | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 src/modules/geography/tests/unit/interface/get-route.controller.spec.ts diff --git a/src/modules/geography/interface/controllers/get-route.controller.ts b/src/modules/geography/interface/controllers/get-route.controller.ts index 8fdd2f9..0f16766 100644 --- a/src/modules/geography/interface/controllers/get-route.controller.ts +++ b/src/modules/geography/interface/controllers/get-route.controller.ts @@ -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 { - 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); } } diff --git a/src/modules/geography/tests/unit/interface/get-route.controller.spec.ts b/src/modules/geography/tests/unit/interface/get-route.controller.spec.ts new file mode 100644 index 0000000..6b0ed30 --- /dev/null +++ b/src/modules/geography/tests/unit/interface/get-route.controller.spec.ts @@ -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); + }); + + 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); + }); +});