-
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
1a7dd82
commit 264eafd
Showing
3 changed files
with
51 additions
and
0 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 |
---|---|---|
@@ -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,43 @@ | ||
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; | ||
} | ||
|
||
async findAll(): Promise<User[]> { | ||
return this.users; | ||
} | ||
} | ||
|
||
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(); | ||
}); | ||
|
||
test('findAll should return all users', async () => { | ||
const users = await userRepository.findAll(); | ||
expect(users).toEqual([ | ||
{ id: '1', name: 'John Doe' }, | ||
{ id: '2', name: 'Jane Doe' }, | ||
]); | ||
}); | ||
}); |
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,6 @@ | ||
import { User } from './user.entity'; | ||
|
||
export interface UserRepository { | ||
findById(id: string): Promise<User | null>; | ||
findAll(): Promise<User[]>; | ||
} |