diff --git a/libs/tasks/application/src/lib/tasks.service.spec.ts b/libs/tasks/application/src/lib/tasks.service.spec.ts index b7b160e..85a3af6 100644 --- a/libs/tasks/application/src/lib/tasks.service.spec.ts +++ b/libs/tasks/application/src/lib/tasks.service.spec.ts @@ -1,29 +1,45 @@ import { Test, TestingModule } from '@nestjs/testing'; import { TasksService } from './tasks.service'; -import { GetAllTasksUseCase } from './use-cases/get-all-tasks.use-case'; +import { TASKS_REPOSITORY, TasksRepository, Task } from '@tasks/domain'; describe('TasksService', () => { let service: TasksService; - let getAllTasksUseCase: GetAllTasksUseCase; + let mockTasksRepository: TasksRepository; beforeEach(async () => { + mockTasksRepository = { + findAll: jest.fn(), + }; + const module: TestingModule = await Test.createTestingModule({ providers: [ TasksService, { - provide: GetAllTasksUseCase, - useValue: { - execute: jest.fn(), - }, + provide: TASKS_REPOSITORY, + useValue: mockTasksRepository, }, ], }).compile(); service = module.get(TasksService); - getAllTasksUseCase = module.get(GetAllTasksUseCase); }); it('should be defined', () => { expect(service).toBeDefined(); }); + + describe('findMany', () => { + it('should return all tasks', async () => { + const mockTasks = [ + Task.create('task-1', 'Task 1', 'Description 1', ['category-1']), + Task.create('task-2', 'Task 2', 'Description 2', ['category-2']), + ]; + mockTasksRepository.findAll = jest.fn().mockResolvedValue(mockTasks); + + const result = await service.findAll(); + + expect(result).toEqual(mockTasks); + expect(mockTasksRepository.findAll).toHaveBeenCalled(); + }); + }); }); diff --git a/libs/tasks/application/src/lib/tasks.service.ts b/libs/tasks/application/src/lib/tasks.service.ts index 35e617c..b71d851 100644 --- a/libs/tasks/application/src/lib/tasks.service.ts +++ b/libs/tasks/application/src/lib/tasks.service.ts @@ -1,13 +1,14 @@ -import { Injectable } from '@nestjs/common'; -import { Task } from '@tasks/domain'; - -import { GetAllTasksUseCase } from './use-cases/get-all-tasks.use-case'; +import { Inject, Injectable } from '@nestjs/common'; +import { Task, TASKS_REPOSITORY, TasksRepository } from '@tasks/domain'; @Injectable() export class TasksService { - constructor(private readonly getAllTasksUseCase: GetAllTasksUseCase) {} + constructor( + @Inject(TASKS_REPOSITORY) + private readonly tasksRepository: TasksRepository, + ) {} async findAll(): Promise { - return await this.getAllTasksUseCase.execute(); + return await this.tasksRepository.findAll(); } } diff --git a/libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.spec.ts b/libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.spec.ts new file mode 100644 index 0000000..97b2cd8 --- /dev/null +++ b/libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.spec.ts @@ -0,0 +1,50 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { TasksService } from '../tasks.service'; +import { GetAllTasksUseCase } from './get-all-tasks.use-case'; +import { Task } from '@tasks/domain'; + +describe('GetAllTasksUseCase', () => { + let useCase: GetAllTasksUseCase; + let tasksService: TasksService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + GetAllTasksUseCase, + { + provide: TasksService, + useValue: { + findAll: jest.fn(), + }, + }, + ], + }).compile(); + + useCase = module.get(GetAllTasksUseCase); + tasksService = module.get(TasksService); + }); + + it('should be defined', () => { + expect(useCase).toBeDefined(); + }); + + describe('execute', () => { + it('should return all tasks', async () => { + const expectedTasks: Task[] = [ + { + id: '1', + title: 'Test Task', + description: 'Test Description', + categories: ['category-1'], + }, + ]; + + jest.spyOn(tasksService, 'findAll').mockResolvedValue(expectedTasks); + + const result = await useCase.execute(); + + expect(result).toEqual(expectedTasks); + expect(tasksService.findAll).toHaveBeenCalled(); + }); + }); +}); diff --git a/libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.ts b/libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.ts index 94cfc69..c23cdc5 100644 --- a/libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.ts +++ b/libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.ts @@ -1,14 +1,13 @@ -import { Inject, Injectable } from '@nestjs/common'; -import { Task, TASKS_REPOSITORY, TasksRepository } from '@tasks/domain'; +import { Injectable } from '@nestjs/common'; +import { Task } from '@tasks/domain'; + +import { TasksService } from '../tasks.service'; @Injectable() export class GetAllTasksUseCase { - constructor( - @Inject(TASKS_REPOSITORY) - private readonly tasksRepository: TasksRepository, - ) {} + constructor(private readonly tasksService: TasksService) {} async execute(): Promise { - return await this.tasksRepository.findAll(); + return await this.tasksService.findAll(); } }