Some checks failed
CI / lint-typecheck (pull_request) Failing after 12s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
114 lines
3.6 KiB
TypeScript
114 lines
3.6 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { LeaguesTestContext } from '../LeaguesTestContext';
|
|
import { Driver } from '../../../../core/racing/domain/entities/Driver';
|
|
import { Standing } from '../../../../core/racing/domain/entities/Standing';
|
|
|
|
describe('GetLeagueStandings', () => {
|
|
let context: LeaguesTestContext;
|
|
|
|
beforeEach(() => {
|
|
context = new LeaguesTestContext();
|
|
});
|
|
|
|
describe('Success Path', () => {
|
|
it('should retrieve championship standings with all driver statistics', async () => {
|
|
// Given: A league exists with multiple drivers
|
|
const leagueId = 'league-123';
|
|
const driver1Id = 'driver-1';
|
|
const driver2Id = 'driver-2';
|
|
|
|
await context.racingDriverRepository.create(Driver.create({
|
|
id: driver1Id,
|
|
name: 'Driver One',
|
|
iracingId: 'ir-1',
|
|
country: 'US',
|
|
}));
|
|
|
|
await context.racingDriverRepository.create(Driver.create({
|
|
id: driver2Id,
|
|
name: 'Driver Two',
|
|
iracingId: 'ir-2',
|
|
country: 'DE',
|
|
}));
|
|
|
|
// And: Each driver has points
|
|
await context.standingRepository.save(Standing.create({
|
|
leagueId,
|
|
driverId: driver1Id,
|
|
points: 100,
|
|
position: 1,
|
|
}));
|
|
|
|
await context.standingRepository.save(Standing.create({
|
|
leagueId,
|
|
driverId: driver2Id,
|
|
points: 80,
|
|
position: 2,
|
|
}));
|
|
|
|
// When: GetLeagueStandingsUseCase.execute() is called with league ID
|
|
const result = await context.getLeagueStandingsUseCase.execute({ leagueId });
|
|
|
|
// Then: The result should contain all drivers ranked by points
|
|
expect(result.isOk()).toBe(true);
|
|
const data = result.unwrap();
|
|
expect(data.standings).toHaveLength(2);
|
|
expect(data.standings[0]!.driverId).toBe(driver1Id);
|
|
expect(data.standings[0]!.points).toBe(100);
|
|
expect(data.standings[0]!.rank).toBe(1);
|
|
expect(data.standings[1]!.driverId).toBe(driver2Id);
|
|
expect(data.standings[1]!.points).toBe(80);
|
|
expect(data.standings[1]!.rank).toBe(2);
|
|
});
|
|
|
|
it('should retrieve standings with minimal driver statistics', async () => {
|
|
const leagueId = 'league-123';
|
|
const driverId = 'driver-1';
|
|
|
|
await context.racingDriverRepository.create(Driver.create({
|
|
id: driverId,
|
|
name: 'Driver One',
|
|
iracingId: 'ir-1',
|
|
country: 'US',
|
|
}));
|
|
|
|
await context.standingRepository.save(Standing.create({
|
|
leagueId,
|
|
driverId,
|
|
points: 10,
|
|
position: 1,
|
|
}));
|
|
|
|
const result = await context.getLeagueStandingsUseCase.execute({ leagueId });
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap().standings).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe('Edge Cases', () => {
|
|
it('should handle drivers with no championship standings', async () => {
|
|
const leagueId = 'league-empty';
|
|
const result = await context.getLeagueStandingsUseCase.execute({ leagueId });
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap().standings).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('Error Handling', () => {
|
|
it('should handle repository errors gracefully', async () => {
|
|
// Mock repository error
|
|
context.standingRepository.findByLeagueId = async () => {
|
|
throw new Error('Database error');
|
|
};
|
|
|
|
const result = await context.getLeagueStandingsUseCase.execute({ leagueId: 'any' });
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
// The Result class in this project seems to use .error for the error value
|
|
expect((result as any).error.code).toBe('REPOSITORY_ERROR');
|
|
});
|
|
});
|
|
});
|