79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { TeamLeaderboardPageQuery } from './TeamLeaderboardPageQuery';
|
|
import { TeamService } from '@/lib/services/teams/TeamService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { TeamSummaryViewModel } from '@/lib/view-models/TeamSummaryViewModel';
|
|
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
|
|
// Mock dependencies
|
|
const mockGetAllTeams = vi.fn();
|
|
vi.mock('@/lib/services/teams/TeamService', () => {
|
|
return {
|
|
TeamService: class {
|
|
getAllTeams = mockGetAllTeams;
|
|
},
|
|
};
|
|
});
|
|
|
|
vi.mock('@/lib/view-models/TeamSummaryViewModel', () => {
|
|
const MockVm = vi.fn().mockImplementation(function(data) {
|
|
return {
|
|
...data,
|
|
equals: vi.fn(),
|
|
};
|
|
});
|
|
return { TeamSummaryViewModel: MockVm };
|
|
});
|
|
|
|
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
|
mapToPresentationError: vi.fn(),
|
|
}));
|
|
|
|
describe('TeamLeaderboardPageQuery', () => {
|
|
let query: TeamLeaderboardPageQuery;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockServiceInstance = {
|
|
getAllTeams: mockGetAllTeams,
|
|
};
|
|
query = new TeamLeaderboardPageQuery(mockServiceInstance);
|
|
});
|
|
|
|
it('should return view data when service succeeds', async () => {
|
|
const apiDto = [{ id: 'team-1', name: 'Test Team' }];
|
|
|
|
mockServiceInstance.getAllTeams.mockResolvedValue(Result.ok(apiDto));
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap().teams[0]).toMatchObject({ id: 'team-1', name: 'Test Team' });
|
|
expect(mockServiceInstance.getAllTeams).toHaveBeenCalled();
|
|
expect(TeamSummaryViewModel).toHaveBeenCalledWith({ id: 'team-1', name: 'Test Team' });
|
|
});
|
|
|
|
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');
|
|
});
|
|
}); |