view data tests
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { ResetPasswordViewDataBuilder } from './ResetPasswordViewDataBuilder';
|
||||
import type { ResetPasswordPageDTO } from '@/lib/services/auth/types/ResetPasswordPageDTO';
|
||||
|
||||
describe('ResetPasswordViewDataBuilder', () => {
|
||||
describe('happy paths', () => {
|
||||
it('should transform ResetPasswordPageDTO to ResetPasswordViewData correctly', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result).toEqual({
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
showSuccess: false,
|
||||
formState: {
|
||||
fields: {
|
||||
newPassword: { 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 resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('');
|
||||
});
|
||||
|
||||
it('should handle returnTo with query parameters', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login?success=true',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('/login?success=true');
|
||||
});
|
||||
});
|
||||
|
||||
describe('data transformation', () => {
|
||||
it('should preserve all DTO fields in the output', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.token).toBe(resetPasswordPageDTO.token);
|
||||
expect(result.returnTo).toBe(resetPasswordPageDTO.returnTo);
|
||||
});
|
||||
|
||||
it('should not modify the input DTO', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const originalDTO = { ...resetPasswordPageDTO };
|
||||
ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(resetPasswordPageDTO).toEqual(originalDTO);
|
||||
});
|
||||
|
||||
it('should initialize form fields with default values', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.formState.fields.newPassword.value).toBe('');
|
||||
expect(result.formState.fields.newPassword.error).toBeUndefined();
|
||||
expect(result.formState.fields.newPassword.touched).toBe(false);
|
||||
expect(result.formState.fields.newPassword.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 resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
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 resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.showSuccess).toBe(false);
|
||||
expect(result.isSubmitting).toBe(false);
|
||||
expect(result.submitError).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
it('should handle token with special characters', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc-123_def.456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.token).toBe('abc-123_def.456');
|
||||
});
|
||||
|
||||
it('should handle token with URL-encoded characters', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc%20123%40def',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.token).toBe('abc%20123%40def');
|
||||
});
|
||||
|
||||
it('should handle returnTo with encoded characters', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login?redirect=%2Fdashboard',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('/login?redirect=%2Fdashboard');
|
||||
});
|
||||
|
||||
it('should handle returnTo with hash fragment', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login#section',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.returnTo).toBe('/login#section');
|
||||
});
|
||||
});
|
||||
|
||||
describe('form state structure', () => {
|
||||
it('should have all required form fields', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
expect(result.formState.fields).toHaveProperty('newPassword');
|
||||
expect(result.formState.fields).toHaveProperty('confirmPassword');
|
||||
});
|
||||
|
||||
it('should have consistent field state structure', () => {
|
||||
const resetPasswordPageDTO: ResetPasswordPageDTO = {
|
||||
token: 'abc123def456',
|
||||
returnTo: '/login',
|
||||
};
|
||||
|
||||
const result = ResetPasswordViewDataBuilder.build(resetPasswordPageDTO);
|
||||
|
||||
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