86 lines
3.1 KiB
TypeScript
86 lines
3.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { LeagueStandingsPageQuery } from './LeagueStandingsPageQuery';
|
|
import { LeagueStandingsService } from '@/lib/services/leagues/LeagueStandingsService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { LeagueStandingsViewDataBuilder } from '@/lib/builders/view-data/LeagueStandingsViewDataBuilder';
|
|
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/leagues/LeagueStandingsService', () => ({
|
|
LeagueStandingsService: vi.fn().mockImplementation(function (this: any) {
|
|
this.getStandingsData = vi.fn();
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/lib/builders/view-data/LeagueStandingsViewDataBuilder', () => ({
|
|
LeagueStandingsViewDataBuilder: {
|
|
build: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
|
mapToPresentationError: vi.fn(),
|
|
}));
|
|
|
|
describe('LeagueStandingsPageQuery', () => {
|
|
let query: LeagueStandingsPageQuery;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new LeagueStandingsPageQuery();
|
|
mockServiceInstance = {
|
|
getStandingsData: vi.fn(),
|
|
};
|
|
(LeagueStandingsService as any).mockImplementation(function () {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
it('should return view data when service succeeds', async () => {
|
|
const leagueId = 'league-123';
|
|
const apiDto = { standings: [], memberships: [] };
|
|
const viewData = { standings: [], memberships: [] };
|
|
|
|
mockServiceInstance.getStandingsData.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueStandingsViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(LeagueStandingsService).toHaveBeenCalled();
|
|
expect(mockServiceInstance.getStandingsData).toHaveBeenCalledWith(leagueId);
|
|
expect(LeagueStandingsViewDataBuilder.build).toHaveBeenCalledWith(apiDto.standings, apiDto.memberships, leagueId);
|
|
});
|
|
|
|
it('should return mapped presentation error when service fails', async () => {
|
|
const leagueId = 'league-123';
|
|
const serviceError = { type: 'notFound' };
|
|
const presentationError = 'notFound';
|
|
|
|
mockServiceInstance.getStandingsData.mockResolvedValue(Result.err(serviceError));
|
|
(mapToPresentationError as any).mockReturnValue(presentationError);
|
|
|
|
const result = await query.execute(leagueId);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe(presentationError);
|
|
expect(mapToPresentationError).toHaveBeenCalledWith(serviceError);
|
|
});
|
|
|
|
it('should provide a static execute method', async () => {
|
|
const leagueId = 'league-123';
|
|
const apiDto = { standings: [], memberships: [] };
|
|
const viewData = { standings: [], memberships: [] };
|
|
|
|
mockServiceInstance.getStandingsData.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueStandingsViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await LeagueStandingsPageQuery.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
});
|
|
});
|