view data fixes
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped

This commit is contained in:
2026-01-24 23:29:55 +01:00
parent c1750a33dd
commit 1b0a1f4aee
134 changed files with 10380 additions and 415 deletions

View File

@@ -0,0 +1,79 @@
/* eslint-disable gridpilot-rules/page-query-filename, gridpilot-rules/single-export-per-file, @typescript-eslint/no-explicit-any */
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { TeamsPageQuery } from './TeamsPageQuery';
import { TeamService } from '@/lib/services/teams/TeamService';
import { Result } from '@/lib/contracts/Result';
import { TeamsViewDataBuilder } from '@/lib/builders/view-data/TeamsViewDataBuilder';
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
// Mock dependencies
vi.mock('@/lib/services/teams/TeamService', () => ({
TeamService: vi.fn().mockImplementation(function (this: any) {
this.getAllTeams = vi.fn();
}),
}));
vi.mock('@/lib/builders/view-data/TeamsViewDataBuilder', () => ({
TeamsViewDataBuilder: {
build: vi.fn(),
},
}));
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
mapToPresentationError: vi.fn(),
}));
describe('TeamsPageQuery', () => {
let query: TeamsPageQuery;
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
query = new TeamsPageQuery();
mockServiceInstance = {
getAllTeams: vi.fn(),
};
(TeamService as any).mockImplementation(function () {
return mockServiceInstance;
});
});
it('should return view data when service succeeds', async () => {
const apiDto = { teams: [{ id: 'team-1', name: 'Test Team' }] };
const viewData = { teams: [{ id: 'team-1', name: 'Test Team' }] };
mockServiceInstance.getAllTeams.mockResolvedValue(Result.ok(apiDto.teams));
(TeamsViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await query.execute();
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
expect(TeamService).toHaveBeenCalled();
expect(mockServiceInstance.getAllTeams).toHaveBeenCalled();
expect(TeamsViewDataBuilder.build).toHaveBeenCalledWith({ teams: apiDto.teams });
});
it('should return mapped presentation error when service fails', async () => {
const serviceError = { type: 'notFound' };
const presentationError = 'notFound';
mockServiceInstance.getAllTeams.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 return unknown error on exception', async () => {
mockServiceInstance.getAllTeams.mockRejectedValue(new Error('Unexpected error'));
const result = await query.execute();
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('unknown');
});
});