86 lines
3.0 KiB
TypeScript
86 lines
3.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { LeagueSchedulePageQuery } from './LeagueSchedulePageQuery';
|
|
import { LeagueScheduleService } from '@/lib/services/leagues/LeagueScheduleService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { LeagueScheduleViewDataBuilder } from '@/lib/builders/view-data/LeagueScheduleViewDataBuilder';
|
|
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/leagues/LeagueScheduleService', () => ({
|
|
LeagueScheduleService: vi.fn(class {
|
|
getScheduleData = vi.fn();
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/lib/builders/view-data/LeagueScheduleViewDataBuilder', () => ({
|
|
LeagueScheduleViewDataBuilder: {
|
|
build: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
|
mapToPresentationError: vi.fn(),
|
|
}));
|
|
|
|
describe('LeagueSchedulePageQuery', () => {
|
|
let query: LeagueSchedulePageQuery;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new LeagueSchedulePageQuery();
|
|
mockServiceInstance = {
|
|
getScheduleData: vi.fn(),
|
|
};
|
|
(LeagueScheduleService as any).mockImplementation(function () {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
it('should return view data when service succeeds', async () => {
|
|
const leagueId = 'league-123';
|
|
const apiDto = { races: [] };
|
|
const viewData = { races: [] };
|
|
|
|
mockServiceInstance.getScheduleData.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueScheduleViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(LeagueScheduleService).toHaveBeenCalled();
|
|
expect(mockServiceInstance.getScheduleData).toHaveBeenCalledWith(leagueId);
|
|
expect(LeagueScheduleViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
|
});
|
|
|
|
it('should return mapped presentation error when service fails', async () => {
|
|
const leagueId = 'league-123';
|
|
const serviceError = { type: 'notFound' };
|
|
const presentationError = 'notFound';
|
|
|
|
mockServiceInstance.getScheduleData.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 = { races: [] };
|
|
const viewData = { races: [] };
|
|
|
|
mockServiceInstance.getScheduleData.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueScheduleViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await LeagueSchedulePageQuery.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
});
|
|
});
|