import { describe, it, expect, vi, beforeEach } from 'vitest'; import { GetCategoryIconPageQuery } from './GetCategoryIconPageQuery'; import { MediaService } from '@/lib/services/media/MediaService'; import { Result } from '@/lib/contracts/Result'; import { CategoryIconViewDataBuilder } from '@/lib/builders/view-data/CategoryIconViewDataBuilder'; import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError'; // Mock dependencies vi.mock('@/lib/services/media/MediaService', () => ({ MediaService: vi.fn(class { getCategoryIcon = vi.fn(); }), })); vi.mock('@/lib/builders/view-data/CategoryIconViewDataBuilder', () => ({ CategoryIconViewDataBuilder: { build: vi.fn(), }, })); vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({ mapToPresentationError: vi.fn(), })); describe('GetCategoryIconPageQuery', () => { let query: GetCategoryIconPageQuery; let mockServiceInstance: any; beforeEach(() => { vi.clearAllMocks(); query = new GetCategoryIconPageQuery(); mockServiceInstance = { getCategoryIcon: vi.fn(), }; (MediaService as any).mockImplementation(function () { return mockServiceInstance; }); }); it('should return view data when service succeeds', async () => { const params = { categoryId: 'category-123' }; const apiDto = { url: 'icon-url', data: 'base64-data' }; const viewData = { url: 'icon-url', data: 'base64-data' }; mockServiceInstance.getCategoryIcon.mockResolvedValue(Result.ok(apiDto)); (CategoryIconViewDataBuilder.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.getCategoryIcon).toHaveBeenCalledWith('category-123'); expect(CategoryIconViewDataBuilder.build).toHaveBeenCalledWith(apiDto); }); it('should return mapped presentation error when service fails', async () => { const params = { categoryId: 'category-123' }; const serviceError = { type: 'notFound' }; const presentationError = 'notFound'; mockServiceInstance.getCategoryIcon.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 = { categoryId: 'category-123' }; mockServiceInstance.getCategoryIcon.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 = { categoryId: 'category-123' }; const apiDto = { url: 'icon-url', data: 'base64-data' }; const viewData = { url: 'icon-url', data: 'base64-data' }; mockServiceInstance.getCategoryIcon.mockResolvedValue(Result.ok(apiDto)); (CategoryIconViewDataBuilder.build as any).mockReturnValue(viewData); const result = await GetCategoryIconPageQuery.execute(params); expect(result.isOk()).toBe(true); expect(result.unwrap()).toEqual(viewData); }); });