-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46958c7
commit 6635353
Showing
2 changed files
with
34 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 () => { | ||
|
@@ -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]"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
|
@@ -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]'); | ||
}); | ||
}); | ||
|
||
}); |