-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4efd91f
commit 372d010
Showing
4 changed files
with
86 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>(TasksService); | ||
getAllTasksUseCase = module.get<GetAllTasksUseCase>(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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Task[]> { | ||
return await this.getAllTasksUseCase.execute(); | ||
return await this.tasksRepository.findAll(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>(GetAllTasksUseCase); | ||
tasksService = module.get<TasksService>(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(); | ||
}); | ||
}); | ||
}); |
13 changes: 6 additions & 7 deletions
13
libs/tasks/application/src/lib/use-cases/get-all-tasks.use-case.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Task[]> { | ||
return await this.tasksRepository.findAll(); | ||
return await this.tasksService.findAll(); | ||
} | ||
} |