Skip to content

Commit

Permalink
feat: ✨ add UserRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 14, 2024
1 parent 1a7dd82 commit 264eafd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/user/domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// user
export * from './lib/user.entity';
export * from './lib/user.repository';
43 changes: 43 additions & 0 deletions libs/user/domain/src/lib/user.repository.spec.ts
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' },
]);
});
});
6 changes: 6 additions & 0 deletions libs/user/domain/src/lib/user.repository.ts
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[]>;
}

0 comments on commit 264eafd

Please sign in to comment.