117 lines
3.4 KiB
TypeScript
117 lines
3.4 KiB
TypeScript
import { IdResponse } from '@mobicoop/ddd-library';
|
|
import { RpcExceptionCode } from '@mobicoop/ddd-library';
|
|
import { AdAlreadyExistsException } from '@modules/ad/core/domain/ad.errors';
|
|
import { Frequency } from '@modules/ad/core/domain/ad.types';
|
|
import { CreateAdGrpcController } from '@modules/ad/interface/grpc-controllers/create-ad.grpc.controller';
|
|
import { CreateAdRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto';
|
|
import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto';
|
|
import { CommandBus } from '@nestjs/cqrs';
|
|
import { RpcException } from '@nestjs/microservices';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
const originWaypoint: WaypointDto = {
|
|
position: 0,
|
|
lon: 48.689445,
|
|
lat: 6.17651,
|
|
houseNumber: '5',
|
|
street: 'Avenue Foch',
|
|
locality: 'Nancy',
|
|
postalCode: '54000',
|
|
country: 'France',
|
|
};
|
|
const destinationWaypoint: WaypointDto = {
|
|
position: 1,
|
|
lon: 48.8566,
|
|
lat: 2.3522,
|
|
locality: 'Paris',
|
|
postalCode: '75000',
|
|
country: 'France',
|
|
};
|
|
const punctualCreateAdRequest: CreateAdRequestDto = {
|
|
userId: '4eb6a6af-ecfd-41c3-9118-473a507014d4',
|
|
fromDate: '2023-12-21',
|
|
toDate: '2023-12-21',
|
|
schedule: {
|
|
thu: '08:15',
|
|
},
|
|
driver: false,
|
|
passenger: true,
|
|
seatsRequested: 1,
|
|
frequency: Frequency.PUNCTUAL,
|
|
waypoints: [originWaypoint, destinationWaypoint],
|
|
};
|
|
|
|
const mockCommandBus = {
|
|
execute: jest
|
|
.fn()
|
|
.mockImplementationOnce(() => '200d61a8-d878-4378-a609-c19ea71633d2')
|
|
.mockImplementationOnce(() => {
|
|
throw new AdAlreadyExistsException();
|
|
})
|
|
.mockImplementationOnce(() => {
|
|
throw new Error();
|
|
}),
|
|
};
|
|
|
|
describe('Create Ad Grpc Controller', () => {
|
|
let createAdGrpcController: CreateAdGrpcController;
|
|
|
|
beforeAll(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
{
|
|
provide: CommandBus,
|
|
useValue: mockCommandBus,
|
|
},
|
|
CreateAdGrpcController,
|
|
],
|
|
}).compile();
|
|
|
|
createAdGrpcController = module.get<CreateAdGrpcController>(
|
|
CreateAdGrpcController,
|
|
);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(createAdGrpcController).toBeDefined();
|
|
});
|
|
|
|
it('should create a new ad', async () => {
|
|
jest.spyOn(mockCommandBus, 'execute');
|
|
const result: IdResponse = await createAdGrpcController.create(
|
|
punctualCreateAdRequest,
|
|
);
|
|
expect(result).toBeInstanceOf(IdResponse);
|
|
expect(result.id).toBe('200d61a8-d878-4378-a609-c19ea71633d2');
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should throw a dedicated RpcException if ad already exists', async () => {
|
|
jest.spyOn(mockCommandBus, 'execute');
|
|
expect.assertions(3);
|
|
try {
|
|
await createAdGrpcController.create(punctualCreateAdRequest);
|
|
} catch (e: any) {
|
|
expect(e).toBeInstanceOf(RpcException);
|
|
expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS);
|
|
}
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should throw a generic RpcException', async () => {
|
|
jest.spyOn(mockCommandBus, 'execute');
|
|
expect.assertions(3);
|
|
try {
|
|
await createAdGrpcController.create(punctualCreateAdRequest);
|
|
} catch (e: any) {
|
|
expect(e).toBeInstanceOf(RpcException);
|
|
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
|
|
}
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|