86 lines
3.1 KiB
TypeScript
86 lines
3.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { LeagueSettingsPageQuery } from './LeagueSettingsPageQuery';
|
|
import { LeagueSettingsService } from '@/lib/services/leagues/LeagueSettingsService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { LeagueSettingsViewDataBuilder } from '@/lib/builders/view-data/LeagueSettingsViewDataBuilder';
|
|
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/leagues/LeagueSettingsService', () => ({
|
|
LeagueSettingsService: vi.fn().mockImplementation(function (this: any) {
|
|
this.getSettingsData = vi.fn();
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/lib/builders/view-data/LeagueSettingsViewDataBuilder', () => ({
|
|
LeagueSettingsViewDataBuilder: {
|
|
build: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
|
mapToPresentationError: vi.fn(),
|
|
}));
|
|
|
|
describe('LeagueSettingsPageQuery', () => {
|
|
let query: LeagueSettingsPageQuery;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new LeagueSettingsPageQuery();
|
|
mockServiceInstance = {
|
|
getSettingsData: vi.fn(),
|
|
};
|
|
(LeagueSettingsService as any).mockImplementation(function () {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
it('should return view data when service succeeds', async () => {
|
|
const leagueId = 'league-123';
|
|
const apiDto = { id: leagueId, name: 'Test League' };
|
|
const viewData = { id: leagueId, name: 'Test League' };
|
|
|
|
mockServiceInstance.getSettingsData.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueSettingsViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(LeagueSettingsService).toHaveBeenCalled();
|
|
expect(mockServiceInstance.getSettingsData).toHaveBeenCalledWith(leagueId);
|
|
expect(LeagueSettingsViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
|
});
|
|
|
|
it('should return mapped presentation error when service fails', async () => {
|
|
const leagueId = 'league-123';
|
|
const serviceError = { type: 'notFound' };
|
|
const presentationError = 'notFound';
|
|
|
|
mockServiceInstance.getSettingsData.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 = { id: leagueId, name: 'Test League' };
|
|
const viewData = { id: leagueId, name: 'Test League' };
|
|
|
|
mockServiceInstance.getSettingsData.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueSettingsViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await LeagueSettingsPageQuery.execute(leagueId);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
});
|
|
});
|