-
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.
Merge pull request #36 from zhumeisongsong/feature/user-usecase
feat: ✨ GetUserUsecase
- Loading branch information
Showing
8 changed files
with
95 additions
and
26 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
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 +1,3 @@ | ||
// user | ||
export * from './lib/user.entity'; | ||
export * from './lib/user.repository'; |
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,32 @@ | ||
import { UserRepository } from './user.repository'; | ||
import { User } from './user.entity'; | ||
|
||
class MockUserRepository implements UserRepository { | ||
private users: User[] = [ | ||
{ id: '1', name: 'John Doe' }, | ||
{ id: '2', name: 'Jane Doe' }, | ||
]; | ||
|
||
async findById(id: string): Promise<User | null> { | ||
return this.users.find((user) => user.id === id) || null; | ||
} | ||
} | ||
|
||
describe('UserRepository', () => { | ||
let userRepository: UserRepository; | ||
|
||
beforeEach(() => { | ||
userRepository = new MockUserRepository(); | ||
}); | ||
|
||
test('findById should return a user by id', async () => { | ||
const user = await userRepository.findById('1'); | ||
expect(user).toEqual({ id: '1', name: 'John Doe' }); | ||
}); | ||
|
||
test('findById should return null if user not found', async () => { | ||
const user = await userRepository.findById('3'); | ||
expect(user).toBeNull(); | ||
}); | ||
|
||
}); |
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,5 @@ | ||
import { User } from './user.entity'; | ||
|
||
export interface UserRepository { | ||
findById(id: string): Promise<User | null>; | ||
} |
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,36 @@ | ||
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(), | ||
} 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'); | ||
}); | ||
}); | ||
}); |
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,9 @@ | ||
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.