Files
gridpilot.gg/apps/website/lib/page-queries/DriverProfilePageQuery.test.ts
Marc Mintel 1b0a1f4aee
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 23:29:55 +01:00

106 lines
3.6 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { DriverProfilePageQuery } from './DriverProfilePageQuery';
import { DriverProfilePageService } from '@/lib/services/drivers/DriverProfilePageService';
import { Result } from '@/lib/contracts/Result';
import { DriverProfileViewDataBuilder } from '@/lib/builders/view-data/DriverProfileViewDataBuilder';
// Mock dependencies
vi.mock('@/lib/services/drivers/DriverProfilePageService', () => ({
DriverProfilePageService: vi.fn().mockImplementation(function() {
return { getDriverProfile: vi.fn() };
}),
}));
vi.mock('@/lib/builders/view-data/DriverProfileViewDataBuilder', () => ({
DriverProfileViewDataBuilder: {
build: vi.fn(),
},
}));
describe('DriverProfilePageQuery', () => {
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
mockServiceInstance = {
getDriverProfile: vi.fn(),
};
(DriverProfilePageService as any).mockImplementation(function() { return mockServiceInstance; });
});
it('should return view data when driverId is provided and service succeeds', async () => {
const driverId = 'driver-123';
const apiDto = { id: driverId, name: 'Test Driver' };
const viewData = { id: driverId, name: 'Test Driver' };
mockServiceInstance.getDriverProfile.mockResolvedValue(Result.ok(apiDto));
(DriverProfileViewDataBuilder.build as any).mockReturnValue(viewData);
const result = await DriverProfilePageQuery.execute(driverId);
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toEqual(viewData);
expect(DriverProfilePageService).toHaveBeenCalled();
expect(mockServiceInstance.getDriverProfile).toHaveBeenCalledWith(driverId);
expect(DriverProfileViewDataBuilder.build).toHaveBeenCalledWith(apiDto);
});
it('should return NotFound error when driverId is null', async () => {
const result = await DriverProfilePageQuery.execute(null);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('NotFound');
});
it('should return NotFound error when driverId is empty string', async () => {
const result = await DriverProfilePageQuery.execute('');
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('NotFound');
});
it('should return NotFound error when service returns notFound', async () => {
const driverId = 'driver-123';
mockServiceInstance.getDriverProfile.mockResolvedValue(Result.err('notFound'));
const result = await DriverProfilePageQuery.execute(driverId);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('NotFound');
});
it('should return Unauthorized error when service returns unauthorized', async () => {
const driverId = 'driver-123';
mockServiceInstance.getDriverProfile.mockResolvedValue(Result.err('unauthorized'));
const result = await DriverProfilePageQuery.execute(driverId);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Unauthorized');
});
it('should return Error for other service errors', async () => {
const driverId = 'driver-123';
mockServiceInstance.getDriverProfile.mockResolvedValue(Result.err('serverError'));
const result = await DriverProfilePageQuery.execute(driverId);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Error');
});
it('should return Error on exception', async () => {
const driverId = 'driver-123';
mockServiceInstance.getDriverProfile.mockRejectedValue(new Error('Unexpected error'));
const result = await DriverProfilePageQuery.execute(driverId);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('Error');
});
});