Skip to content

Commit

Permalink
feat: ✨ add GetAllUserTasksUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 17, 2024
1 parent f6dfa7d commit 45875fa
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/tasks/application/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export * from './lib/use-cases/get-all-tasks.use-case';

// user tasks
export * from './lib/user-tasks.service';
export * from './lib/use-cases/get-all-user-tasks.use-case';
export * from './lib/use-cases/create-some-user-tasks.use-case';
export * from './lib/use-cases/update-some-user-tasks.use-case';
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Test } from '@nestjs/testing';
import { TaskStatusEnum, UserTask } from '@tasks/domain';
import { GetAllUserTasksUseCase } from './get-all-user-tasks.use-case';
import { UserTasksService } from '../user-tasks.service';

describe('GetAllUserTasksUseCase', () => {
let useCase: GetAllUserTasksUseCase;
let userTasksService: UserTasksService;

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
GetAllUserTasksUseCase,
{
provide: UserTasksService,
useValue: {
findAll: jest.fn(),
},
},
],
}).compile();

useCase = moduleRef.get<GetAllUserTasksUseCase>(GetAllUserTasksUseCase);
userTasksService = moduleRef.get<UserTasksService>(UserTasksService);
});

describe('execute', () => {
it('should return all user tasks', async () => {
const userId = 'test-user-id';
const expectedTasks: UserTask[] = [
{
id: '1',
userId,
taskId: '1',
createdAt: new Date('2024-01-01T00:00:00.000Z'),
status: TaskStatusEnum.TODO,
updatedAt: null,
},
];

jest.spyOn(userTasksService, 'findAll').mockResolvedValue(expectedTasks);

const result = await useCase.execute(userId);

expect(result).toBe(expectedTasks);
expect(userTasksService.findAll).toHaveBeenCalledWith(userId);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from "@nestjs/common";
import { UserTask } from "@tasks/domain";

import { UserTasksService } from "../user-tasks.service";

@Injectable()
export class GetAllUserTasksUseCase {
constructor(private readonly userTasksService: UserTasksService) {}

async execute(userId: string): Promise<UserTask[]> {
return await this.userTasksService.findAll(userId);
}
}

0 comments on commit 45875fa

Please sign in to comment.