Files
gridpilot.gg/apps/website/lib/page-queries/DriverRankingsPageQuery.test.ts
Marc Mintel 1b0a1f4aee
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 23:29:55 +01:00

81 lines
3.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { DriverRankingsPageQuery } from './DriverRankingsPageQuery';
import { DriverRankingsService } from '@/lib/services/leaderboards/DriverRankingsService';
import { Result } from '@/lib/contracts/Result';
import { DriverRankingsViewDataBuilder } from '@/lib/builders/view-data/DriverRankingsViewDataBuilder';
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
// Mock dependencies
const mockGetDriverRankings = vi.fn();
vi.mock('@/lib/services/leaderboards/DriverRankingsService', () => {
return {
DriverRankingsService: class {
getDriverRankings = mockGetDriverRankings;
},
};
});
vi.mock('@/lib/builders/view-data/DriverRankingsViewDataBuilder', () => ({
DriverRankingsViewDataBuilder: {
build: vi.fn(),
},
}));
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
mapToPresentationError: vi.fn(),
}));
describe('DriverRankingsPageQuery', () => {
let query: DriverRankingsPageQuery;
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
mockServiceInstance = {
getDriverRankings: mockGetDriverRankings,
};
query = new DriverRankingsPageQuery(mockServiceInstance);
});
it('should return view data when service succeeds', async () => {
const apiDto = { drivers: [{ id: 'driver-1', name: 'Test Driver', points: 100 }] };
const viewData = { drivers: [{ id: 'driver-1', name: 'Test Driver', points: 100 }] };
mockServiceInstance.getDriverRankings.mockResolvedValue(Result.ok(apiDto));
(DriverRankingsViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await query.execute();
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
expect(mockServiceInstance.getDriverRankings).toHaveBeenCalled();
expect(DriverRankingsViewDataBuilder.build).toHaveBeenCalledWith(apiDto.drivers);
});
it('should return mapped presentation error when service fails', async () => {
const serviceError = { type: 'notFound' };
const presentationError = 'notFound';
mockServiceInstance.getDriverRankings.mockResolvedValue(Result.err(serviceError));
(mapToPresentationError as any).mockReturnValue(presentationError);
const result = await query.execute();
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(presentationError);
expect(mapToPresentationError).toHaveBeenCalledWith(serviceError);
});
it('should provide a static execute method', async () => {
const apiDto = { drivers: [{ id: 'driver-1', name: 'Test Driver', points: 100 }] };
const viewData = { drivers: [{ id: 'driver-1', name: 'Test Driver', points: 100 }] };
mockServiceInstance.getDriverRankings.mockResolvedValue(Result.ok(apiDto));
(DriverRankingsViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await DriverRankingsPageQuery.execute();
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
});
});