matcher/src/modules/ad/tests/unit/interface/match.grpc.controller.spec.ts

110 lines
2.8 KiB
TypeScript

import { RpcExceptionCode } from '@mobicoop/ddd-library';
import { AlgorithmType } from '@modules/ad/core/application/types/algorithm.types';
import { Frequency } from '@modules/ad/core/domain/ad.types';
import { MatchRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/match.request.dto';
import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto';
import { MatchGrpcController } from '@modules/ad/interface/grpc-controllers/match.grpc-controller';
import { QueryBus } from '@nestjs/cqrs';
import { RpcException } from '@nestjs/microservices';
import { Test, TestingModule } from '@nestjs/testing';
const originWaypoint: WaypointDto = {
position: 0,
lat: 48.689445,
lon: 6.17651,
houseNumber: '5',
street: 'Avenue Foch',
locality: 'Nancy',
postalCode: '54000',
country: 'France',
};
const destinationWaypoint: WaypointDto = {
position: 1,
lat: 48.8566,
lon: 2.3522,
locality: 'Paris',
postalCode: '75000',
country: 'France',
};
const punctualMatchRequestDto: MatchRequestDto = {
driver: false,
passenger: true,
frequency: Frequency.PUNCTUAL,
fromDate: '2023-08-15',
toDate: '2023-08-15',
schedule: [
{
time: '07:00',
day: 2,
margin: 900,
},
],
waypoints: [originWaypoint, destinationWaypoint],
strict: false,
algorithmType: AlgorithmType.PASSENGER_ORIENTED,
};
const mockQueryBus = {
execute: jest
.fn()
.mockImplementationOnce(() => [
{
adId: 1,
},
{
adId: 2,
},
])
.mockImplementationOnce(() => {
throw new Error();
}),
};
describe('Match Grpc Controller', () => {
let matchGrpcController: MatchGrpcController;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
MatchGrpcController,
{
provide: QueryBus,
useValue: mockQueryBus,
},
],
}).compile();
matchGrpcController = module.get<MatchGrpcController>(MatchGrpcController);
});
afterEach(async () => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(matchGrpcController).toBeDefined();
});
it('should return matches', async () => {
jest.spyOn(mockQueryBus, 'execute');
const matchPaginatedResponseDto = await matchGrpcController.match(
punctualMatchRequestDto,
);
expect(matchPaginatedResponseDto.data).toHaveLength(2);
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
});
it('should throw a generic RpcException', async () => {
jest.spyOn(mockQueryBus, 'execute');
expect.assertions(3);
try {
await matchGrpcController.match(punctualMatchRequestDto);
} catch (e: any) {
expect(e).toBeInstanceOf(RpcException);
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
}
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
});
});