-
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
264eafd
commit 11b3e7f
Showing
4 changed files
with
62 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { GetUserUsecase } from './get-user.usecase'; | ||
import { UserRepository } from '@user/domain'; | ||
|
||
describe('GetUserUsecase', () => { | ||
let getUserUsecase: GetUserUsecase; | ||
let userRepository: jest.Mocked<UserRepository>; | ||
|
||
beforeEach(() => { | ||
userRepository = { | ||
findById: jest.fn(), | ||
findAll: jest.fn(), | ||
} as unknown as jest.Mocked<UserRepository>; | ||
|
||
getUserUsecase = new GetUserUsecase(userRepository); | ||
}); | ||
|
||
describe('execute', () => { | ||
it('should return a user when found', async () => { | ||
const user = { id: '1', name: 'John Doe' }; | ||
userRepository.findById.mockResolvedValue(user); | ||
|
||
const result = await getUserUsecase.execute('1'); | ||
|
||
expect(result).toEqual(user); | ||
expect(userRepository.findById).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
it('should return null when user is not found', async () => { | ||
userRepository.findById.mockResolvedValue(null); | ||
|
||
const result = await getUserUsecase.execute('1'); | ||
|
||
expect(result).toBeNull(); | ||
expect(userRepository.findById).toHaveBeenCalledWith('1'); | ||
}); | ||
}); | ||
|
||
describe('executeAll', () => { | ||
it('should return all users', async () => { | ||
const users = [{ id: '1', name: 'John Doe' }, { id: '2', name: 'Jane Doe' }]; | ||
userRepository.findAll.mockResolvedValue(users); | ||
|
||
const result = await getUserUsecase.executeAll(); | ||
|
||
expect(result).toEqual(users); | ||
expect(userRepository.findAll).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { User, UserRepository } from '@user/domain'; | ||
|
||
export class GetUserUsecase { | ||
constructor(private readonly userRepository: UserRepository) {} | ||
|
||
async execute(id: string): Promise<User | null> { | ||
return this.userRepository.findById(id); | ||
} | ||
|
||
async executeAll(): Promise<User[]> { | ||
return this.userRepository.findAll(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.