view data fixes
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { CompleteOnboardingMutation } from './CompleteOnboardingMutation';
|
||||
import { OnboardingService } from '@/lib/services/onboarding/OnboardingService';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/services/onboarding/OnboardingService', () => {
|
||||
return {
|
||||
OnboardingService: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe('CompleteOnboardingMutation', () => {
|
||||
let mutation: CompleteOnboardingMutation;
|
||||
let mockServiceInstance: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockServiceInstance = {
|
||||
completeOnboarding: vi.fn(),
|
||||
};
|
||||
// Use mockImplementation to return the instance
|
||||
(OnboardingService as any).mockImplementation(function() {
|
||||
return mockServiceInstance;
|
||||
});
|
||||
mutation = new CompleteOnboardingMutation();
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should successfully complete onboarding with valid input', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
timezone: 'America/New_York',
|
||||
bio: 'Test bio',
|
||||
};
|
||||
const mockResult = { success: true };
|
||||
mockServiceInstance.completeOnboarding.mockResolvedValue(Result.ok(mockResult));
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledWith(command);
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should successfully complete onboarding with minimal input', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const mockResult = { success: true };
|
||||
mockServiceInstance.completeOnboarding.mockResolvedValue(Result.ok(mockResult));
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledWith({
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
timezone: undefined,
|
||||
bio: undefined,
|
||||
});
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('failure modes', () => {
|
||||
it('should handle service failure during onboarding completion', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const serviceError = new Error('Service error');
|
||||
mockServiceInstance.completeOnboarding.mockRejectedValue(serviceError);
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('onboardingFailed');
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle service returning error result', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const domainError = { type: 'serverError', message: 'Database connection failed' };
|
||||
mockServiceInstance.completeOnboarding.mockResolvedValue(Result.err(domainError));
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('onboardingFailed');
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle service returning validation error', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const domainError = { type: 'validationError', message: 'Display name taken' };
|
||||
mockServiceInstance.completeOnboarding.mockResolvedValue(Result.err(domainError));
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('onboardingFailed');
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle service returning notFound error', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const domainError = { type: 'notFound', message: 'User not found' };
|
||||
mockServiceInstance.completeOnboarding.mockResolvedValue(Result.err(domainError));
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('onboardingFailed');
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('error mapping', () => {
|
||||
it('should map various domain errors to mutation errors', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const testCases = [
|
||||
{ domainError: { type: 'notFound' }, expectedError: 'onboardingFailed' },
|
||||
{ domainError: { type: 'unauthorized' }, expectedError: 'onboardingFailed' },
|
||||
{ domainError: { type: 'validationError' }, expectedError: 'onboardingFailed' },
|
||||
{ domainError: { type: 'serverError' }, expectedError: 'onboardingFailed' },
|
||||
{ domainError: { type: 'networkError' }, expectedError: 'onboardingFailed' },
|
||||
{ domainError: { type: 'notImplemented' }, expectedError: 'onboardingFailed' },
|
||||
{ domainError: { type: 'unknown' }, expectedError: 'onboardingFailed' },
|
||||
];
|
||||
|
||||
for (const testCase of testCases) {
|
||||
mockServiceInstance.completeOnboarding.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 input', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const mockResult = { success: true };
|
||||
mockServiceInstance.completeOnboarding.mockResolvedValue(Result.ok(mockResult));
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockServiceInstance.completeOnboarding).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('service instantiation', () => {
|
||||
it('should create OnboardingService instance', () => {
|
||||
// Arrange & Act
|
||||
const mutation = new CompleteOnboardingMutation();
|
||||
|
||||
// Assert
|
||||
expect(mutation).toBeInstanceOf(CompleteOnboardingMutation);
|
||||
});
|
||||
});
|
||||
|
||||
describe('result shape', () => {
|
||||
it('should return void on success', async () => {
|
||||
// Arrange
|
||||
const command = {
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
displayName: 'johndoe',
|
||||
country: 'US',
|
||||
};
|
||||
const mockResult = { success: true };
|
||||
mockServiceInstance.completeOnboarding.mockResolvedValue(Result.ok(mockResult));
|
||||
|
||||
// Act
|
||||
const result = await mutation.execute(command);
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,30 +1,52 @@
|
||||
/**
|
||||
* Complete Onboarding Mutation
|
||||
*
|
||||
* Framework-agnostic mutation for completing onboarding.
|
||||
* Called from Server Actions.
|
||||
*
|
||||
* Pattern: Server Action → Mutation → Service → API Client
|
||||
*/
|
||||
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { Mutation } from '@/lib/contracts/mutations/Mutation';
|
||||
import { mapToMutationError } from '@/lib/contracts/mutations/MutationError';
|
||||
import { OnboardingService } from '@/lib/services/onboarding/OnboardingService';
|
||||
import { CompleteOnboardingInputDTO } from '@/lib/types/generated/CompleteOnboardingInputDTO';
|
||||
import { CompleteOnboardingViewDataBuilder } from '@/lib/builders/view-data/CompleteOnboardingViewDataBuilder';
|
||||
import { CompleteOnboardingViewData } from '@/lib/builders/view-data/CompleteOnboardingViewData';
|
||||
import type { Mutation } from '@/lib/contracts/mutations/Mutation';
|
||||
|
||||
export class CompleteOnboardingMutation implements Mutation<CompleteOnboardingInputDTO, CompleteOnboardingViewData, string> {
|
||||
async execute(params: CompleteOnboardingInputDTO): Promise<Result<CompleteOnboardingViewData, string>> {
|
||||
const onboardingService = new OnboardingService();
|
||||
const result = await onboardingService.completeOnboarding(params);
|
||||
|
||||
if (result.isErr()) {
|
||||
return Result.err(mapToMutationError(result.getError()));
|
||||
}
|
||||
|
||||
const output = CompleteOnboardingViewDataBuilder.build(result.unwrap());
|
||||
return Result.ok(output);
|
||||
export interface CompleteOnboardingCommand {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
displayName: string;
|
||||
country: string;
|
||||
timezone?: string;
|
||||
bio?: string;
|
||||
}
|
||||
|
||||
export type CompleteOnboardingMutationError = 'onboardingFailed';
|
||||
|
||||
export class CompleteOnboardingMutation
|
||||
implements
|
||||
Mutation<
|
||||
CompleteOnboardingCommand,
|
||||
void,
|
||||
CompleteOnboardingMutationError
|
||||
>
|
||||
{
|
||||
private readonly service: OnboardingService;
|
||||
|
||||
constructor() {
|
||||
this.service = new OnboardingService();
|
||||
}
|
||||
}
|
||||
|
||||
async execute(
|
||||
command: CompleteOnboardingCommand,
|
||||
): Promise<Result<void, CompleteOnboardingMutationError>> {
|
||||
try {
|
||||
const result = await this.service.completeOnboarding({
|
||||
firstName: command.firstName,
|
||||
lastName: command.lastName,
|
||||
displayName: command.displayName,
|
||||
country: command.country,
|
||||
timezone: command.timezone,
|
||||
bio: command.bio,
|
||||
});
|
||||
|
||||
if (result.isErr()) {
|
||||
return Result.err('onboardingFailed');
|
||||
}
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
return Result.err('onboardingFailed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { GenerateAvatarsMutation } from './GenerateAvatarsMutation';
|
||||
import { OnboardingService } from '@/lib/services/onboarding/OnboardingService';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { GenerateAvatarsViewDataBuilder } from '@/lib/builders/view-data/GenerateAvatarsViewDataBuilder';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('@/lib/services/onboarding/OnboardingService', () => {
|
||||
return {
|
||||
OnboardingService: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('@/lib/builders/view-data/GenerateAvatarsViewDataBuilder', () => ({
|
||||
GenerateAvatarsViewDataBuilder: {
|
||||
build: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/contracts/mutations/MutationError', () => ({
|
||||
mapToMutationError: vi.fn((err) => err.type || 'unknown'),
|
||||
}));
|
||||
|
||||
describe('GenerateAvatarsMutation', () => {
|
||||
let mutation: GenerateAvatarsMutation;
|
||||
let mockServiceInstance: any;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mutation = new GenerateAvatarsMutation();
|
||||
mockServiceInstance = {
|
||||
generateAvatars: vi.fn(),
|
||||
};
|
||||
// Use mockImplementation to return the instance
|
||||
(OnboardingService as any).mockImplementation(function() {
|
||||
return mockServiceInstance;
|
||||
});
|
||||
});
|
||||
|
||||
it('should return success result when service succeeds', async () => {
|
||||
const input = { prompt: 'test prompt' };
|
||||
const serviceOutput = { success: true, avatarUrls: ['url1'] };
|
||||
const viewData = { success: true, avatarUrls: ['url1'], errorMessage: undefined };
|
||||
|
||||
mockServiceInstance.generateAvatars.mockResolvedValue(Result.ok(serviceOutput));
|
||||
(GenerateAvatarsViewDataBuilder.build as any).mockReturnValue(viewData);
|
||||
|
||||
const result = await mutation.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toEqual(viewData);
|
||||
expect(mockServiceInstance.generateAvatars).toHaveBeenCalledWith(input);
|
||||
expect(GenerateAvatarsViewDataBuilder.build).toHaveBeenCalledWith(serviceOutput);
|
||||
});
|
||||
|
||||
it('should return error result when service fails', async () => {
|
||||
const input = { prompt: 'test prompt' };
|
||||
const domainError = { type: 'notImplemented', message: 'Not implemented' };
|
||||
|
||||
mockServiceInstance.generateAvatars.mockResolvedValue(Result.err(domainError));
|
||||
|
||||
const result = await mutation.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.getError()).toBe('notImplemented');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user