user/.old/tests/unit/find-user-by-uuid.usecase.s...

83 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-01-11 10:26:58 +00:00
import { NotFoundException } from '@nestjs/common';
2022-12-13 17:00:07 +00:00
import { Test, TestingModule } from '@nestjs/testing';
import { UsersRepository } from '../../adapters/secondaries/users.repository';
2023-01-11 10:26:58 +00:00
import { FindUserByUuidRequest } from '../../domain/dtos/find-user-by-uuid.request';
2022-12-13 17:00:07 +00:00
import { FindUserByUuidUseCase } from '../../domain/usecases/find-user-by-uuid.usecase';
import { FindUserByUuidQuery } from '../../queries/find-user-by-uuid.query';
import { MESSAGE_PUBLISHER } from '../../../../app.constants';
2022-12-13 17:00:07 +00:00
const mockUser = {
uuid: 'bb281075-1b98-4456-89d6-c643d3044a91',
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@email.com',
2022-12-21 15:15:57 +00:00
phone: '0601020304',
2022-12-13 17:00:07 +00:00
};
const mockUserRepository = {
2023-01-11 10:26:58 +00:00
findOneByUuid: jest
.fn()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.mockImplementationOnce((query?: FindUserByUuidQuery) => {
return Promise.resolve(mockUser);
})
.mockImplementation(() => {
return Promise.resolve(undefined);
}),
2022-12-13 17:00:07 +00:00
};
const mockMessagePublisher = {
2022-12-23 14:14:51 +00:00
publish: jest.fn().mockImplementation(),
};
2022-12-13 17:00:07 +00:00
describe('FindUserByUuidUseCase', () => {
let findUserByUuidUseCase: FindUserByUuidUseCase;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [],
providers: [
{
provide: UsersRepository,
useValue: mockUserRepository,
},
2022-12-23 14:14:51 +00:00
{
provide: MESSAGE_PUBLISHER,
useValue: mockMessagePublisher,
2022-12-23 14:14:51 +00:00
},
2022-12-13 17:00:07 +00:00
FindUserByUuidUseCase,
],
}).compile();
findUserByUuidUseCase = module.get<FindUserByUuidUseCase>(
FindUserByUuidUseCase,
);
});
it('should be defined', () => {
expect(findUserByUuidUseCase).toBeDefined();
});
describe('execute', () => {
it('should return a user', async () => {
2023-01-11 10:26:58 +00:00
const findUserByUuidRequest: FindUserByUuidRequest =
new FindUserByUuidRequest();
findUserByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91';
2022-12-13 17:00:07 +00:00
const user = await findUserByUuidUseCase.execute(
2023-01-11 10:26:58 +00:00
new FindUserByUuidQuery(findUserByUuidRequest),
2022-12-13 17:00:07 +00:00
);
expect(user).toBe(mockUser);
});
2023-01-11 10:26:58 +00:00
it('should throw an error if user does not exist', async () => {
const findUserByUuidRequest: FindUserByUuidRequest =
new FindUserByUuidRequest();
findUserByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a90';
await expect(
findUserByUuidUseCase.execute(
new FindUserByUuidQuery(findUserByUuidRequest),
),
).rejects.toBeInstanceOf(NotFoundException);
});
2022-12-13 17:00:07 +00:00
});
});