refactor racing use cases
This commit is contained in:
@@ -1,28 +1,38 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { GetRacePenaltiesUseCase } from './GetRacePenaltiesUseCase';
|
||||
import {
|
||||
GetRacePenaltiesUseCase,
|
||||
type GetRacePenaltiesInput,
|
||||
type GetRacePenaltiesResult,
|
||||
type GetRacePenaltiesErrorCode,
|
||||
} from './GetRacePenaltiesUseCase';
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetRacePenaltiesUseCase', () => {
|
||||
let useCase: GetRacePenaltiesUseCase;
|
||||
let penaltyRepository: { findByRaceId: Mock };
|
||||
let driverRepository: { findById: Mock };
|
||||
let output: UseCaseOutputPort<GetRacePenaltiesResult> & { present: Mock };
|
||||
|
||||
beforeEach(() => {
|
||||
penaltyRepository = { findByRaceId: vi.fn() };
|
||||
driverRepository = { findById: vi.fn() };
|
||||
output = { present: vi.fn() } as UseCaseOutputPort<GetRacePenaltiesResult> & { present: Mock };
|
||||
useCase = new GetRacePenaltiesUseCase(
|
||||
penaltyRepository as unknown as IPenaltyRepository,
|
||||
driverRepository as unknown as IDriverRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return penalties with driver map', async () => {
|
||||
const raceId = 'race-1';
|
||||
it('should return penalties with drivers', async () => {
|
||||
const input: GetRacePenaltiesInput = { raceId: 'race-1' };
|
||||
const penalties = [
|
||||
{
|
||||
id: 'penalty-1',
|
||||
raceId,
|
||||
raceId: input.raceId,
|
||||
driverId: 'driver-1',
|
||||
issuedBy: 'driver-2',
|
||||
type: 'time' as const,
|
||||
@@ -38,26 +48,47 @@ describe('GetRacePenaltiesUseCase', () => {
|
||||
];
|
||||
|
||||
penaltyRepository.findByRaceId.mockResolvedValue(penalties);
|
||||
driverRepository.findById.mockImplementation((id) => Promise.resolve(drivers.find(d => d.id === id)));
|
||||
driverRepository.findById.mockImplementation((id) => Promise.resolve(drivers.find((d) => d.id === id)));
|
||||
|
||||
const result = await useCase.execute({ raceId });
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.penalties).toEqual(penalties);
|
||||
expect(dto.driverMap.get('driver-1')).toBe('Driver 1');
|
||||
expect(dto.driverMap.get('driver-2')).toBe('Driver 2');
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as GetRacePenaltiesResult;
|
||||
expect(presented.penalties).toEqual(penalties);
|
||||
expect(presented.drivers).toEqual(drivers);
|
||||
});
|
||||
|
||||
it('should return empty when no penalties', async () => {
|
||||
penaltyRepository.findByRaceId.mockResolvedValue([]);
|
||||
driverRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute({ raceId: 'race-1' });
|
||||
const input: GetRacePenaltiesInput = { raceId: 'race-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.unwrap();
|
||||
expect(dto.penalties).toEqual([]);
|
||||
expect(dto.driverMap.size).toBe(0);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = output.present.mock.calls[0]![0] as GetRacePenaltiesResult;
|
||||
expect(presented.penalties).toEqual([]);
|
||||
expect(presented.drivers).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return repository error when repository throws', async () => {
|
||||
const input: GetRacePenaltiesInput = { raceId: 'race-1' };
|
||||
const repositoryError = new Error('Repository failure');
|
||||
penaltyRepository.findByRaceId.mockRejectedValue(repositoryError);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetRacePenaltiesErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('Repository failure');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user