Files
gridpilot.gg/apps/website/lib/page-queries/DashboardPageQuery.test.ts
Marc Mintel 1b0a1f4aee
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 23:29:55 +01:00

83 lines
2.9 KiB
TypeScript

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);
});
});