view data fixes
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { GetTrackImagePageQuery } from './GetTrackImagePageQuery';
|
||||
import { MediaService } from '@/lib/services/media/MediaService';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { TrackImageViewDataBuilder } from '@/lib/builders/view-data/TrackImageViewDataBuilder';
|
||||
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/services/media/MediaService', () => ({
|
||||
MediaService: vi.fn(class {
|
||||
getTrackImage = vi.fn();
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/builders/view-data/TrackImageViewDataBuilder', () => ({
|
||||
TrackImageViewDataBuilder: {
|
||||
build: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
||||
mapToPresentationError: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('GetTrackImagePageQuery', () => {
|
||||
let query: GetTrackImagePageQuery;
|
||||
let mockServiceInstance: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
query = new GetTrackImagePageQuery();
|
||||
mockServiceInstance = {
|
||||
getTrackImage: vi.fn(),
|
||||
};
|
||||
(MediaService as any).mockImplementation(function () {
|
||||
return mockServiceInstance;
|
||||
});
|
||||
});
|
||||
|
||||
it('should return view data when service succeeds', async () => {
|
||||
const params = { trackId: 'track-123' };
|
||||
const apiDto = { url: 'image-url', data: 'base64-data' };
|
||||
const viewData = { url: 'image-url', data: 'base64-data' };
|
||||
|
||||
mockServiceInstance.getTrackImage.mockResolvedValue(Result.ok(apiDto));
|
||||
(TrackImageViewDataBuilder.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.getTrackImage).toHaveBeenCalledWith('track-123');
|
||||
expect(TrackImageViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
||||
});
|
||||
|
||||
it('should return mapped presentation error when service fails', async () => {
|
||||
const params = { trackId: 'track-123' };
|
||||
const serviceError = { type: 'notFound' };
|
||||
const presentationError = 'notFound';
|
||||
|
||||
mockServiceInstance.getTrackImage.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 = { trackId: 'track-123' };
|
||||
|
||||
mockServiceInstance.getTrackImage.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 = { trackId: 'track-123' };
|
||||
const apiDto = { url: 'image-url', data: 'base64-data' };
|
||||
const viewData = { url: 'image-url', data: 'base64-data' };
|
||||
|
||||
mockServiceInstance.getTrackImage.mockResolvedValue(Result.ok(apiDto));
|
||||
(TrackImageViewDataBuilder.build as any).mockReturnValue(viewData);
|
||||
|
||||
const result = await GetTrackImagePageQuery.execute(params);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(viewData);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user