-
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.
feat: ✨ make GetUserUseCase be injectbale
- Loading branch information
1 parent
3886d0d
commit fae29b5
Showing
3 changed files
with
24 additions
and
12 deletions.
There are no files selected for viewing
18 changes: 9 additions & 9 deletions
18
libs/users/application/src/lib/use-cases/get-user.use-case.spec.ts
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,36 +1,36 @@ | ||
import { GetUserUseCase } from './get-user.use-case'; | ||
import { UserRepository } from '@users/domain'; | ||
import { UsersRepository } from '@users/domain'; | ||
|
||
describe('GetUserUseCase', () => { | ||
let getUserUseCase: GetUserUseCase; | ||
let userRepository: jest.Mocked<UserRepository>; | ||
let usersRepository: jest.Mocked<UsersRepository>; | ||
|
||
beforeEach(() => { | ||
userRepository = { | ||
usersRepository = { | ||
findById: jest.fn(), | ||
} as unknown as jest.Mocked<UserRepository>; | ||
} as unknown as jest.Mocked<UsersRepository>; | ||
|
||
getUserUseCase = new GetUserUseCase(userRepository); | ||
getUserUseCase = new GetUserUseCase(usersRepository); | ||
}); | ||
|
||
describe('execute', () => { | ||
it('should return a user when found', async () => { | ||
const user = { id: '1', name: 'John Doe' }; | ||
userRepository.findById.mockResolvedValue(user); | ||
usersRepository.findById.mockResolvedValue(user); | ||
|
||
const result = await getUserUseCase.execute('1'); | ||
|
||
expect(result).toEqual(user); | ||
expect(userRepository.findById).toHaveBeenCalledWith('1'); | ||
expect(usersRepository.findById).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
it('should return null when user is not found', async () => { | ||
userRepository.findById.mockResolvedValue(null); | ||
usersRepository.findById.mockResolvedValue(null); | ||
|
||
const result = await getUserUseCase.execute('1'); | ||
|
||
expect(result).toBeNull(); | ||
expect(userRepository.findById).toHaveBeenCalledWith('1'); | ||
expect(usersRepository.findById).toHaveBeenCalledWith('1'); | ||
}); | ||
}); | ||
}); |
16 changes: 13 additions & 3 deletions
16
libs/users/application/src/lib/use-cases/get-user.use-case.ts
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,9 +1,19 @@ | ||
import { User, UserRepository } from '@users/domain'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { User, USERS_REPOSITORY, UsersRepository } from '@users/domain'; | ||
|
||
@Injectable() | ||
export class GetUserUseCase { | ||
constructor(private readonly userRepository: UserRepository) {} | ||
constructor( | ||
@Inject(USERS_REPOSITORY) | ||
private readonly usersRepository: UsersRepository, | ||
) {} | ||
|
||
async execute(id: string): Promise<User | null> { | ||
return this.userRepository.findById(id); | ||
const user = this.usersRepository.findById(id); | ||
|
||
if (!user) { | ||
return null; | ||
} | ||
return user; | ||
} | ||
} |
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