view data fixes
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 7m11s
Contract Testing / contract-snapshot (pull_request) Has been skipped

This commit is contained in:
2026-01-24 23:29:55 +01:00
parent c1750a33dd
commit 1b0a1f4aee
134 changed files with 10380 additions and 415 deletions

View File

@@ -0,0 +1,105 @@
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');
});
});