123 lines
3.9 KiB
TypeScript
123 lines
3.9 KiB
TypeScript
import { IdResponse } from '@mobicoop/ddd-library';
|
|
import { RpcExceptionCode } from '@mobicoop/ddd-library';
|
|
import { CommandBus } from '@nestjs/cqrs';
|
|
import { RpcException } from '@nestjs/microservices';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import {
|
|
EmailAlreadyExistsException,
|
|
PhoneAlreadyExistsException,
|
|
UserAlreadyExistsException,
|
|
} from '@modules/user/core/domain/user.errors';
|
|
import { CreateUserGrpcController } from '@modules/user/interface/grpc-controllers/create-user.grpc.controller';
|
|
import { CreateUserRequestDto } from '@modules/user/interface/grpc-controllers/dtos/create-user.request.dto';
|
|
|
|
const createUserRequest: CreateUserRequestDto = {
|
|
firstName: 'John',
|
|
lastName: 'Doe',
|
|
email: 'john.doe@email.com',
|
|
phone: '+33611223344',
|
|
};
|
|
|
|
const mockCommandBus = {
|
|
execute: jest
|
|
.fn()
|
|
.mockImplementationOnce(() => '200d61a8-d878-4378-a609-c19ea71633d2')
|
|
.mockImplementationOnce(() => {
|
|
throw new UserAlreadyExistsException();
|
|
})
|
|
.mockImplementationOnce(() => {
|
|
throw new EmailAlreadyExistsException();
|
|
})
|
|
.mockImplementationOnce(() => {
|
|
throw new PhoneAlreadyExistsException();
|
|
})
|
|
.mockImplementationOnce(() => {
|
|
throw new Error();
|
|
}),
|
|
};
|
|
|
|
describe('Create User Grpc Controller', () => {
|
|
let createUserGrpcController: CreateUserGrpcController;
|
|
|
|
beforeAll(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
{
|
|
provide: CommandBus,
|
|
useValue: mockCommandBus,
|
|
},
|
|
CreateUserGrpcController,
|
|
],
|
|
}).compile();
|
|
|
|
createUserGrpcController = module.get<CreateUserGrpcController>(
|
|
CreateUserGrpcController,
|
|
);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(createUserGrpcController).toBeDefined();
|
|
});
|
|
|
|
it('should create a new user', async () => {
|
|
jest.spyOn(mockCommandBus, 'execute');
|
|
const result: IdResponse =
|
|
await createUserGrpcController.create(createUserRequest);
|
|
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 user already exists', async () => {
|
|
jest.spyOn(mockCommandBus, 'execute');
|
|
expect.assertions(3);
|
|
try {
|
|
await createUserGrpcController.create(createUserRequest);
|
|
} catch (e: any) {
|
|
expect(e).toBeInstanceOf(RpcException);
|
|
expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS);
|
|
}
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should throw a dedicated RpcException if email already exists', async () => {
|
|
jest.spyOn(mockCommandBus, 'execute');
|
|
expect.assertions(3);
|
|
try {
|
|
await createUserGrpcController.create(createUserRequest);
|
|
} catch (e: any) {
|
|
expect(e).toBeInstanceOf(RpcException);
|
|
expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS);
|
|
}
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should throw a dedicated RpcException if phone already exists', async () => {
|
|
jest.spyOn(mockCommandBus, 'execute');
|
|
expect.assertions(3);
|
|
try {
|
|
await createUserGrpcController.create(createUserRequest);
|
|
} 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 createUserGrpcController.create(createUserRequest);
|
|
} catch (e: any) {
|
|
expect(e).toBeInstanceOf(RpcException);
|
|
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
|
|
}
|
|
expect(mockCommandBus.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|