refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -13,7 +13,6 @@ import { Race } from '../../domain/entities/Race';
import { Protest } from '../../domain/entities/Protest';
import { Driver } from '../../domain/entities/Driver';
import { League } from '../../domain/entities/League';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
describe('GetLeagueProtestsUseCase', () => {
@@ -30,8 +29,6 @@ describe('GetLeagueProtestsUseCase', () => {
let leagueRepository: {
findById: Mock;
};
let output: UseCaseOutputPort<GetLeagueProtestsResult> & { present: Mock };
beforeEach(() => {
raceRepository = {
findByLeagueId: vi.fn(),
@@ -45,17 +42,10 @@ describe('GetLeagueProtestsUseCase', () => {
leagueRepository = {
findById: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<GetLeagueProtestsResult> & { present: Mock };
useCase = new GetLeagueProtestsUseCase(
raceRepository as unknown as IRaceRepository,
useCase = new GetLeagueProtestsUseCase(raceRepository as unknown as IRaceRepository,
protestRepository as unknown as IProtestRepository,
driverRepository as unknown as IDriverRepository,
leagueRepository as unknown as ILeagueRepository,
output,
);
leagueRepository as unknown as ILeagueRepository);
});
it('should return protests with races and drivers', async () => {
@@ -108,18 +98,14 @@ describe('GetLeagueProtestsUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0]?.[0] as GetLeagueProtestsResult;
expect(presented?.league).toEqual(league);
expect(presented?.protests).toHaveLength(1);
const presentedProtest = presented?.protests[0];
expect(presentedProtest?.protest).toEqual(protest);
expect(presentedProtest?.race).toEqual(race);
expect(presentedProtest?.protestingDriver).toEqual(driver1);
expect(presentedProtest?.accusedDriver).toEqual(driver2);
const resultValue = result.unwrap();
expect(resultValue.league).toEqual(league);
expect(resultValue.protests).toHaveLength(1);
const presentedProtest = resultValue.protests[0];
expect(presentedProtest.protest).toEqual(protest);
expect(presentedProtest.race).toEqual(race);
expect(presentedProtest.protestingDriver).toEqual(driver1);
expect(presentedProtest.accusedDriver).toEqual(driver2);
});
it('should return empty protests when no races', async () => {
@@ -138,13 +124,9 @@ describe('GetLeagueProtestsUseCase', () => {
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
const presented = output.present.mock.calls[0]?.[0] as GetLeagueProtestsResult;
expect(presented?.league).toEqual(league);
expect(presented?.protests).toEqual([]);
const resultValue = result.unwrap();
expect(resultValue.league).toEqual(league);
expect(resultValue.protests).toEqual([]);
});
it('should return LEAGUE_NOT_FOUND when league does not exist', async () => {
@@ -164,8 +146,7 @@ describe('GetLeagueProtestsUseCase', () => {
expect(err.code).toBe('LEAGUE_NOT_FOUND');
expect(err.details.message).toBe('League not found');
expect(output.present).not.toHaveBeenCalled();
});
});
it('should return REPOSITORY_ERROR when repository throws', async () => {
const leagueId = 'league-1';
@@ -191,6 +172,5 @@ describe('GetLeagueProtestsUseCase', () => {
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('DB error');
expect(output.present).not.toHaveBeenCalled();
});
});
});