From 45875fa6aa76ace3b8c87b28f6e9429303072ba0 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Tue, 17 Dec 2024 14:18:38 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20GetAllUserTasksUseCas?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/tasks/application/src/index.ts | 1 + .../get-all-user-tasks.use-case.spec.ts | 49 +++++++++++++++++++ .../use-cases/get-all-user-tasks.use-case.ts | 13 +++++ 3 files changed, 63 insertions(+) create mode 100644 libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.spec.ts create mode 100644 libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.ts diff --git a/libs/tasks/application/src/index.ts b/libs/tasks/application/src/index.ts index 4552cf9..d450914 100644 --- a/libs/tasks/application/src/index.ts +++ b/libs/tasks/application/src/index.ts @@ -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'; diff --git a/libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.spec.ts b/libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.spec.ts new file mode 100644 index 0000000..9fe21aa --- /dev/null +++ b/libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.spec.ts @@ -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); + userTasksService = moduleRef.get(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); + }); + }); +}); diff --git a/libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.ts b/libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.ts new file mode 100644 index 0000000..61e14b6 --- /dev/null +++ b/libs/tasks/application/src/lib/use-cases/get-all-user-tasks.use-case.ts @@ -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 { + return await this.userTasksService.findAll(userId); + } +}