Skip to content

Commit

Permalink
refactor: ♻️ TasksService
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 17, 2024
1 parent 4efd91f commit 372d010
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 20 deletions.
30 changes: 23 additions & 7 deletions libs/tasks/application/src/lib/tasks.service.spec.ts
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();
});
});
});
13 changes: 7 additions & 6 deletions libs/tasks/application/src/lib/tasks.service.ts
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();
}
}
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();
});
});
});
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();
}
}

0 comments on commit 372d010

Please sign in to comment.