90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import { describe, it, expect, vi, Mocked } from 'vitest';
|
|
import { PenaltyService } from './PenaltyService';
|
|
import { PenaltiesApiClient } from '../../api/penalties/PenaltiesApiClient';
|
|
|
|
describe('PenaltyService', () => {
|
|
let mockApiClient: Mocked<PenaltiesApiClient>;
|
|
let service: PenaltyService;
|
|
|
|
beforeEach(() => {
|
|
mockApiClient = {
|
|
getRacePenalties: vi.fn(),
|
|
applyPenalty: vi.fn(),
|
|
} as Mocked<PenaltiesApiClient>;
|
|
|
|
service = new PenaltyService(mockApiClient);
|
|
});
|
|
|
|
describe('findByRaceId', () => {
|
|
it('should call apiClient.getRacePenalties and return penalties array', async () => {
|
|
const raceId = 'race-123';
|
|
const mockDto = {
|
|
penalties: [
|
|
{ id: 'penalty-1', driverId: 'driver-1', type: 'time', value: 5, reason: 'Incident' },
|
|
{ id: 'penalty-2', driverId: 'driver-2', type: 'grid', value: 3, reason: 'Qualifying incident' },
|
|
],
|
|
};
|
|
|
|
mockApiClient.getRacePenalties.mockResolvedValue(mockDto);
|
|
|
|
const result = await service.findByRaceId(raceId);
|
|
|
|
expect(mockApiClient.getRacePenalties).toHaveBeenCalledWith(raceId);
|
|
expect(result).toEqual(mockDto.penalties);
|
|
expect(result).toHaveLength(2);
|
|
});
|
|
|
|
it('should handle empty penalties array', async () => {
|
|
const raceId = 'race-123';
|
|
const mockDto = { penalties: [] };
|
|
|
|
mockApiClient.getRacePenalties.mockResolvedValue(mockDto);
|
|
|
|
const result = await service.findByRaceId(raceId);
|
|
|
|
expect(result).toEqual([]);
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
it('should throw error when apiClient.getRacePenalties fails', async () => {
|
|
const raceId = 'race-123';
|
|
const error = new Error('API call failed');
|
|
mockApiClient.getRacePenalties.mockRejectedValue(error);
|
|
|
|
await expect(service.findByRaceId(raceId)).rejects.toThrow('API call failed');
|
|
});
|
|
});
|
|
|
|
describe('applyPenalty', () => {
|
|
it('should call apiClient.applyPenalty with input', async () => {
|
|
const input = {
|
|
driverId: 'driver-123',
|
|
raceId: 'race-456',
|
|
type: 'time',
|
|
value: 10,
|
|
reason: 'Test reason',
|
|
};
|
|
|
|
mockApiClient.applyPenalty.mockResolvedValue(undefined);
|
|
|
|
await service.applyPenalty(input);
|
|
|
|
expect(mockApiClient.applyPenalty).toHaveBeenCalledWith(input);
|
|
});
|
|
|
|
it('should throw error when apiClient.applyPenalty fails', async () => {
|
|
const input = {
|
|
driverId: 'driver-123',
|
|
raceId: 'race-456',
|
|
type: 'time',
|
|
value: 10,
|
|
reason: 'Test reason',
|
|
};
|
|
|
|
const error = new Error('API call failed');
|
|
mockApiClient.applyPenalty.mockRejectedValue(error);
|
|
|
|
await expect(service.applyPenalty(input)).rejects.toThrow('API call failed');
|
|
});
|
|
});
|
|
}); |