101 lines
3.3 KiB
TypeScript
101 lines
3.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { ProfilePageQuery } from './ProfilePageQuery';
|
|
import { SessionGateway } from '@/lib/gateways/SessionGateway';
|
|
import { DriverProfileService } from '@/lib/services/drivers/DriverProfileService';
|
|
import { ProfileViewDataBuilder } from '@/lib/builders/view-data/ProfileViewDataBuilder';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
|
|
// Mock dependencies
|
|
vi.mock('@/lib/gateways/SessionGateway', () => ({
|
|
SessionGateway: vi.fn().mockImplementation(function() {
|
|
return { getSession: vi.fn() };
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/lib/services/drivers/DriverProfileService', () => ({
|
|
DriverProfileService: vi.fn().mockImplementation(function() {
|
|
return { getDriverProfile: vi.fn() };
|
|
}),
|
|
}));
|
|
|
|
vi.mock('@/lib/builders/view-data/ProfileViewDataBuilder', () => ({
|
|
ProfileViewDataBuilder: {
|
|
build: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe('ProfilePageQuery', () => {
|
|
let query: ProfilePageQuery;
|
|
let mockSessionGateway: any;
|
|
let mockService: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
query = new ProfilePageQuery();
|
|
|
|
mockSessionGateway = {
|
|
getSession: vi.fn(),
|
|
};
|
|
(SessionGateway as any).mockImplementation(function() { return mockSessionGateway; });
|
|
|
|
mockService = {
|
|
getDriverProfile: vi.fn(),
|
|
};
|
|
(DriverProfileService 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 = { id: driverId, name: 'Test Driver' };
|
|
const viewData = { id: driverId, name: 'Test Driver' };
|
|
|
|
mockSessionGateway.getSession.mockResolvedValue({ user: { primaryDriverId: driverId } });
|
|
mockService.getDriverProfile.mockResolvedValue(Result.ok(apiDto));
|
|
(ProfileViewDataBuilder.build as any).mockReturnValue(viewData);
|
|
|
|
const result = await query.execute();
|
|
|
|
expect(result.isOk()).toBe(true);
|
|
expect(result.unwrap()).toEqual(viewData);
|
|
expect(mockService.getDriverProfile).toHaveBeenCalledWith(driverId);
|
|
expect(ProfileViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
|
|
});
|
|
|
|
it('should map service errors correctly', async () => {
|
|
const driverId = 'driver-123';
|
|
mockSessionGateway.getSession.mockResolvedValue({ user: { primaryDriverId: driverId } });
|
|
|
|
const errorMappings = [
|
|
{ service: 'notFound', expected: 'notFound' },
|
|
{ service: 'unauthorized', expected: 'unauthorized' },
|
|
{ service: 'serverError', expected: 'serverError' },
|
|
{ service: 'other', expected: 'unknown' },
|
|
];
|
|
|
|
for (const mapping of errorMappings) {
|
|
mockService.getDriverProfile.mockResolvedValue(Result.err(mapping.service));
|
|
const result = await query.execute();
|
|
expect(result.isErr()).toBe(true);
|
|
expect(result.getError()).toBe(mapping.expected);
|
|
}
|
|
});
|
|
});
|