refactor racing use cases
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetRaceResultsDetailUseCase } from './GetRaceResultsDetailUseCase';
|
||||
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetRaceResultsDetailUseCase,
|
||||
type GetRaceResultsDetailInput,
|
||||
type GetRaceResultsDetailResult,
|
||||
type GetRaceResultsDetailErrorCode,
|
||||
} from './GetRaceResultsDetailUseCase';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetRaceResultsDetailUseCase', () => {
|
||||
let useCase: GetRaceResultsDetailUseCase;
|
||||
@@ -13,6 +20,7 @@ describe('GetRaceResultsDetailUseCase', () => {
|
||||
let resultRepository: { findByRaceId: Mock };
|
||||
let driverRepository: { findAll: Mock };
|
||||
let penaltyRepository: { findByRaceId: Mock };
|
||||
let output: UseCaseOutputPort<GetRaceResultsDetailResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = { findById: vi.fn() };
|
||||
@@ -20,39 +28,79 @@ describe('GetRaceResultsDetailUseCase', () => {
|
||||
resultRepository = { findByRaceId: vi.fn() };
|
||||
driverRepository = { findAll: vi.fn() };
|
||||
penaltyRepository = { findByRaceId: vi.fn() };
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<GetRaceResultsDetailResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
useCase = new GetRaceResultsDetailUseCase(
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
resultRepository as unknown as IResultRepository,
|
||||
driverRepository as unknown as IDriverRepository,
|
||||
penaltyRepository as unknown as IPenaltyRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return race results detail when race exists', async () => {
|
||||
const raceId = 'race-1';
|
||||
it('presents race results detail when race exists', async () => {
|
||||
const input: GetRaceResultsDetailInput = { raceId: 'race-1' };
|
||||
|
||||
const race = {
|
||||
id: raceId,
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
track: 'Track 1',
|
||||
scheduledAt: new Date('2023-01-01T10:00:00Z'),
|
||||
status: 'completed' as const,
|
||||
};
|
||||
|
||||
const league = {
|
||||
id: 'league-1',
|
||||
name: 'League 1',
|
||||
settings: { pointsSystem: 'f1-2024' },
|
||||
};
|
||||
|
||||
const results = [
|
||||
{ driverId: 'driver-1', position: 1, fastestLap: 120 },
|
||||
{ driverId: 'driver-2', position: 2, fastestLap: 125 },
|
||||
{
|
||||
id: 'res-1',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-1',
|
||||
position: { toNumber: () => 1 },
|
||||
fastestLap: { toNumber: () => 120 },
|
||||
incidents: { toNumber: () => 0 },
|
||||
startPosition: { toNumber: () => 1 },
|
||||
},
|
||||
{
|
||||
id: 'res-2',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-2',
|
||||
position: { toNumber: () => 2 },
|
||||
fastestLap: { toNumber: () => 125 },
|
||||
incidents: { toNumber: () => 1 },
|
||||
startPosition: { toNumber: () => 2 },
|
||||
},
|
||||
];
|
||||
|
||||
const drivers = [
|
||||
{ id: 'driver-1', name: 'Driver 1' },
|
||||
{ id: 'driver-2', name: 'Driver 2' },
|
||||
];
|
||||
|
||||
const penalties = [
|
||||
{ driverId: 'driver-1', type: 'time' as const, value: 10 },
|
||||
{
|
||||
id: 'pen-1',
|
||||
leagueId: 'league-1',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-1',
|
||||
type: 'time_penalty',
|
||||
value: 5,
|
||||
reason: 'cut track',
|
||||
protestId: undefined,
|
||||
issuedBy: 'steward-1',
|
||||
status: 'pending',
|
||||
issuedAt: new Date(),
|
||||
appliedAt: undefined,
|
||||
notes: undefined,
|
||||
},
|
||||
];
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
@@ -61,32 +109,57 @@ describe('GetRaceResultsDetailUseCase', () => {
|
||||
driverRepository.findAll.mockResolvedValue(drivers);
|
||||
penaltyRepository.findByRaceId.mockResolvedValue(penalties);
|
||||
|
||||
const result = await useCase.execute({ raceId });
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const viewModel = result.unwrap();
|
||||
expect(viewModel.race).toEqual({
|
||||
id: raceId,
|
||||
leagueId: 'league-1',
|
||||
track: 'Track 1',
|
||||
scheduledAt: new Date('2023-01-01T10:00:00Z'),
|
||||
status: 'completed',
|
||||
});
|
||||
expect(viewModel.league).toEqual({ id: 'league-1', name: 'League 1' });
|
||||
expect(viewModel.results).toEqual(results);
|
||||
expect(viewModel.drivers).toEqual(drivers);
|
||||
expect(viewModel.penalties).toEqual([{ driverId: 'driver-1', type: 'time', value: 10 }]);
|
||||
expect(viewModel.pointsSystem).toBeDefined();
|
||||
expect(viewModel.fastestLapTime).toBe(120);
|
||||
expect(viewModel.currentDriverId).toBe('driver-1');
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = (output.present as Mock).mock.calls[0]![0] as GetRaceResultsDetailResult;
|
||||
|
||||
expect(presented.race).toEqual(race);
|
||||
expect(presented.league).toEqual(league);
|
||||
expect(presented.results).toEqual(results);
|
||||
expect(presented.drivers).toEqual(drivers);
|
||||
expect(presented.penalties).toEqual(penalties);
|
||||
expect(presented.pointsSystem).toBeDefined();
|
||||
expect(presented.fastestLapTime).toBe(120);
|
||||
expect(presented.currentDriverId).toBe('driver-1');
|
||||
});
|
||||
|
||||
it('should return error when race not found', async () => {
|
||||
it('returns error when race not found and does not present data', async () => {
|
||||
const input: GetRaceResultsDetailInput = { raceId: 'race-1' };
|
||||
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.error).toEqual({ code: 'RACE_NOT_FOUND' });
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetRaceResultsDetailErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('RACE_NOT_FOUND');
|
||||
expect(error.details.message).toBe('Race not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns repository error when an unexpected error occurs', async () => {
|
||||
const input: GetRaceResultsDetailInput = { raceId: 'race-1' };
|
||||
|
||||
raceRepository.findById.mockRejectedValue(new Error('Database failure'));
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetRaceResultsDetailErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(error.code).toBe('REPOSITORY_ERROR');
|
||||
expect(error.details.message).toBe('Database failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user