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