view data fixes
This commit is contained in:
105
apps/website/lib/page-queries/DriverProfilePageQuery.test.ts
Normal file
105
apps/website/lib/page-queries/DriverProfilePageQuery.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user