From f6dfa7d143b6cef73c96afaba03b4a17c90c243f Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Tue, 17 Dec 2024 14:06:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20task=20module=20use?= =?UTF-8?q?=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/tasks/application/src/index.ts | 4 ++ .../create-some-user-tasks.use-case.spec.ts | 47 +++++++++++++++++++ .../create-some-user-tasks.use-case.ts | 14 ++++++ .../update-some-user-tasks.use-case.spec.ts | 47 +++++++++++++++++++ .../update-some-user-tasks.use-case.ts | 14 ++++++ 5 files changed, 126 insertions(+) create mode 100644 libs/tasks/application/src/lib/use-cases/create-some-user-tasks.use-case.spec.ts create mode 100644 libs/tasks/application/src/lib/use-cases/create-some-user-tasks.use-case.ts create mode 100644 libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.spec.ts create mode 100644 libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.ts diff --git a/libs/tasks/application/src/index.ts b/libs/tasks/application/src/index.ts index db6d3b9..4552cf9 100644 --- a/libs/tasks/application/src/index.ts +++ b/libs/tasks/application/src/index.ts @@ -1,4 +1,8 @@ +// tasks export * from './lib/tasks.service'; export * from './lib/use-cases/get-all-tasks.use-case'; +// user tasks export * from './lib/user-tasks.service'; +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/create-some-user-tasks.use-case.spec.ts b/libs/tasks/application/src/lib/use-cases/create-some-user-tasks.use-case.spec.ts new file mode 100644 index 0000000..fd678db --- /dev/null +++ b/libs/tasks/application/src/lib/use-cases/create-some-user-tasks.use-case.spec.ts @@ -0,0 +1,47 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UserTasksService } from '../user-tasks.service'; +import { CreateSomeUserTasksUseCase } from './create-some-user-tasks.use-case'; + +describe('CreateSomeUserTasksUseCase', () => { + let useCase: CreateSomeUserTasksUseCase; + let userTasksService: UserTasksService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + CreateSomeUserTasksUseCase, + { + provide: UserTasksService, + useValue: { + createSome: jest.fn(), + }, + }, + ], + }).compile(); + + useCase = module.get(CreateSomeUserTasksUseCase); + userTasksService = module.get(UserTasksService); + }); + + it('should be defined', () => { + expect(useCase).toBeDefined(); + }); + + describe('execute', () => { + it('should create multiple user tasks', async () => { + const userId = 'test-user-id'; + const tasks = [ + { id: '1', createdAt: new Date() }, + { id: '2', createdAt: new Date() }, + ]; + const expectedResult = 'success'; + + jest.spyOn(userTasksService, 'createSome').mockResolvedValue(expectedResult); + + const result = await useCase.execute(userId, tasks); + + expect(result).toEqual(expectedResult); + expect(userTasksService.createSome).toHaveBeenCalledWith(userId, tasks); + }); + }); +}); diff --git a/libs/tasks/application/src/lib/use-cases/create-some-user-tasks.use-case.ts b/libs/tasks/application/src/lib/use-cases/create-some-user-tasks.use-case.ts new file mode 100644 index 0000000..4656e6c --- /dev/null +++ b/libs/tasks/application/src/lib/use-cases/create-some-user-tasks.use-case.ts @@ -0,0 +1,14 @@ +import { Injectable } from '@nestjs/common'; +import { UserTasksService } from '../user-tasks.service'; + +@Injectable() +export class CreateSomeUserTasksUseCase { + constructor(private readonly userTasksService: UserTasksService) {} + + async execute( + userId: string, + tasks: { id: string; createdAt: Date }[], + ): Promise { + return await this.userTasksService.createSome(userId, tasks); + } +} diff --git a/libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.spec.ts b/libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.spec.ts new file mode 100644 index 0000000..bdc4a85 --- /dev/null +++ b/libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.spec.ts @@ -0,0 +1,47 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UserTasksService } from '../user-tasks.service'; +import { UpdateSomeUserTasksUseCase } from './update-some-user-tasks.use-case'; + +describe('UpdateSomeUserTasksUseCase', () => { + let useCase: UpdateSomeUserTasksUseCase; + let userTasksService: UserTasksService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + UpdateSomeUserTasksUseCase, + { + provide: UserTasksService, + useValue: { + updateSome: jest.fn(), + }, + }, + ], + }).compile(); + + useCase = module.get(UpdateSomeUserTasksUseCase); + userTasksService = module.get(UserTasksService); + }); + + it('should be defined', () => { + expect(useCase).toBeDefined(); + }); + + describe('execute', () => { + it('should update multiple user tasks', async () => { + const userId = 'test-user-id'; + const userTasks = [ + { id: '1', updatedAt: new Date() }, + { id: '2', updatedAt: new Date() }, + ]; + const expectedResult = 'success'; + + jest.spyOn(userTasksService, 'updateSome').mockResolvedValue(expectedResult); + + const result = await useCase.execute(userId, userTasks); + + expect(result).toEqual(expectedResult); + expect(userTasksService.updateSome).toHaveBeenCalledWith(userId, userTasks); + }); + }); +}); diff --git a/libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.ts b/libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.ts new file mode 100644 index 0000000..70864ca --- /dev/null +++ b/libs/tasks/application/src/lib/use-cases/update-some-user-tasks.use-case.ts @@ -0,0 +1,14 @@ +import { Injectable } from '@nestjs/common'; +import { UserTasksService } from '../user-tasks.service'; + +@Injectable() +export class UpdateSomeUserTasksUseCase { + constructor(private readonly userTasksService: UserTasksService) {} + + async execute( + userId: string, + userTasks: { id: string; updatedAt: Date }[], + ): Promise { + return await this.userTasksService.updateSome(userId, userTasks); + } +}