view data tests
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { SignupViewDataBuilder } from './SignupViewDataBuilder';
|
||||
import type { SignupPageDTO } from '@/lib/services/auth/types/SignupPageDTO';
|
||||
|
||||
describe('SignupViewDataBuilder', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should transform SignupPageDTO to SignupViewData correctly', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result).toEqual({
|
||||
returnTo: '/dashboard',
|
||||
formState: {
|
||||
fields: {
|
||||
firstName: { value: '', error: undefined, touched: false, validating: false },
|
||||
lastName: { value: '', error: undefined, touched: false, validating: false },
|
||||
email: { value: '', error: undefined, touched: false, validating: false },
|
||||
password: { value: '', error: undefined, touched: false, validating: false },
|
||||
confirmPassword: { value: '', error: undefined, touched: false, validating: false },
|
||||
},
|
||||
isValid: true,
|
||||
isSubmitting: false,
|
||||
submitError: undefined,
|
||||
submitCount: 0,
|
||||
},
|
||||
isSubmitting: false,
|
||||
submitError: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty returnTo path', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('');
|
||||
});
|
||||
|
||||
it('should handle returnTo with query parameters', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard?welcome=true',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('/dashboard?welcome=true');
|
||||
});
|
||||
});
|
||||
|
||||
describe('data transformation', () => {
|
||||
it('should preserve all DTO fields in the output', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe(signupPageDTO.returnTo);
|
||||
});
|
||||
|
||||
it('should not modify the input DTO', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const originalDTO = { ...signupPageDTO };
|
||||
SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(signupPageDTO).toEqual(originalDTO);
|
||||
});
|
||||
|
||||
it('should initialize all signup form fields with default values', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.formState.fields.firstName.value).toBe('');
|
||||
expect(result.formState.fields.firstName.error).toBeUndefined();
|
||||
expect(result.formState.fields.firstName.touched).toBe(false);
|
||||
expect(result.formState.fields.firstName.validating).toBe(false);
|
||||
|
||||
expect(result.formState.fields.lastName.value).toBe('');
|
||||
expect(result.formState.fields.lastName.error).toBeUndefined();
|
||||
expect(result.formState.fields.lastName.touched).toBe(false);
|
||||
expect(result.formState.fields.lastName.validating).toBe(false);
|
||||
|
||||
expect(result.formState.fields.email.value).toBe('');
|
||||
expect(result.formState.fields.email.error).toBeUndefined();
|
||||
expect(result.formState.fields.email.touched).toBe(false);
|
||||
expect(result.formState.fields.email.validating).toBe(false);
|
||||
|
||||
expect(result.formState.fields.password.value).toBe('');
|
||||
expect(result.formState.fields.password.error).toBeUndefined();
|
||||
expect(result.formState.fields.password.touched).toBe(false);
|
||||
expect(result.formState.fields.password.validating).toBe(false);
|
||||
|
||||
expect(result.formState.fields.confirmPassword.value).toBe('');
|
||||
expect(result.formState.fields.confirmPassword.error).toBeUndefined();
|
||||
expect(result.formState.fields.confirmPassword.touched).toBe(false);
|
||||
expect(result.formState.fields.confirmPassword.validating).toBe(false);
|
||||
});
|
||||
|
||||
it('should initialize form state with default values', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.formState.isValid).toBe(true);
|
||||
expect(result.formState.isSubmitting).toBe(false);
|
||||
expect(result.formState.submitError).toBeUndefined();
|
||||
expect(result.formState.submitCount).toBe(0);
|
||||
});
|
||||
|
||||
it('should initialize UI state flags correctly', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.isSubmitting).toBe(false);
|
||||
expect(result.submitError).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
it('should handle returnTo with encoded characters', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard?redirect=%2Fadmin',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('/dashboard?redirect=%2Fadmin');
|
||||
});
|
||||
|
||||
it('should handle returnTo with hash fragment', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard#section',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('/dashboard#section');
|
||||
});
|
||||
});
|
||||
|
||||
describe('form state structure', () => {
|
||||
it('should have all required form fields', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
expect(result.formState.fields).toHaveProperty('firstName');
|
||||
expect(result.formState.fields).toHaveProperty('lastName');
|
||||
expect(result.formState.fields).toHaveProperty('email');
|
||||
expect(result.formState.fields).toHaveProperty('password');
|
||||
expect(result.formState.fields).toHaveProperty('confirmPassword');
|
||||
});
|
||||
|
||||
it('should have consistent field state structure', () => {
|
||||
const signupPageDTO: SignupPageDTO = {
|
||||
returnTo: '/dashboard',
|
||||
};
|
||||
|
||||
const result = SignupViewDataBuilder.build(signupPageDTO);
|
||||
|
||||
const fields = result.formState.fields;
|
||||
Object.values(fields).forEach((field) => {
|
||||
expect(field).toHaveProperty('value');
|
||||
expect(field).toHaveProperty('error');
|
||||
expect(field).toHaveProperty('touched');
|
||||
expect(field).toHaveProperty('validating');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user