Skip to content

Commit

Permalink
feat: ✨ add GetUserUsecase
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 14, 2024
1 parent 264eafd commit 11b3e7f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
49 changes: 49 additions & 0 deletions libs/user/usecase/src/lib/get-user.usecase.spec.ts
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();
});
});
});
13 changes: 13 additions & 0 deletions libs/user/usecase/src/lib/get-user.usecase.ts
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();
}
}
7 changes: 0 additions & 7 deletions libs/user/usecase/src/lib/user-usecase.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions libs/user/usecase/src/lib/user-usecase.ts

This file was deleted.

0 comments on commit 11b3e7f

Please sign in to comment.