improve tests, added findAdById grpc controller test
This commit is contained in:
parent
f644d34068
commit
d294049a28
|
@ -118,7 +118,8 @@
|
||||||
"moduleNameMapper": {
|
"moduleNameMapper": {
|
||||||
"^@libs(.*)": "<rootDir>/libs/$1",
|
"^@libs(.*)": "<rootDir>/libs/$1",
|
||||||
"^@modules(.*)": "<rootDir>/modules/$1",
|
"^@modules(.*)": "<rootDir>/modules/$1",
|
||||||
"^@src(.*)": "<rootDir>$1"
|
"^@src(.*)": "<rootDir>$1",
|
||||||
|
"^@utils(.*)": "<rootDir>utils/$1"
|
||||||
},
|
},
|
||||||
"testEnvironment": "node"
|
"testEnvironment": "node"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import { Controller, UsePipes } from '@nestjs/common';
|
import { Controller, UsePipes } from '@nestjs/common';
|
||||||
import { CommandBus } from '@nestjs/cqrs';
|
import { CommandBus } from '@nestjs/cqrs';
|
||||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
||||||
import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe';
|
|
||||||
import { CreateAdRequestDto } from './dtos/create-ad.request.dto';
|
import { CreateAdRequestDto } from './dtos/create-ad.request.dto';
|
||||||
import { CreateAdCommand } from '../../core/commands/create-ad/create-ad.command';
|
import { CreateAdCommand } from '../../core/commands/create-ad/create-ad.command';
|
||||||
import { AggregateID } from '@libs/ddd';
|
import { AggregateID } from '@libs/ddd';
|
||||||
import { AdAlreadyExistsException } from '../../core/ad.errors';
|
import { AdAlreadyExistsException } from '../../core/ad.errors';
|
||||||
import { IdResponse } from '@libs/api/id.response.dto';
|
import { IdResponse } from '@libs/api/id.response.dto';
|
||||||
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
||||||
|
import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe';
|
||||||
|
|
||||||
@UsePipes(
|
@UsePipes(
|
||||||
new RpcValidationPipe({
|
new RpcValidationPipe({
|
||||||
|
@ -32,7 +32,10 @@ export class CreateAdGrpcController {
|
||||||
code: RpcExceptionCode.ALREADY_EXISTS,
|
code: RpcExceptionCode.ALREADY_EXISTS,
|
||||||
message: error.message,
|
message: error.message,
|
||||||
});
|
});
|
||||||
throw new RpcException({});
|
throw new RpcException({
|
||||||
|
code: RpcExceptionCode.UNKNOWN,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@ import { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad-
|
||||||
import { AdResponseDto } from '../dtos/ad.response.dto';
|
import { AdResponseDto } from '../dtos/ad.response.dto';
|
||||||
import { AdEntity } from '@modules/ad/core/ad.entity';
|
import { AdEntity } from '@modules/ad/core/ad.entity';
|
||||||
import { AdMapper } from '@modules/ad/ad.mapper';
|
import { AdMapper } from '@modules/ad/ad.mapper';
|
||||||
|
import { NotFoundException } from '@libs/exceptions';
|
||||||
|
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
||||||
|
|
||||||
@UsePipes(
|
@UsePipes(
|
||||||
new RpcValidationPipe({
|
new RpcValidationPipe({
|
||||||
|
@ -29,8 +31,14 @@ export class FindAdByIdGrpcController {
|
||||||
);
|
);
|
||||||
return this.mapper.toResponse(ad);
|
return this.mapper.toResponse(ad);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (e instanceof NotFoundException) {
|
||||||
|
throw new RpcException({
|
||||||
|
code: RpcExceptionCode.NOT_FOUND,
|
||||||
|
message: e.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
throw new RpcException({
|
throw new RpcException({
|
||||||
code: e.code,
|
code: RpcExceptionCode.UNKNOWN,
|
||||||
message: e.message,
|
message: e.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,35 +72,45 @@ describe('Create Ad Grpc Controller', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(createAdGrpcController).toBeDefined();
|
expect(createAdGrpcController).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a new ad', async () => {
|
it('should create a new ad', async () => {
|
||||||
|
jest.spyOn(mockCommandBus, 'execute');
|
||||||
const result: IdResponse = await createAdGrpcController.create(
|
const result: IdResponse = await createAdGrpcController.create(
|
||||||
punctualCreateAdRequest,
|
punctualCreateAdRequest,
|
||||||
);
|
);
|
||||||
expect(result).toBeInstanceOf(IdResponse);
|
expect(result).toBeInstanceOf(IdResponse);
|
||||||
expect(result.id).toBe('200d61a8-d878-4378-a609-c19ea71633d2');
|
expect(result.id).toBe('200d61a8-d878-4378-a609-c19ea71633d2');
|
||||||
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw an dedicated RpcException if ad already exists', async () => {
|
it('should throw a dedicated RpcException if ad already exists', async () => {
|
||||||
expect.assertions(2);
|
jest.spyOn(mockCommandBus, 'execute');
|
||||||
|
expect.assertions(3);
|
||||||
try {
|
try {
|
||||||
await createAdGrpcController.create(punctualCreateAdRequest);
|
await createAdGrpcController.create(punctualCreateAdRequest);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
expect(e).toBeInstanceOf(RpcException);
|
expect(e).toBeInstanceOf(RpcException);
|
||||||
expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS);
|
expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS);
|
||||||
}
|
}
|
||||||
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw a generic RpcException', async () => {
|
it('should throw a generic RpcException', async () => {
|
||||||
expect.assertions(2);
|
jest.spyOn(mockCommandBus, 'execute');
|
||||||
|
expect.assertions(3);
|
||||||
try {
|
try {
|
||||||
await createAdGrpcController.create(punctualCreateAdRequest);
|
await createAdGrpcController.create(punctualCreateAdRequest);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
expect(e).toBeInstanceOf(RpcException);
|
expect(e).toBeInstanceOf(RpcException);
|
||||||
expect(e.error.code).toBeUndefined();
|
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
|
||||||
}
|
}
|
||||||
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,140 @@
|
||||||
|
import { NotFoundException } from '@libs/exceptions';
|
||||||
|
import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum';
|
||||||
|
import { AdMapper } from '@modules/ad/ad.mapper';
|
||||||
|
import { Frequency } from '@modules/ad/core/ad.types';
|
||||||
|
import { FindAdByIdGrpcController } from '@modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller';
|
||||||
|
import { QueryBus } from '@nestjs/cqrs';
|
||||||
|
import { RpcException } from '@nestjs/microservices';
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
|
||||||
|
const mockQueryBus = {
|
||||||
|
execute: jest
|
||||||
|
.fn()
|
||||||
|
.mockImplementationOnce(() => '200d61a8-d878-4378-a609-c19ea71633d2')
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
throw new NotFoundException();
|
||||||
|
})
|
||||||
|
.mockImplementationOnce(() => {
|
||||||
|
throw new Error();
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockAdMapper = {
|
||||||
|
toResponse: jest.fn().mockImplementationOnce(() => ({
|
||||||
|
userId: '8cc90d1a-4a59-4289-a7d8-078f9db7857f',
|
||||||
|
driver: true,
|
||||||
|
passenger: true,
|
||||||
|
frequency: Frequency.PUNCTUAL,
|
||||||
|
fromDate: '2023-06-27',
|
||||||
|
toDate: '2023-06-27',
|
||||||
|
schedule: {
|
||||||
|
tue: '07:15',
|
||||||
|
},
|
||||||
|
marginDurations: {
|
||||||
|
mon: 900,
|
||||||
|
tue: 900,
|
||||||
|
wed: 900,
|
||||||
|
thu: 900,
|
||||||
|
fri: 900,
|
||||||
|
sat: 900,
|
||||||
|
sun: 900,
|
||||||
|
},
|
||||||
|
seatsProposed: 3,
|
||||||
|
seatsRequested: 1,
|
||||||
|
waypoints: [
|
||||||
|
{
|
||||||
|
position: 0,
|
||||||
|
lon: 48.689445,
|
||||||
|
lat: 6.17651,
|
||||||
|
houseNumber: '5',
|
||||||
|
street: 'Avenue Foch',
|
||||||
|
locality: 'Nancy',
|
||||||
|
postalCode: '54000',
|
||||||
|
country: 'France',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
position: 1,
|
||||||
|
lon: 48.8566,
|
||||||
|
lat: 2.3522,
|
||||||
|
locality: 'Paris',
|
||||||
|
postalCode: '75000',
|
||||||
|
country: 'France',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('Find Ad By Id Grpc Controller', () => {
|
||||||
|
let findAdbyIdGrpcController: FindAdByIdGrpcController;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
{
|
||||||
|
provide: QueryBus,
|
||||||
|
useValue: mockQueryBus,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: AdMapper,
|
||||||
|
useValue: mockAdMapper,
|
||||||
|
},
|
||||||
|
FindAdByIdGrpcController,
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
findAdbyIdGrpcController = module.get<FindAdByIdGrpcController>(
|
||||||
|
FindAdByIdGrpcController,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(findAdbyIdGrpcController).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an ad', async () => {
|
||||||
|
jest.spyOn(mockQueryBus, 'execute');
|
||||||
|
jest.spyOn(mockAdMapper, 'toResponse');
|
||||||
|
const response = await findAdbyIdGrpcController.findOnebyId({
|
||||||
|
id: '6dcf093c-c7db-4dae-8e9c-c715cebf83c7',
|
||||||
|
});
|
||||||
|
expect(response.userId).toBe('8cc90d1a-4a59-4289-a7d8-078f9db7857f');
|
||||||
|
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockAdMapper.toResponse).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw a dedicated RpcException if ad is not found', async () => {
|
||||||
|
jest.spyOn(mockQueryBus, 'execute');
|
||||||
|
jest.spyOn(mockAdMapper, 'toResponse');
|
||||||
|
expect.assertions(4);
|
||||||
|
try {
|
||||||
|
await findAdbyIdGrpcController.findOnebyId({
|
||||||
|
id: 'ac85f5f4-41cd-4c5d-9aee-0a1acb176fb8',
|
||||||
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
expect(e).toBeInstanceOf(RpcException);
|
||||||
|
expect(e.error.code).toBe(RpcExceptionCode.NOT_FOUND);
|
||||||
|
}
|
||||||
|
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockAdMapper.toResponse).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw a generic RpcException', async () => {
|
||||||
|
jest.spyOn(mockQueryBus, 'execute');
|
||||||
|
jest.spyOn(mockAdMapper, 'toResponse');
|
||||||
|
expect.assertions(4);
|
||||||
|
try {
|
||||||
|
await findAdbyIdGrpcController.findOnebyId({
|
||||||
|
id: '53c8e7ec-ef68-42bc-ba4c-5ef3effa60a6',
|
||||||
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
expect(e).toBeInstanceOf(RpcException);
|
||||||
|
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
|
||||||
|
}
|
||||||
|
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockAdMapper.toResponse).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue