view data fixes
This commit is contained in:
117
apps/website/lib/page-queries/ProfileLeaguesPageQuery.test.ts
Normal file
117
apps/website/lib/page-queries/ProfileLeaguesPageQuery.test.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { ProfileLeaguesPageQuery } from './ProfileLeaguesPageQuery';
|
||||
import { SessionGateway } from '@/lib/gateways/SessionGateway';
|
||||
import { ProfileLeaguesService } from '@/lib/services/leagues/ProfileLeaguesService';
|
||||
import { ProfileLeaguesViewDataBuilder } from '@/lib/builders/view-data/ProfileLeaguesViewDataBuilder';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { mapToPresentationError } from '@/lib/contracts/page-queries/PresentationError';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/gateways/SessionGateway', () => ({
|
||||
SessionGateway: vi.fn().mockImplementation(function() {
|
||||
return { getSession: vi.fn() };
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/services/leagues/ProfileLeaguesService', () => ({
|
||||
ProfileLeaguesService: vi.fn().mockImplementation(function() {
|
||||
return { getProfileLeagues: vi.fn() };
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/builders/view-data/ProfileLeaguesViewDataBuilder', () => ({
|
||||
ProfileLeaguesViewDataBuilder: {
|
||||
build: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/contracts/page-queries/PresentationError', () => ({
|
||||
mapToPresentationError: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('ProfileLeaguesPageQuery', () => {
|
||||
let query: ProfileLeaguesPageQuery;
|
||||
let mockSessionGateway: any;
|
||||
let mockService: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
query = new ProfileLeaguesPageQuery();
|
||||
|
||||
mockSessionGateway = {
|
||||
getSession: vi.fn(),
|
||||
};
|
||||
(SessionGateway as any).mockImplementation(function() { return mockSessionGateway; });
|
||||
|
||||
mockService = {
|
||||
getProfileLeagues: vi.fn(),
|
||||
};
|
||||
(ProfileLeaguesService as any).mockImplementation(function() { return mockService; });
|
||||
});
|
||||
|
||||
it('should return notFound if no session exists', async () => {
|
||||
mockSessionGateway.getSession.mockResolvedValue(null);
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('notFound');
|
||||
});
|
||||
|
||||
it('should return notFound if session has no primaryDriverId', async () => {
|
||||
mockSessionGateway.getSession.mockResolvedValue({ user: {} });
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('notFound');
|
||||
});
|
||||
|
||||
it('should return view data when service succeeds', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const apiDto = { leagues: [] };
|
||||
const viewData = { leagues: [] };
|
||||
|
||||
mockSessionGateway.getSession.mockResolvedValue({ user: { primaryDriverId: driverId } });
|
||||
mockService.getProfileLeagues.mockResolvedValue(Result.ok(apiDto));
|
||||
(ProfileLeaguesViewDataBuilder.build as any).mockReturnValue(viewData);
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(viewData);
|
||||
expect(mockService.getProfileLeagues).toHaveBeenCalledWith(driverId);
|
||||
expect(ProfileLeaguesViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
||||
});
|
||||
|
||||
it('should return mapped presentation error when service fails', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const serviceError = 'someError';
|
||||
const presentationError = 'serverError';
|
||||
|
||||
mockSessionGateway.getSession.mockResolvedValue({ user: { primaryDriverId: driverId } });
|
||||
mockService.getProfileLeagues.mockResolvedValue(Result.err(serviceError));
|
||||
(mapToPresentationError as any).mockReturnValue(presentationError);
|
||||
|
||||
const result = await query.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe(presentationError);
|
||||
expect(mapToPresentationError).toHaveBeenCalledWith(serviceError);
|
||||
});
|
||||
|
||||
it('should provide a static execute method', async () => {
|
||||
const driverId = 'driver-123';
|
||||
const apiDto = { leagues: [] };
|
||||
const viewData = { leagues: [] };
|
||||
|
||||
mockSessionGateway.getSession.mockResolvedValue({ user: { primaryDriverId: driverId } });
|
||||
mockService.getProfileLeagues.mockResolvedValue(Result.ok(apiDto));
|
||||
(ProfileLeaguesViewDataBuilder.build as any).mockReturnValue(viewData);
|
||||
|
||||
const result = await ProfileLeaguesPageQuery.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(viewData);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user