refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -1,8 +1,9 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { GetAllTeamsUseCase } from './GetAllTeamsUseCase';
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
import { GetAllTeamsUseCase, type GetAllTeamsInput, type GetAllTeamsResult } from './GetAllTeamsUseCase';
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import type { Logger } from '@core/shared/application';
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
describe('GetAllTeamsUseCase', () => {
const mockTeamFindAll = vi.fn();
@@ -36,8 +37,13 @@ describe('GetAllTeamsUseCase', () => {
error: vi.fn(),
};
let output: UseCaseOutputPort<GetAllTeamsResult> & { present: Mock };
beforeEach(() => {
vi.clearAllMocks();
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<GetAllTeamsResult> & { present: Mock };
});
it('should return teams data', async () => {
@@ -45,6 +51,7 @@ describe('GetAllTeamsUseCase', () => {
mockTeamRepo,
mockTeamMembershipRepo,
mockLogger,
output,
);
const team1 = {
@@ -69,10 +76,15 @@ describe('GetAllTeamsUseCase', () => {
mockTeamFindAll.mockResolvedValue([team1, team2]);
mockTeamMembershipCountByTeamId.mockImplementation((id: string) => Promise.resolve(id === 'team1' ? 5 : 3));
const result = await useCase.execute();
const result = await useCase.execute({} as GetAllTeamsInput);
expect(result.isOk()).toBe(true);
expect(result.value).toEqual({
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0][0] as GetAllTeamsResult;
expect(presented).toEqual({
teams: [
{
id: 'team1',
@@ -95,6 +107,7 @@ describe('GetAllTeamsUseCase', () => {
memberCount: 3,
},
],
totalCount: 2,
});
});
@@ -103,15 +116,22 @@ describe('GetAllTeamsUseCase', () => {
mockTeamRepo,
mockTeamMembershipRepo,
mockLogger,
output,
);
mockTeamFindAll.mockResolvedValue([]);
const result = await useCase.execute();
const result = await useCase.execute({} as GetAllTeamsInput);
expect(result.isOk()).toBe(true);
expect(result.value).toEqual({
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0][0] as GetAllTeamsResult;
expect(presented).toEqual({
teams: [],
totalCount: 0,
});
});
@@ -120,15 +140,20 @@ describe('GetAllTeamsUseCase', () => {
mockTeamRepo,
mockTeamMembershipRepo,
mockLogger,
output,
);
const error = new Error('Repository error');
mockTeamFindAll.mockRejectedValue(error);
const result = await useCase.execute();
const result = await useCase.execute({} as GetAllTeamsInput);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
expect(result.unwrapErr().details.message).toBe('Repository error');
const err = result.unwrapErr();
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('Repository error');
expect(output.present).not.toHaveBeenCalled();
});
});
});