Files
gridpilot.gg/apps/website/lib/mutations/drivers/UpdateDriverProfileMutation.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

246 lines
9.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { UpdateDriverProfileMutation } from './UpdateDriverProfileMutation';
import { DriverProfileUpdateService } from '@/lib/services/drivers/DriverProfileUpdateService';
import { Result } from '@/lib/contracts/Result';
// Mock dependencies
vi.mock('@/lib/services/drivers/DriverProfileUpdateService', () => {
return {
DriverProfileUpdateService: vi.fn(),
};
});
describe('UpdateDriverProfileMutation', () => {
let mutation: UpdateDriverProfileMutation;
let mockServiceInstance: any;
beforeEach(() => {
vi.clearAllMocks();
mockServiceInstance = {
updateProfile: vi.fn(),
};
// Use mockImplementation to return the instance
(DriverProfileUpdateService as any).mockImplementation(function() {
return mockServiceInstance;
});
mutation = new UpdateDriverProfileMutation();
});
describe('execute', () => {
describe('happy paths', () => {
it('should successfully update driver profile with bio and country', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockServiceInstance.updateProfile).toHaveBeenCalledWith({ bio: 'Test bio', country: 'US' });
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
it('should successfully update driver profile with only bio', async () => {
// Arrange
const command = { bio: 'Test bio' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockServiceInstance.updateProfile).toHaveBeenCalledWith({ bio: 'Test bio', country: undefined });
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
it('should successfully update driver profile with only country', async () => {
// Arrange
const command = { country: 'GB' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockServiceInstance.updateProfile).toHaveBeenCalledWith({ bio: undefined, country: 'GB' });
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
it('should successfully update driver profile with empty command', async () => {
// Arrange
const command = {};
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
expect(mockServiceInstance.updateProfile).toHaveBeenCalledWith({ bio: undefined, country: undefined });
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
});
describe('failure modes', () => {
it('should handle service failure during profile update', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
const serviceError = new Error('Service error');
mockServiceInstance.updateProfile.mockRejectedValue(serviceError);
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('DRIVER_PROFILE_UPDATE_FAILED');
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
it('should handle service returning error result', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
const domainError = { type: 'serverError', message: 'Database connection failed' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('DRIVER_PROFILE_UPDATE_FAILED');
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
it('should handle service returning validation error', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
const domainError = { type: 'validationError', message: 'Invalid country code' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('DRIVER_PROFILE_UPDATE_FAILED');
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
it('should handle service returning notFound error', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
const domainError = { type: 'notFound', message: 'Driver not found' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.err(domainError));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe('DRIVER_PROFILE_UPDATE_FAILED');
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
});
describe('error mapping', () => {
it('should map various domain errors to mutation errors', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
const testCases = [
{ domainError: { type: 'notFound' }, expectedError: 'DRIVER_PROFILE_UPDATE_FAILED' },
{ domainError: { type: 'unauthorized' }, expectedError: 'DRIVER_PROFILE_UPDATE_FAILED' },
{ domainError: { type: 'validationError' }, expectedError: 'DRIVER_PROFILE_UPDATE_FAILED' },
{ domainError: { type: 'serverError' }, expectedError: 'DRIVER_PROFILE_UPDATE_FAILED' },
{ domainError: { type: 'networkError' }, expectedError: 'DRIVER_PROFILE_UPDATE_FAILED' },
{ domainError: { type: 'notImplemented' }, expectedError: 'DRIVER_PROFILE_UPDATE_FAILED' },
{ domainError: { type: 'unknown' }, expectedError: 'DRIVER_PROFILE_UPDATE_FAILED' },
];
for (const testCase of testCases) {
mockServiceInstance.updateProfile.mockResolvedValue(Result.err(testCase.domainError));
const result = await mutation.execute(command);
expect(result.isErr()).toBe(true);
expect(result.getError()).toBe(testCase.expectedError);
}
});
});
describe('input validation', () => {
it('should accept valid command input', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.updateProfile).toHaveBeenCalledTimes(1);
});
it('should handle empty bio gracefully', async () => {
// Arrange
const command = { bio: '', country: 'US' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.updateProfile).toHaveBeenCalledWith({ bio: '', country: 'US' });
});
it('should handle empty country gracefully', async () => {
// Arrange
const command = { bio: 'Test bio', country: '' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(mockServiceInstance.updateProfile).toHaveBeenCalledWith({ bio: 'Test bio', country: '' });
});
});
describe('service instantiation', () => {
it('should create DriverProfileUpdateService instance', () => {
// Arrange & Act
const mutation = new UpdateDriverProfileMutation();
// Assert
expect(mutation).toBeInstanceOf(UpdateDriverProfileMutation);
});
});
describe('result shape', () => {
it('should return void on success', async () => {
// Arrange
const command = { bio: 'Test bio', country: 'US' };
mockServiceInstance.updateProfile.mockResolvedValue(Result.ok(undefined));
// Act
const result = await mutation.execute(command);
// Assert
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
});
});
});
});