import { describe, it, expect, vi, beforeEach } from 'vitest'; import { LeagueWalletPageQuery } from './LeagueWalletPageQuery'; import { LeagueWalletService } from '@/lib/services/leagues/LeagueWalletService'; import { Result } from '@/lib/contracts/Result'; import { LeagueWalletViewDataBuilder } from '@/lib/builders/view-data/LeagueWalletViewDataBuilder'; import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError'; // Mock dependencies vi.mock('@/lib/services/leagues/LeagueWalletService', () => ({ LeagueWalletService: vi.fn(class { getWalletData = vi.fn(); }), })); vi.mock('@/lib/builders/view-data/LeagueWalletViewDataBuilder', () => ({ LeagueWalletViewDataBuilder: { build: vi.fn(), }, })); vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({ mapToPresentationError: vi.fn(), })); describe('LeagueWalletPageQuery', () => { let query: LeagueWalletPageQuery; let mockServiceInstance: any; beforeEach(() => { vi.clearAllMocks(); query = new LeagueWalletPageQuery(); mockServiceInstance = { getWalletData: vi.fn(), }; (LeagueWalletService as any).mockImplementation(function () { return mockServiceInstance; }); }); it('should return view data when service succeeds', async () => { const leagueId = 'league-123'; const apiDto = { balance: 100 }; const viewData = { balance: 100 }; mockServiceInstance.getWalletData.mockResolvedValue(Result.ok(apiDto)); (LeagueWalletViewDataBuilder.build as any).mockReturnValue(viewData); const result = await query.execute(leagueId); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual(viewData); expect(LeagueWalletService).toHaveBeenCalled(); expect(mockServiceInstance.getWalletData).toHaveBeenCalledWith(leagueId); expect(LeagueWalletViewDataBuilder.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.getWalletData.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 = { balance: 100 }; const viewData = { balance: 100 }; mockServiceInstance.getWalletData.mockResolvedValue(Result.ok(apiDto)); (LeagueWalletViewDataBuilder.build as any).mockReturnValue(viewData); const result = await LeagueWalletPageQuery.execute(leagueId); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual(viewData); }); });