mirror of
https://gitlab.com/mobicoop/v3/service/ad.git
synced 2026-02-23 09:00:45 +00:00
77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import { NotFoundException, RpcExceptionCode } from '@mobicoop/ddd-library';
|
|
import { UpdateAdGrpcController } from '@modules/ad/interface/grpc-controllers/update-ad.grpc.controller';
|
|
import { CommandBus } from '@nestjs/cqrs';
|
|
import { RpcException } from '@nestjs/microservices';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { punctualCreateAdRequest } from './ad.fixtures';
|
|
|
|
const validAdId = '200d61a8-d878-4378-a609-c19ea71633d2';
|
|
const mockCommandBus = {
|
|
execute: jest.fn().mockImplementation(async (command) => {
|
|
if (command.adId === '') throw 'Ad id is empty';
|
|
if (command.adId != validAdId) throw new NotFoundException();
|
|
}),
|
|
};
|
|
|
|
describe('Update Ad GRPC Controller', () => {
|
|
let updateAdGrpcController: UpdateAdGrpcController;
|
|
|
|
beforeAll(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
{
|
|
provide: CommandBus,
|
|
useValue: mockCommandBus,
|
|
},
|
|
UpdateAdGrpcController,
|
|
],
|
|
}).compile();
|
|
updateAdGrpcController = module.get<UpdateAdGrpcController>(
|
|
UpdateAdGrpcController,
|
|
);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(updateAdGrpcController).toBeDefined();
|
|
});
|
|
|
|
it('should execute the update ad command', async () => {
|
|
await updateAdGrpcController.update({
|
|
id: validAdId,
|
|
...punctualCreateAdRequest(),
|
|
});
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should throw a dedicated RpcException if ad is not found', async () => {
|
|
expect.assertions(3);
|
|
try {
|
|
await updateAdGrpcController.update({
|
|
id: 'ac85f5f4-41cd-4c5d-9aee-0a1acb176fb8',
|
|
...punctualCreateAdRequest(),
|
|
});
|
|
} catch (e: any) {
|
|
expect(e).toBeInstanceOf(RpcException);
|
|
expect(e.error.code).toBe(RpcExceptionCode.NOT_FOUND);
|
|
}
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should rethrow any other exceptions', async () => {
|
|
expect.assertions(2);
|
|
try {
|
|
await updateAdGrpcController.update({
|
|
id: '',
|
|
...punctualCreateAdRequest(),
|
|
});
|
|
} catch (e: any) {
|
|
expect(e).toBe('Ad id is empty');
|
|
}
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|