Skip to content

Commit

Permalink
test: 🧪 add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 4, 2024
1 parent 46958c7 commit 6635353
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('GetUserByEmailUseCase', () => {
const result = await getUserByEmailUseCase.execute('[email protected]');

expect(result).toEqual(mockUser);
expect(usersRepository.findByEmail).toHaveBeenCalledWith('1');
expect(usersRepository.findByEmail).toHaveBeenCalledWith("[email protected]");
});

it('should return null when user not found', async () => {
Expand All @@ -35,7 +35,7 @@ describe('GetUserByEmailUseCase', () => {
const result = await getUserByEmailUseCase.execute('[email protected]');

expect(result).toBeNull();
expect(usersRepository.findByEmail).toHaveBeenCalledWith('1');
expect(usersRepository.findByEmail).toHaveBeenCalledWith("[email protected]");
});
});
});
54 changes: 32 additions & 22 deletions libs/users/application/src/lib/users.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Test, TestingModule } from '@nestjs/testing';
import { User } from '@users/domain';

import { UsersService } from './users.service';
import { GetUserByIdUseCase } from './use-cases/get-user-by-id.use-case';
import { GetUserByEmailUseCase } from './use-cases/get-user-by-email.use-case';

describe('UsersService', () => {
let service: UsersService;
let getUserByIdUseCase: GetUserByIdUseCase;
let getUserByEmailUseCase: GetUserByEmailUseCase;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -18,59 +18,69 @@ describe('UsersService', () => {
execute: jest.fn(),
},
},
{
provide: GetUserByEmailUseCase,
useValue: {
execute: jest.fn(),
},
},
],
}).compile();

service = module.get<UsersService>(UsersService);
getUserByIdUseCase = module.get<GetUserByIdUseCase>(GetUserByIdUseCase);
getUserByEmailUseCase = module.get<GetUserByEmailUseCase>(GetUserByEmailUseCase);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findById', () => {
it('should return a user if found', async () => {
const user: User = {
describe('getUserById', () => {
it('should return user when found', async () => {
const mockUser = {
id: '1',
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
};
jest.spyOn(getUserByIdUseCase, 'execute').mockResolvedValue(user);
(getUserByIdUseCase.execute as jest.Mock).mockResolvedValue(mockUser);

const result = await service.findById('1');
expect(result).toEqual(user);

expect(result).toEqual(mockUser);
expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1');
});

it('should return null if user not found', async () => {
jest.spyOn(getUserByIdUseCase, 'execute').mockResolvedValue(null);
it('should return null when user not found', async () => {
(getUserByIdUseCase.execute as jest.Mock).mockResolvedValue(null);

const result = await service.findById('1');

const result = await service.findById('2');
expect(result).toBeNull();
expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1');
});
});

describe('findByEmail', () => {
it('should return a user if found', async () => {
const user: User = {
describe('getUserByEmail', () => {
it('should return user when found', async () => {
const mockUser = {
id: '1',
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
};
jest.spyOn(getUserByIdUseCase, 'execute').mockResolvedValue(user);
(getUserByEmailUseCase.execute as jest.Mock).mockResolvedValue(mockUser);

const result = await service.findByEmail('[email protected]');
expect(result).toEqual(user);

expect(result).toEqual(mockUser);
expect(getUserByEmailUseCase.execute).toHaveBeenCalledWith('[email protected]');
});

it('should return null if user not found', async () => {
jest.spyOn(getUserByIdUseCase, 'execute').mockResolvedValue(null);
it('should return null when user not found', async () => {
(getUserByEmailUseCase.execute as jest.Mock).mockResolvedValue(null);

const result = await service.findByEmail('[email protected]');

expect(result).toBeNull();
expect(getUserByEmailUseCase.execute).toHaveBeenCalledWith('[email protected]');
});
});

});

0 comments on commit 6635353

Please sign in to comment.