Files
gridpilot.gg/apps/website/lib/builders/view-data/CompleteOnboardingViewDataBuilder.test.ts
Marc Mintel 1f4f837282
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 5m58s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data tests
2026-01-22 18:06:46 +01:00

176 lines
5.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { CompleteOnboardingViewDataBuilder } from './CompleteOnboardingViewDataBuilder';
import type { CompleteOnboardingOutputDTO } from '@/lib/types/generated/CompleteOnboardingOutputDTO';
describe('CompleteOnboardingViewDataBuilder', () => {
describe('happy paths', () => {
it('should transform successful onboarding completion DTO to ViewData correctly', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: true,
driverId: 'driver-123',
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result).toEqual({
success: true,
driverId: 'driver-123',
errorMessage: undefined,
});
});
it('should handle onboarding completion with error message', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: false,
driverId: undefined,
errorMessage: 'Failed to complete onboarding',
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result).toEqual({
success: false,
driverId: undefined,
errorMessage: 'Failed to complete onboarding',
});
});
it('should handle onboarding completion with only success field', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: true,
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result).toEqual({
success: true,
driverId: undefined,
errorMessage: undefined,
});
});
});
describe('data transformation', () => {
it('should preserve all DTO fields in the output', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: true,
driverId: 'driver-123',
errorMessage: undefined,
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result.success).toBe(apiDto.success);
expect(result.driverId).toBe(apiDto.driverId);
expect(result.errorMessage).toBe(apiDto.errorMessage);
});
it('should not modify the input DTO', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: true,
driverId: 'driver-123',
errorMessage: undefined,
};
const originalDto = { ...apiDto };
CompleteOnboardingViewDataBuilder.build(apiDto);
expect(apiDto).toEqual(originalDto);
});
});
describe('edge cases', () => {
it('should handle false success value', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: false,
driverId: undefined,
errorMessage: 'Error occurred',
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result.success).toBe(false);
expect(result.driverId).toBeUndefined();
expect(result.errorMessage).toBe('Error occurred');
});
it('should handle empty string error message', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: false,
driverId: undefined,
errorMessage: '',
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result.success).toBe(false);
expect(result.errorMessage).toBe('');
});
it('should handle very long driverId', () => {
const longDriverId = 'driver-' + 'a'.repeat(1000);
const apiDto: CompleteOnboardingOutputDTO = {
success: true,
driverId: longDriverId,
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result.driverId).toBe(longDriverId);
});
it('should handle special characters in error message', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: false,
driverId: undefined,
errorMessage: 'Error: "Failed to create driver" (code: 500)',
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result.errorMessage).toBe('Error: "Failed to create driver" (code: 500)');
});
});
describe('derived fields calculation', () => {
it('should calculate isSuccessful derived field correctly', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: true,
driverId: 'driver-123',
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
// Note: The builder doesn't add derived fields, but we can verify the structure
expect(result.success).toBe(true);
expect(result.driverId).toBe('driver-123');
});
it('should handle success with no driverId', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: true,
driverId: undefined,
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result.success).toBe(true);
expect(result.driverId).toBeUndefined();
});
it('should handle failure with driverId', () => {
const apiDto: CompleteOnboardingOutputDTO = {
success: false,
driverId: 'driver-123',
errorMessage: 'Partial failure',
};
const result = CompleteOnboardingViewDataBuilder.build(apiDto);
expect(result.success).toBe(false);
expect(result.driverId).toBe('driver-123');
expect(result.errorMessage).toBe('Partial failure');
});
});
});