add tests

This commit is contained in:
Gsk54 2022-12-14 11:53:50 +01:00
parent c2ae623e72
commit 95555c476b
5 changed files with 142 additions and 10 deletions

View File

@ -1,12 +1,6 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import {
BadRequestException,
Body,
ConflictException,
Controller,
NotFoundException,
} from '@nestjs/common';
import { Controller } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { DatabaseException } from 'src/modules/database/src/exceptions/DatabaseException';

View File

@ -1,3 +1,20 @@
import { CreateUserRequest } from './create-user.request';
import { AutoMap } from '@automapper/classes';
import { IsString } from 'class-validator';
export class UpdateUserRequest extends CreateUserRequest {}
export class UpdateUserRequest {
@IsString()
@AutoMap()
uuid: string;
@IsString()
@AutoMap()
firstName?: string;
@IsString()
@AutoMap()
lastName?: string;
@IsString()
@AutoMap()
email?: string;
}

View File

@ -0,0 +1,57 @@
import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { CreateUserCommand } from '../../commands/create-user.command';
import { CreateUserRequest } from '../../domain/dto/create-user.request';
import { User } from '../../domain/entities/user';
import { CreateUserUseCase } from '../../domain/usecases/create-user.usecase';
import { UserProfile } from '../../mappers/user.profile';
const newUserRequest: CreateUserRequest = {
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@email.com',
};
const newUserCommand: CreateUserCommand = new CreateUserCommand(newUserRequest);
const mockUsersRepository = {
create: jest.fn().mockResolvedValue({
...newUserRequest,
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
}),
};
describe('CreateUserUseCase', () => {
let createUserUseCase: CreateUserUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })],
providers: [
{
provide: UsersRepository,
useValue: mockUsersRepository,
},
CreateUserUseCase,
UserProfile,
],
}).compile();
createUserUseCase = module.get<CreateUserUseCase>(CreateUserUseCase);
});
it('should be defined', () => {
expect(createUserUseCase).toBeDefined();
});
describe('execute', () => {
it('should create an User and returns new entity object', async () => {
const newUser: User = await createUserUseCase.execute(newUserCommand);
expect(newUser.lastName).toBe(newUserRequest.lastName);
expect(newUser.uuid).toBeDefined();
});
});
});

View File

@ -28,7 +28,6 @@ describe('FindUserByUuidUseCase', () => {
provide: UsersRepository,
useValue: mockUserRepository,
},
FindUserByUuidUseCase,
],
}).compile();

View File

@ -0,0 +1,65 @@
import { classes } from '@automapper/classes';
import { AutomapperModule } from '@automapper/nestjs';
import { Test, TestingModule } from '@nestjs/testing';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
import { UpdateUserCommand } from '../../commands/update-user.command';
import { UpdateUserRequest } from '../../domain/dto/update-user.request';
import { User } from '../../domain/entities/user';
import { UpdateUserUseCase } from '../../domain/usecases/update-user.usecase';
import { UserProfile } from '../../mappers/user.profile';
const originalUser: User = new User();
originalUser.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
originalUser.lastName = 'Doe';
const updateUserRequest: UpdateUserRequest = {
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
lastName: 'Dane',
};
const updateUserCommand: UpdateUserCommand = new UpdateUserCommand(
updateUserRequest,
);
const mockUsersRepository = {
update: jest.fn().mockImplementation((uuid: string, params: any) => {
originalUser.lastName = params.lastName;
return Promise.resolve(originalUser);
}),
};
describe('UpdateUserUseCase', () => {
let updateUserUseCase: UpdateUserUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })],
providers: [
{
provide: UsersRepository,
useValue: mockUsersRepository,
},
UpdateUserUseCase,
UserProfile,
],
}).compile();
updateUserUseCase = module.get<UpdateUserUseCase>(UpdateUserUseCase);
});
it('should be defined', () => {
expect(updateUserUseCase).toBeDefined();
});
describe('execute', () => {
it('should update an User', async () => {
const updatedUser: User = await updateUserUseCase.execute(
updateUserCommand,
);
expect(updatedUser.lastName).toBe(updateUserRequest.lastName);
});
});
});