Skip to content

Commit

Permalink
feat: ✨ add task module use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 17, 2024
1 parent 1a24931 commit f6dfa7d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libs/tasks/application/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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>(CreateSomeUserTasksUseCase);
userTasksService = module.get<UserTasksService>(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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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<string> {
return await this.userTasksService.createSome(userId, tasks);
}
}
Original file line number Diff line number Diff line change
@@ -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>(UpdateSomeUserTasksUseCase);
userTasksService = module.get<UserTasksService>(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);
});
});
});
Original file line number Diff line number Diff line change
@@ -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<string> {
return await this.userTasksService.updateSome(userId, userTasks);
}
}

0 comments on commit f6dfa7d

Please sign in to comment.