Files
gridpilot.gg/apps/website/lib/services/races/RaceResultsService.test.ts
2025-12-17 18:01:47 +01:00

121 lines
3.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getRaceResults, getRaceSOF, importRaceResults } from './RaceResultsService';
import type { RaceResultsDetailDto, RaceWithSOFDto, ImportRaceResultsSummaryDto } from '../../dtos';
// Mock the API client
vi.mock('../../api', () => ({
apiClient: {
races: {
getResultsDetail: vi.fn(),
getWithSOF: vi.fn(),
importResults: vi.fn(),
},
},
}));
// Mock the presenter
vi.mock('../../presenters', () => ({
presentRaceResultsDetail: vi.fn(),
}));
import { api } from '../../api';
import { presentRaceResultsDetail } from '../../presenters';
describe('RaceResultsService', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('getRaceResults', () => {
it('should call API and presenter with correct parameters', async () => {
const mockDto: RaceResultsDetailDto = {
id: 'race-1',
name: 'Test Race',
results: [],
// ... other required fields
} as RaceResultsDetailDto;
const mockViewModel = {
id: 'race-1',
name: 'Test Race',
formattedResults: [],
};
const raceId = 'race-123';
const currentUserId = 'user-456';
// Mock API call
vi.mocked(api.races.getResultsDetail).mockResolvedValue(mockDto);
// Mock presenter
vi.mocked(presentRaceResultsDetail).mockReturnValue(mockViewModel);
const result = await getRaceResults(raceId, currentUserId);
expect(api.races.getResultsDetail).toHaveBeenCalledWith(raceId);
expect(presentRaceResultsDetail).toHaveBeenCalledWith(mockDto, currentUserId);
expect(result).toBe(mockViewModel);
});
it('should call presenter with undefined currentUserId when not provided', async () => {
const mockDto: RaceResultsDetailDto = {
id: 'race-1',
name: 'Test Race',
results: [],
} as RaceResultsDetailDto;
const mockViewModel = {
id: 'race-1',
name: 'Test Race',
formattedResults: [],
};
const raceId = 'race-123';
vi.mocked(api.races.getResultsDetail).mockResolvedValue(mockDto);
vi.mocked(presentRaceResultsDetail).mockReturnValue(mockViewModel);
await getRaceResults(raceId);
expect(presentRaceResultsDetail).toHaveBeenCalledWith(mockDto, undefined);
});
});
describe('getRaceSOF', () => {
it('should call API and return DTO directly', async () => {
const mockDto: RaceWithSOFDto = {
id: 'race-1',
name: 'Test Race',
sof: 1500,
// ... other fields
} as RaceWithSOFDto;
const raceId = 'race-123';
vi.mocked(api.races.getWithSOF).mockResolvedValue(mockDto);
const result = await getRaceSOF(raceId);
expect(api.races.getWithSOF).toHaveBeenCalledWith(raceId);
expect(result).toBe(mockDto);
});
});
describe('importRaceResults', () => {
it('should call API with correct parameters and return result', async () => {
const mockInput = { results: [] };
const mockSummary: ImportRaceResultsSummaryDto = {
totalImported: 10,
errors: [],
};
const raceId = 'race-123';
vi.mocked(api.races.importResults).mockResolvedValue(mockSummary);
const result = await importRaceResults(raceId, mockInput);
expect(api.races.importResults).toHaveBeenCalledWith(raceId, mockInput);
expect(result).toBe(mockSummary);
});
});
});