refactor core presenters
This commit is contained in:
@@ -4,6 +4,8 @@ import type { IStandingRepository } from '../../domain/repositories/IStandingRep
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
||||
import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
|
||||
import type { DriverRatingPort } from '../ports/DriverRatingPort';
|
||||
|
||||
describe('GetLeagueDriverSeasonStatsUseCase', () => {
|
||||
@@ -12,12 +14,16 @@ describe('GetLeagueDriverSeasonStatsUseCase', () => {
|
||||
const mockPenaltyFindByRaceId = vi.fn();
|
||||
const mockRaceFindByLeagueId = vi.fn();
|
||||
const mockDriverRatingGetRating = vi.fn();
|
||||
const mockDriverFindById = vi.fn();
|
||||
const mockTeamFindById = vi.fn();
|
||||
|
||||
let useCase: GetLeagueDriverSeasonStatsUseCase;
|
||||
let standingRepository: IStandingRepository;
|
||||
let resultRepository: IResultRepository;
|
||||
let penaltyRepository: IPenaltyRepository;
|
||||
let raceRepository: IRaceRepository;
|
||||
let driverRepository: IDriverRepository;
|
||||
let teamRepository: ITeamRepository;
|
||||
let driverRatingPort: DriverRatingPort;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -51,6 +57,12 @@ describe('GetLeagueDriverSeasonStatsUseCase', () => {
|
||||
delete: vi.fn(),
|
||||
exists: vi.fn(),
|
||||
};
|
||||
driverRepository = {
|
||||
findById: mockDriverFindById,
|
||||
};
|
||||
teamRepository = {
|
||||
findById: mockTeamFindById,
|
||||
};
|
||||
driverRatingPort = {
|
||||
getRating: mockDriverRatingGetRating,
|
||||
};
|
||||
@@ -60,6 +72,8 @@ describe('GetLeagueDriverSeasonStatsUseCase', () => {
|
||||
resultRepository,
|
||||
penaltyRepository,
|
||||
raceRepository,
|
||||
driverRepository,
|
||||
teamRepository,
|
||||
driverRatingPort,
|
||||
);
|
||||
});
|
||||
@@ -80,6 +94,8 @@ describe('GetLeagueDriverSeasonStatsUseCase', () => {
|
||||
];
|
||||
const mockResults = [{ position: 1 }];
|
||||
const mockRating = { rating: 1500, ratingChange: 50 };
|
||||
const mockDriver = { id: 'driver-1', name: 'Driver One', teamId: 'team-1' };
|
||||
const mockTeam = { id: 'team-1', name: 'Team One' };
|
||||
|
||||
standingRepository.findByLeagueId.mockResolvedValue(mockStandings);
|
||||
raceRepository.findByLeagueId.mockResolvedValue(mockRaces);
|
||||
@@ -89,19 +105,39 @@ describe('GetLeagueDriverSeasonStatsUseCase', () => {
|
||||
});
|
||||
driverRatingPort.getRating.mockReturnValue(mockRating);
|
||||
resultRepository.findByDriverIdAndLeagueId.mockResolvedValue(mockResults);
|
||||
driverRepository.findById.mockImplementation((id) => {
|
||||
if (id === 'driver-1') return Promise.resolve(mockDriver);
|
||||
if (id === 'driver-2') return Promise.resolve({ id: 'driver-2', name: 'Driver Two' });
|
||||
return Promise.resolve(null);
|
||||
});
|
||||
teamRepository.findById.mockResolvedValue(mockTeam);
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.value!;
|
||||
expect(dto.leagueId).toBe('league-1');
|
||||
expect(dto.standings).toEqual([
|
||||
{ driverId: 'driver-1', position: 1, points: 100, racesCompleted: 5 },
|
||||
{ driverId: 'driver-2', position: 2, points: 80, racesCompleted: 5 },
|
||||
]);
|
||||
expect(dto.penalties.get('driver-1')).toEqual({ baseDelta: -10, bonusDelta: 0 });
|
||||
expect(dto.driverRatings.get('driver-1')).toEqual(mockRating);
|
||||
expect(dto.driverResults.get('driver-1')).toEqual(mockResults);
|
||||
const output = result.value!;
|
||||
expect(output.leagueId).toBe('league-1');
|
||||
expect(output.stats).toHaveLength(2);
|
||||
expect(output.stats[0]).toEqual({
|
||||
leagueId: 'league-1',
|
||||
driverId: 'driver-1',
|
||||
position: 1,
|
||||
driverName: 'Driver One',
|
||||
teamId: 'team-1',
|
||||
teamName: 'Team One',
|
||||
totalPoints: 100,
|
||||
basePoints: 90,
|
||||
penaltyPoints: -10,
|
||||
bonusPoints: 0,
|
||||
pointsPerRace: 20,
|
||||
racesStarted: 1,
|
||||
racesFinished: 1,
|
||||
dnfs: 0,
|
||||
noShows: 1,
|
||||
avgFinish: 1,
|
||||
rating: 1500,
|
||||
ratingChange: 50,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle no penalties', async () => {
|
||||
@@ -111,17 +147,20 @@ describe('GetLeagueDriverSeasonStatsUseCase', () => {
|
||||
const mockRaces = [{ id: 'race-1' }];
|
||||
const mockResults = [{ position: 1 }];
|
||||
const mockRating = { rating: null, ratingChange: null };
|
||||
const mockDriver = { id: 'driver-1', name: 'Driver One' };
|
||||
|
||||
standingRepository.findByLeagueId.mockResolvedValue(mockStandings);
|
||||
raceRepository.findByLeagueId.mockResolvedValue(mockRaces);
|
||||
penaltyRepository.findByRaceId.mockResolvedValue([]);
|
||||
driverRatingPort.getRating.mockReturnValue(mockRating);
|
||||
resultRepository.findByDriverIdAndLeagueId.mockResolvedValue(mockResults);
|
||||
driverRepository.findById.mockResolvedValue(mockDriver);
|
||||
teamRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(params);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const dto = result.value!;
|
||||
expect(dto.penalties.size).toBe(0);
|
||||
const output = result.value!;
|
||||
expect(output.stats[0].penaltyPoints).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user