import { describe, it, expect, vi, beforeEach } from 'vitest'; import { DashboardPageQuery } from './DashboardPageQuery'; import { DashboardService } from '@/lib/services/analytics/DashboardService'; import { Result } from '@/lib/contracts/Result'; import { DashboardViewDataBuilder } from '@/lib/builders/view-data/DashboardViewDataBuilder'; import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError'; // Mock dependencies vi.mock('@/lib/services/analytics/DashboardService', () => ({ DashboardService: vi.fn().mockImplementation(function (this: any) { this.getDashboardOverview = vi.fn(); }), })); vi.mock('@/lib/builders/view-data/DashboardViewDataBuilder', () => ({ DashboardViewDataBuilder: { build: vi.fn(), }, })); vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({ mapToPresentationError: vi.fn(), })); describe('DashboardPageQuery', () => { let query: DashboardPageQuery; let mockServiceInstance: any; beforeEach(() => { vi.clearAllMocks(); query = new DashboardPageQuery(); mockServiceInstance = { getDashboardOverview: vi.fn(), }; (DashboardService as any).mockImplementation(function (this: any) { return mockServiceInstance; }); }); it('should return view data when service succeeds', async () => { const apiDto = { some: 'dashboard-data' }; const viewData = { transformed: 'dashboard-view' } as any; mockServiceInstance.getDashboardOverview.mockResolvedValue(Result.ok(apiDto)); (DashboardViewDataBuilder.build as any).mockReturnValue(viewData); const result = await query.execute(); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual(viewData); expect(DashboardService).toHaveBeenCalled(); expect(mockServiceInstance.getDashboardOverview).toHaveBeenCalled(); expect(DashboardViewDataBuilder.build).toHaveBeenCalledWith(apiDto); }); it('should return mapped presentation error when service fails', async () => { const serviceError = 'some-domain-error'; const presentationError = 'some-presentation-error'; mockServiceInstance.getDashboardOverview.mockResolvedValue(Result.err(serviceError)); (mapToPresentationError as any).mockReturnValue(presentationError); const result = await query.execute(); expect(result.isErr()).toBe(true); expect(result.getError()).toBe(presentationError); expect(mapToPresentationError).toHaveBeenCalledWith(serviceError); }); it('should provide a static execute method', async () => { const apiDto = { some: 'dashboard-data' }; const viewData = { transformed: 'dashboard-view' } as any; mockServiceInstance.getDashboardOverview.mockResolvedValue(Result.ok(apiDto)); (DashboardViewDataBuilder.build as any).mockReturnValue(viewData); const result = await DashboardPageQuery.execute(); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual(viewData); }); });