Files
gridpilot.gg/apps/website/lib/page-queries/LeagueStandingsPageQuery.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

86 lines
3.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { LeagueStandingsPageQuery } from './LeagueStandingsPageQuery';
import { LeagueStandingsService } from '@/lib/services/leagues/LeagueStandingsService';
import { Result } from '@/lib/contracts/Result';
import { LeagueStandingsViewDataBuilder } from '@/lib/builders/view-data/LeagueStandingsViewDataBuilder';
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
// Mock dependencies
vi.mock('@/lib/services/leagues/LeagueStandingsService', () => ({
LeagueStandingsService: vi.fn().mockImplementation(function (this: any) {
this.getStandingsData = vi.fn();
}),
}));
vi.mock('@/lib/builders/view-data/LeagueStandingsViewDataBuilder', () => ({
LeagueStandingsViewDataBuilder: {
build: vi.fn(),
},
}));
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
mapToPresentationError: vi.fn(),
}));
describe('LeagueStandingsPageQuery', () => {
let query: LeagueStandingsPageQuery;
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
query = new LeagueStandingsPageQuery();
mockServiceInstance = {
getStandingsData: vi.fn(),
};
(LeagueStandingsService as any).mockImplementation(function () {
return mockServiceInstance;
});
});
it('should return view data when service succeeds', async () => {
const leagueId = 'league-123';
const apiDto = { standings: [], memberships: [] };
const viewData = { standings: [], memberships: [] };
mockServiceInstance.getStandingsData.mockResolvedValue(Result.ok(apiDto));
(LeagueStandingsViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await query.execute(leagueId);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
expect(LeagueStandingsService).toHaveBeenCalled();
expect(mockServiceInstance.getStandingsData).toHaveBeenCalledWith(leagueId);
expect(LeagueStandingsViewDataBuilder.build).toHaveBeenCalledWith(apiDto.standings, apiDto.memberships, leagueId);
});
it('should return mapped presentation error when service fails', async () => {
const leagueId = 'league-123';
const serviceError = { type: 'notFound' };
const presentationError = 'notFound';
mockServiceInstance.getStandingsData.mockResolvedValue(Result.err(serviceError));
(mapToPresentationError as any).mockReturnValue(presentationError);
const result = await query.execute(leagueId);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(presentationError);
expect(mapToPresentationError).toHaveBeenCalledWith(serviceError);
});
it('should provide a static execute method', async () => {
const leagueId = 'league-123';
const apiDto = { standings: [], memberships: [] };
const viewData = { standings: [], memberships: [] };
mockServiceInstance.getStandingsData.mockResolvedValue(Result.ok(apiDto));
(LeagueStandingsViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await LeagueStandingsPageQuery.execute(leagueId);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
});
});