77 lines
2.6 KiB
TypeScript
77 lines
2.6 KiB
TypeScript
/* eslint-disable gridpilot-rules/page-query-filename, gridpilot-rules/page-query-must-use-builders, gridpilot-rules/single-export-per-file, @typescript-eslint/no-explicit-any */
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { LeagueDetailPageQuery } from './LeagueDetailPageQuery';
|
|
import { LeagueService } from '@/lib/services/leagues/LeagueService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/leagues/LeagueService', () => ({
|
|
LeagueService: vi.fn(class {
|
|
getLeagueDetailData = vi.fn();
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
|
mapToPresentationError: vi.fn(),
|
|
}));
|
|
|
|
describe('LeagueDetailPageQuery', () => {
|
|
let query: LeagueDetailPageQuery;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new LeagueDetailPageQuery();
|
|
mockServiceInstance = {
|
|
getLeagueDetailData: vi.fn(),
|
|
};
|
|
(LeagueService as any).mockImplementation(function () {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
it('should return league detail data when service succeeds', async () => {
|
|
const leagueId = 'league-123';
|
|
const apiDto = { id: leagueId, name: 'Test League' } as any;
|
|
|
|
mockServiceInstance.getLeagueDetailData.mockResolvedValue(Result.ok(apiDto));
|
|
|
|
const result = await query.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(apiDto);
|
|
expect(LeagueService).toHaveBeenCalled();
|
|
expect(mockServiceInstance.getLeagueDetailData).toHaveBeenCalledWith(leagueId);
|
|
});
|
|
|
|
it('should return mapped presentation error when service fails', async () => {
|
|
const leagueId = 'league-123';
|
|
const serviceError = { type: 'notFound' };
|
|
const presentationError = 'notFound';
|
|
|
|
mockServiceInstance.getLeagueDetailData.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 = { id: leagueId, name: 'Test League' } as any;
|
|
|
|
mockServiceInstance.getLeagueDetailData.mockResolvedValue(Result.ok(apiDto));
|
|
|
|
const result = await LeagueDetailPageQuery.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(apiDto);
|
|
});
|
|
});
|
|
|
|
export {};
|