api client refactor
This commit is contained in:
121
apps/website/lib/services/races/RaceResultsService.test.ts
Normal file
121
apps/website/lib/services/races/RaceResultsService.test.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
23
apps/website/lib/services/races/RaceResultsService.ts
Normal file
23
apps/website/lib/services/races/RaceResultsService.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentRaceResultsDetail } from '../../presenters';
|
||||
import { RaceResultsDetailViewModel } from '../../view-models';
|
||||
|
||||
export async function getRaceResults(
|
||||
raceId: string,
|
||||
currentUserId?: string
|
||||
): Promise<RaceResultsDetailViewModel> {
|
||||
const dto = await api.races.getResultsDetail(raceId);
|
||||
return presentRaceResultsDetail(dto, currentUserId);
|
||||
}
|
||||
|
||||
export async function getRaceSOF(raceId: string): Promise<any> {
|
||||
const dto = await api.races.getWithSOF(raceId);
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
}
|
||||
|
||||
export async function importRaceResults(raceId: string, input: any): Promise<any> {
|
||||
const dto = await api.races.importResults(raceId, input);
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
}
|
||||
22
apps/website/lib/services/races/RaceService.ts
Normal file
22
apps/website/lib/services/races/RaceService.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentRaceDetail } from '../../presenters';
|
||||
import { RaceDetailViewModel } from '../../view-models';
|
||||
|
||||
export async function getRaceDetail(
|
||||
raceId: string,
|
||||
driverId: string
|
||||
): Promise<RaceDetailViewModel> {
|
||||
const dto = await api.races.getDetail(raceId, driverId);
|
||||
return presentRaceDetail(dto);
|
||||
}
|
||||
|
||||
export async function getRacesPageData(): Promise<any> {
|
||||
const dto = await api.races.getPageData();
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
}
|
||||
|
||||
export async function getRacesTotal(): Promise<any> {
|
||||
const dto = await api.races.getTotal();
|
||||
return dto;
|
||||
}
|
||||
Reference in New Issue
Block a user