96 lines
3.3 KiB
TypeScript
96 lines
3.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { GetLeagueCoverPageQuery } from './GetLeagueCoverPageQuery';
|
|
import { MediaService } from '@/lib/services/media/MediaService';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { LeagueCoverViewDataBuilder } from '@/lib/builders/view-data/LeagueCoverViewDataBuilder';
|
|
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/services/media/MediaService', () => ({
|
|
MediaService: vi.fn(class {
|
|
getLeagueCover = vi.fn();
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/lib/builders/view-data/LeagueCoverViewDataBuilder', () => ({
|
|
LeagueCoverViewDataBuilder: {
|
|
build: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
|
mapToPresentationError: vi.fn(),
|
|
}));
|
|
|
|
describe('GetLeagueCoverPageQuery', () => {
|
|
let query: GetLeagueCoverPageQuery;
|
|
let mockServiceInstance: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new GetLeagueCoverPageQuery();
|
|
mockServiceInstance = {
|
|
getLeagueCover: vi.fn(),
|
|
};
|
|
(MediaService as any).mockImplementation(function () {
|
|
return mockServiceInstance;
|
|
});
|
|
});
|
|
|
|
it('should return view data when service succeeds', async () => {
|
|
const params = { leagueId: 'league-123' };
|
|
const apiDto = { url: 'cover-url', data: 'base64-data' };
|
|
const viewData = { url: 'cover-url', data: 'base64-data' };
|
|
|
|
mockServiceInstance.getLeagueCover.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueCoverViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute(params);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(MediaService).toHaveBeenCalled();
|
|
expect(mockServiceInstance.getLeagueCover).toHaveBeenCalledWith('league-123');
|
|
expect(LeagueCoverViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
|
});
|
|
|
|
it('should return mapped presentation error when service fails', async () => {
|
|
const params = { leagueId: 'league-123' };
|
|
const serviceError = { type: 'notFound' };
|
|
const presentationError = 'notFound';
|
|
|
|
mockServiceInstance.getLeagueCover.mockResolvedValue(Result.err(serviceError));
|
|
(mapToPresentationError as any).mockReturnValue(presentationError);
|
|
|
|
const result = await query.execute(params);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe(presentationError);
|
|
expect(mapToPresentationError).toHaveBeenCalledWith(serviceError);
|
|
});
|
|
|
|
it('should return serverError on exception', async () => {
|
|
const params = { leagueId: 'league-123' };
|
|
|
|
mockServiceInstance.getLeagueCover.mockRejectedValue(new Error('Network error'));
|
|
|
|
const result = await query.execute(params);
|
|
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe('serverError');
|
|
});
|
|
|
|
it('should provide a static execute method', async () => {
|
|
const params = { leagueId: 'league-123' };
|
|
const apiDto = { url: 'cover-url', data: 'base64-data' };
|
|
const viewData = { url: 'cover-url', data: 'base64-data' };
|
|
|
|
mockServiceInstance.getLeagueCover.mockResolvedValue(Result.ok(apiDto));
|
|
(LeagueCoverViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await GetLeagueCoverPageQuery.execute(params);
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
});
|
|
}); |