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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user