view models
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { describe, it, expect, vi, Mocked } from 'vitest';
|
||||
import { DriverRegistrationService } from './DriverRegistrationService';
|
||||
import { DriversApiClient } from '../../api/drivers/DriversApiClient';
|
||||
import { DriverRegistrationStatusViewModel } from '../../view-models/DriverRegistrationStatusViewModel';
|
||||
|
||||
describe('DriverRegistrationService', () => {
|
||||
let mockApiClient: Mocked<DriversApiClient>;
|
||||
let service: DriverRegistrationService;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
getRegistrationStatus: vi.fn(),
|
||||
} as Mocked<DriversApiClient>;
|
||||
|
||||
service = new DriverRegistrationService(mockApiClient);
|
||||
});
|
||||
|
||||
describe('getDriverRegistrationStatus', () => {
|
||||
it('should call apiClient.getRegistrationStatus and return DriverRegistrationStatusViewModel', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const raceId = 'race-456';
|
||||
|
||||
const mockDto = {
|
||||
isRegistered: true,
|
||||
raceId: 'race-456',
|
||||
driverId: 'driver-123',
|
||||
};
|
||||
|
||||
mockApiClient.getRegistrationStatus.mockResolvedValue(mockDto);
|
||||
|
||||
const result = await service.getDriverRegistrationStatus(driverId, raceId);
|
||||
|
||||
expect(mockApiClient.getRegistrationStatus).toHaveBeenCalledWith(driverId, raceId);
|
||||
expect(result).toBeInstanceOf(DriverRegistrationStatusViewModel);
|
||||
expect(result.isRegistered).toBe(true);
|
||||
expect(result.raceId).toBe('race-456');
|
||||
expect(result.driverId).toBe('driver-123');
|
||||
expect(result.statusMessage).toBe('Registered for this race');
|
||||
expect(result.statusColor).toBe('green');
|
||||
expect(result.statusBadgeVariant).toBe('success');
|
||||
expect(result.registrationButtonText).toBe('Withdraw');
|
||||
expect(result.canRegister).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle unregistered driver', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const raceId = 'race-456';
|
||||
|
||||
const mockDto = {
|
||||
isRegistered: false,
|
||||
raceId: 'race-456',
|
||||
driverId: 'driver-123',
|
||||
};
|
||||
|
||||
mockApiClient.getRegistrationStatus.mockResolvedValue(mockDto);
|
||||
|
||||
const result = await service.getDriverRegistrationStatus(driverId, raceId);
|
||||
|
||||
expect(result.isRegistered).toBe(false);
|
||||
expect(result.statusMessage).toBe('Not registered');
|
||||
expect(result.statusColor).toBe('red');
|
||||
expect(result.statusBadgeVariant).toBe('warning');
|
||||
expect(result.registrationButtonText).toBe('Register');
|
||||
expect(result.canRegister).toBe(true);
|
||||
});
|
||||
|
||||
it('should throw error when apiClient.getRegistrationStatus fails', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const raceId = 'race-456';
|
||||
|
||||
const error = new Error('API call failed');
|
||||
mockApiClient.getRegistrationStatus.mockRejectedValue(error);
|
||||
|
||||
await expect(service.getDriverRegistrationStatus(driverId, raceId)).rejects.toThrow('API call failed');
|
||||
});
|
||||
});
|
||||
});
|
||||
130
apps/website/lib/services/drivers/DriverService.test.ts
Normal file
130
apps/website/lib/services/drivers/DriverService.test.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { describe, it, expect, vi, Mocked } from 'vitest';
|
||||
import { DriverService } from './DriverService';
|
||||
import { DriversApiClient } from '../../api/drivers/DriversApiClient';
|
||||
import { DriverLeaderboardViewModel } from '../../view-models/DriverLeaderboardViewModel';
|
||||
import { DriverViewModel } from '../../view-models/DriverViewModel';
|
||||
import { CompleteOnboardingViewModel } from '../../view-models/CompleteOnboardingViewModel';
|
||||
|
||||
describe('DriverService', () => {
|
||||
let mockApiClient: Mocked<DriversApiClient>;
|
||||
let service: DriverService;
|
||||
|
||||
beforeEach(() => {
|
||||
mockApiClient = {
|
||||
getLeaderboard: vi.fn(),
|
||||
completeOnboarding: vi.fn(),
|
||||
getCurrent: vi.fn(),
|
||||
} as Mocked<DriversApiClient>;
|
||||
|
||||
service = new DriverService(mockApiClient);
|
||||
});
|
||||
|
||||
describe('getDriverLeaderboard', () => {
|
||||
it('should call apiClient.getLeaderboard and return DriverLeaderboardViewModel', async () => {
|
||||
const mockDto = {
|
||||
drivers: [
|
||||
{
|
||||
id: 'driver-1',
|
||||
name: 'John Doe',
|
||||
rating: 2500,
|
||||
wins: 10,
|
||||
podiums: 15,
|
||||
totalRaces: 50,
|
||||
country: 'US',
|
||||
avatarUrl: 'https://example.com/avatar.jpg',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
mockApiClient.getLeaderboard.mockResolvedValue(mockDto);
|
||||
|
||||
const result = await service.getDriverLeaderboard();
|
||||
|
||||
expect(mockApiClient.getLeaderboard).toHaveBeenCalled();
|
||||
expect(result).toBeInstanceOf(DriverLeaderboardViewModel);
|
||||
expect(result.drivers).toHaveLength(1);
|
||||
expect(result.drivers[0].name).toBe('John Doe');
|
||||
});
|
||||
|
||||
it('should throw error when apiClient.getLeaderboard fails', async () => {
|
||||
const error = new Error('API call failed');
|
||||
mockApiClient.getLeaderboard.mockRejectedValue(error);
|
||||
|
||||
await expect(service.getDriverLeaderboard()).rejects.toThrow('API call failed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('completeDriverOnboarding', () => {
|
||||
it('should call apiClient.completeOnboarding and return CompleteOnboardingViewModel', async () => {
|
||||
const input = {
|
||||
iracingId: '123456',
|
||||
country: 'US',
|
||||
};
|
||||
|
||||
const mockDto = {
|
||||
success: true,
|
||||
driverId: 'driver-123',
|
||||
};
|
||||
|
||||
mockApiClient.completeOnboarding.mockResolvedValue(mockDto);
|
||||
|
||||
const result = await service.completeDriverOnboarding(input);
|
||||
|
||||
expect(mockApiClient.completeOnboarding).toHaveBeenCalledWith(input);
|
||||
expect(result).toBeInstanceOf(CompleteOnboardingViewModel);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.isSuccessful).toBe(true);
|
||||
});
|
||||
|
||||
it('should throw error when apiClient.completeOnboarding fails', async () => {
|
||||
const input = {
|
||||
iracingId: '123456',
|
||||
country: 'US',
|
||||
};
|
||||
|
||||
const error = new Error('API call failed');
|
||||
mockApiClient.completeOnboarding.mockRejectedValue(error);
|
||||
|
||||
await expect(service.completeDriverOnboarding(input)).rejects.toThrow('API call failed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCurrentDriver', () => {
|
||||
it('should call apiClient.getCurrent and return DriverViewModel when driver exists', async () => {
|
||||
const mockDto = {
|
||||
id: 'driver-123',
|
||||
name: 'John Doe',
|
||||
avatarUrl: 'https://example.com/avatar.jpg',
|
||||
iracingId: '123456',
|
||||
rating: 2500,
|
||||
};
|
||||
|
||||
mockApiClient.getCurrent.mockResolvedValue(mockDto);
|
||||
|
||||
const result = await service.getCurrentDriver();
|
||||
|
||||
expect(mockApiClient.getCurrent).toHaveBeenCalled();
|
||||
expect(result).toBeInstanceOf(DriverViewModel);
|
||||
expect(result?.id).toBe('driver-123');
|
||||
expect(result?.name).toBe('John Doe');
|
||||
expect(result?.hasIracingId).toBe(true);
|
||||
expect(result?.formattedRating).toBe('2500');
|
||||
});
|
||||
|
||||
it('should return null when apiClient.getCurrent returns null', async () => {
|
||||
mockApiClient.getCurrent.mockResolvedValue(null);
|
||||
|
||||
const result = await service.getCurrentDriver();
|
||||
|
||||
expect(mockApiClient.getCurrent).toHaveBeenCalled();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should throw error when apiClient.getCurrent fails', async () => {
|
||||
const error = new Error('API call failed');
|
||||
mockApiClient.getCurrent.mockRejectedValue(error);
|
||||
|
||||
await expect(service.getCurrentDriver()).rejects.toThrow('API call failed');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,17 @@
|
||||
import type { DriversApiClient } from '../../api/drivers/DriversApiClient';
|
||||
import { DriverLeaderboardViewModel } from '../../view-models';
|
||||
import { DriverViewModel } from '../../view-models/DriverViewModel';
|
||||
import { CompleteOnboardingViewModel } from '../../view-models/CompleteOnboardingViewModel';
|
||||
import type { CompleteOnboardingInputDTO } from '../../types/generated';
|
||||
import { DriversApiClient } from "@/lib/api/drivers/DriversApiClient";
|
||||
import { CompleteOnboardingInputDTO } from "@/lib/types/generated/CompleteOnboardingInputDTO";
|
||||
import { CompleteOnboardingViewModel } from "@/lib/view-models/CompleteOnboardingViewModel";
|
||||
import { DriverLeaderboardViewModel } from "@/lib/view-models/DriverLeaderboardViewModel";
|
||||
import { DriverViewModel } from "@/lib/view-models/DriverViewModel";
|
||||
|
||||
// TODO: Create proper DriverDTO in generated types
|
||||
type DriverDTO = {
|
||||
id: string;
|
||||
name: string;
|
||||
avatarUrl?: string;
|
||||
iracingId?: string;
|
||||
rating?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Driver Service
|
||||
|
||||
Reference in New Issue
Block a user