view data fixes
This commit is contained in:
79
apps/website/lib/page-queries/TeamsPageQuery.test.ts
Normal file
79
apps/website/lib/page-queries/TeamsPageQuery.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user