Skip to content

Latest commit

 

History

History
1090 lines (769 loc) · 26.9 KB

File metadata and controls

1090 lines (769 loc) · 26.9 KB

🧪 El-Form React Hooks Testing Strategy

Comprehensive testing checklist for el-form-react-hooks package
Total Features: 23 categories | Individual Test Cases: 100+

This document provides a systematic approach to testing all features of the el-form-react-hooks package. Features are grouped logically to allow efficient testing of related functionality.


🎯 Testing Progress Summary

✅ CONFIRMED WORKING (Through BasicValidationTest):

  • Core Setup: Hook initialization, field registration, default values, TypeScript support
  • Validation: Zod schema validation, real-time onChange validation, cross-field validation, error handling
  • Field Registration: All input types (text, number, select, checkbox, textarea), automatic type coercion
  • State Management: Form state tracking, dirty state detection, real-time updates
  • Watch System: watch(), watch(fieldName), conditional rendering, real-time subscriptions
  • Array Operations: addArrayItem, removeArrayItem, nested arrays (skills.projects), complex structures
  • Form Submission: handleSubmit, validation before submit, canSubmit(), submission state
  • Reset Operations: Basic reset() functionality, state clearing
  • Complex Data: Nested objects, array indexing, deep validation, mixed notation support
  • File Upload Support: File input registration, validation, management, preview, Zod integration

🔄 PARTIALLY TESTED:

  • 🔄 Advanced Validation: Only onChange mode tested, need onBlur, onSubmit, manual modes
  • 🔄 Form Control: Basic functionality confirmed, need setValue/setValues testing
  • 🔄 Error Management: Display working, need programmatic setError/clearErrors testing

❌ NOT YET TESTED:

  • Context Integration: FormProvider, useFormContext patterns
  • Focus Management: setFocus() methods
  • State Persistence: Snapshots, field state queries
  • Edge Cases: Error boundaries, invalid inputs, memory leaks

📋 Quick Reference

Category Features Priority Can Test Together
Core Setup Hook initialization, field registration 🔴 Critical ✅ Yes
Validation All validation modes and types 🔴 Critical ✅ Yes
State Management Form state, dirty/touched tracking 🔴 Critical ✅ Yes
Form Control Value setting, errors, reset 🟡 High ✅ Yes
Advanced Features Watch, submission, arrays, files 🟡 High ✅ Partially
File Upload Support File inputs, validation, preview 🟡 High ✅ Yes
Context Integration FormProvider, reusability patterns 🟢 Medium ✅ Yes
Performance Optimization, edge cases 🟢 Medium ✅ Yes

🎯 Category 1: Core Hook Features

1.1 Basic Form Setup & Initialization

Test Together: All initialization features in one test suite

  • useForm hook instantiation

    • Basic hook call with no options
    • Hook call with all options provided
    • TypeScript generic type inference
    • Default values assignment
  • Initial form state verification

    • formState.values matches defaultValues
    • formState.errors is empty object
    • formState.touched is empty object
    • formState.isSubmitting is false
    • formState.isValid initial state
    • formState.isDirty is false
  • Hook options handling

    • defaultValues object processing
    • validators configuration
    • fieldValidators configuration
    • mode setting (onChange, onBlur, onSubmit, all)
    • validateOn option override
    • onSubmit callback registration

1.2 Field Registration & Input Handling

Test Together: All register() functionality

  • register() function behavior

    • Returns correct props object structure
    • name property correctly set
    • onChange handler function provided
    • onBlur handler function provided
  • Input type handling

    • Text inputs: returns value property
    • Checkbox inputs: returns checked property
    • Textarea inputs: value handling
    • Number inputs: value type conversion
  • Field value management

    • Initial field values from defaultValues
    • Empty/undefined field value handling
    • Nested field registration (dot notation)
    • Field value retrieval and display
  • Event handler functionality

    • onChange updates form state correctly
    • onBlur marks field as touched
    • Event object parameter handling
    • Synthetic event compatibility

Category 2: Validation Features

2.1 Validation Timing & Modes

Test Together: Validation timing scenarios

  • onChange validation

    • Validates on every keystroke when enabled
    • Does not validate when disabled
    • Error display in real-time
    • Performance with rapid changes
  • onBlur validation

    • Validates when field loses focus
    • Does not validate on focus
    • Touched state interaction
    • Multiple field blur sequence
  • onSubmit validation

    • Validates all fields on form submission
    • Prevents submission if invalid
    • Shows all errors at once
    • Does not validate during typing
  • Manual validation (validateOn: "manual")

    • No automatic validation
    • Only validates when trigger() called
    • Form remains in initial state
    • Custom validation logic support
  • Mode combinations

    • mode: "all" enables all validation
    • validateOn overrides mode setting
    • Complex validation timing scenarios
    • Mode switching behavior

2.2 Validation Types & Configuration

Test Together: Different validation approaches

  • Schema validation

    • Zod schema validation
    • Yup schema validation
    • Custom schema libraries
    • Schema error message parsing
  • Field-level validators

    • Individual field validation rules
    • Override schema validation
    • Custom validation functions
    • Async field validation
  • Custom validation functions

    • Function-based validation
    • Multiple validation rules per field
    • Conditional validation logic
    • Cross-field validation
  • No validation mode

    • Forms without any validation
    • Schema-agnostic operation
    • Pure state management
    • Performance without validation
  • Error handling

    • Error message format
    • Multiple errors per field
    • Error persistence
    • Error clearing logic

2.3 Manual Validation Control

Test Together: Programmatic validation

  • trigger() - validate all fields

    • Returns promise with boolean result
    • Validates all registered fields
    • Updates formState.errors
    • Updates formState.isValid
  • trigger(fieldName) - single field

    • Validates only specified field
    • Returns field-specific result
    • Does not affect other fields
    • Type safety with field names
  • trigger([fields]) - multiple fields

    • Validates specified fields only
    • Array of field names handling
    • Partial validation results
    • Type safety with field array
  • Async validation handling

    • Promise resolution timing
    • Loading states during validation
    • Error handling for failed validation
    • Concurrent validation calls

📊 Category 3: Form State Management

3.1 Form State Queries

Test Together: State inspection methods

  • formState object access

    • Real-time state updates
    • Immutable state handling
    • Reference stability
    • Re-render triggers
  • hasErrors() method

    • Returns true when errors exist
    • Returns false when no errors
    • Updates with validation changes
    • Performance with large forms
  • getErrorCount() method

    • Accurate error counting
    • Updates with error changes
    • Zero count handling
    • Nested error counting
  • isFieldValid(name) method

    • Field-specific validity check
    • Returns inverse of error state
    • Updates with field validation
    • Non-existent field handling

3.2 Dirty State Tracking

Test Together: Change detection system

  • isDirty() - entire form

    • Detects any form changes
    • Initial state (not dirty)
    • After value changes (dirty)
    • After reset (not dirty)
  • isDirty(fieldName) - specific field

    • Field-specific dirty detection
    • Compares to initial value
    • Deep equality checking
    • Nested field support
  • isFieldDirty(name) method

    • Alternative dirty check syntax
    • Consistent with isDirty(fieldName)
    • String field name parameter
    • Type safety considerations
  • getDirtyFields() method

    • Returns object of dirty fields
    • Boolean values for dirty state
    • Efficient dirty field tracking
    • Large form performance
  • hasChanges() method

    • Global change detection
    • Alias for isDirty() check
    • Navigation warning scenarios
    • Form persistence decisions
  • getChanges() method

    • Returns only modified fields
    • Partial object with changes
    • API PATCH request preparation
    • Deep comparison accuracy

3.3 Touched State Management

Test Together: User interaction tracking

  • isFieldTouched(name) method

    • Tracks field focus/blur events
    • Initial state (not touched)
    • After blur event (touched)
    • Persistent touched state
  • getTouchedFields() method

    • Returns all touched fields
    • Object with boolean values
    • Touch state persistence
    • Form submission interaction
  • markFieldTouched(name) method

    • Programmatically mark as touched
    • Show validation errors
    • Custom interaction handling
    • Bulk touch operations
  • markFieldUntouched(name) method

    • Remove touched state
    • Hide validation errors
    • Reset field interaction
    • Conditional touch states
  • markAllTouched() method

    • Mark all fields as touched
    • Pre-submission validation display
    • Bulk state changes
    • Performance with many fields

🎛️ Category 4: Form Control & Manipulation

4.1 Value Setting & Updates

Test Together: Programmatic value control

  • setValue(path, value) method

    • Single field value setting
    • Nested path support (user.profile.name)
    • Array index paths (items[0].name)
    • Type conversion handling
  • setValues(values) method

    • Multiple field value setting
    • Partial object updates
    • Nested object support
    • Merge with existing values
  • Nested value handling

    • Deep object structures
    • Dot notation path parsing
    • Array index notation
    • Mixed nested/array paths
  • Value update side effects

    • Dirty state updates
    • Error clearing on change
    • Re-render triggering
    • Watch system notifications

4.2 Error Management

Test Together: Error state control

  • setError(name, error) method

    • Custom error setting
    • Server-side error display
    • Override validation errors
    • Type-safe field names
  • clearErrors() method

    • Clear all form errors
    • Reset validation state
    • Error-free form state
    • Performance considerations
  • clearErrors(name) method

    • Clear specific field error
    • Selective error removal
    • Field-level error control
    • Partial error clearing
  • Error persistence logic

    • When errors are cleared
    • When errors persist
    • Validation vs manual errors
    • Error priority handling

4.3 Form Reset Operations

Test Together: Form state reset functionality

  • reset() - basic reset

    • Restore default values
    • Clear all errors
    • Clear touched state
    • Reset dirty state
  • reset(options) - advanced reset

    • keepErrors option behavior
    • keepTouched option behavior
    • keepDirty option behavior
    • Option combinations
  • resetValues(values) method

    • Reset with new default values
    • Complete state clearing
    • New baseline establishment
    • Different from setValue
  • resetField(name) method

    • Single field reset
    • Field-specific state clearing
    • Selective form reset
    • Nested field reset

🚀 Category 5: Advanced Features

5.1 Watch System

Test Together: Value observation system

  • watch() - all values

    • Return complete form values
    • Real-time value updates
    • Object reference stability
    • Performance with subscriptions
  • watch(fieldName) - specific field

    • Single field value watching
    • Type-safe field access
    • Value change notifications
    • Undefined field handling
  • watch([fields]) - multiple fields

    • Array of field names
    • Subset value watching
    • Type-safe field arrays
    • Performance optimization
  • Watch subscription behavior

    • Re-render optimization
    • Memory leak prevention
    • Subscription cleanup
    • Conditional watching

5.2 Form Submission

Test Together: Submission workflow

  • handleSubmit(onValid, onError)

    • Form event handling
    • Validation before submission
    • Success callback execution
    • Error callback execution
  • submit() - programmatic submission

    • Direct form submission
    • Requires onSubmit handler
    • Validation integration
    • Promise-based operation
  • submitAsync() - async submission

    • Returns submission results
    • Success/failure discrimination
    • Data and error handling
    • Type-safe result objects
  • canSubmit() method

    • Submission readiness check
    • Validation state consideration
    • Loading state checking
    • UI control integration
  • Submission state management

    • isSubmitting state tracking

    • Loading UI support

    • Submission prevention

    • Error state handling

    • Form event handling

    • Validation before submission

    • Success callback execution

    • Error callback execution

  • submit() - programmatic submission

    • Direct form submission
    • Requires onSubmit handler
    • Validation integration
    • Promise-based operation
  • submitAsync() - async submission

    • Returns submission results
    • Success/failure discrimination
    • Data and error handling
    • Type-safe result objects
  • canSubmit() method

    • Submission readiness check
    • Validation state consideration
    • Loading state checking
    • UI control integration
  • Submission state management

    • isSubmitting state tracking
    • Loading UI support
    • Submission prevention
    • Error state handling

5.3 Array Operations

Test Together: Dynamic array handling

  • addArrayItem(path, item)

    • Add items to form arrays
    • Nested array path support
    • Dynamic list management
    • Type preservation
  • removeArrayItem(path, index)

    • Remove items by index
    • Array bounds checking
    • Index shifting handling
    • State consistency
  • Array path handling

    • Dot notation for arrays
    • Bracket notation support
    • Nested array structures
    • Array within objects
  • Array dirty state tracking

    • Array modification detection
    • Item-level dirty tracking
    • Efficient change detection
    • Performance optimization

5.4 Focus Management

Test Together: Programmatic focus control

  • setFocus(name) method

    • Programmatic field focusing
    • DOM element targeting
    • Focus event triggering
    • Error field focusing
  • setFocus(name, options) method

    • shouldSelect option behavior
    • Text selection on focus
    • Focus with custom options
    • Input type compatibility
  • Field reference management

    • DOM element tracking
    • Reference cleanup
    • Dynamic field handling
    • Memory management

🔄 Category 6: State Persistence & History

6.1 Form Snapshots & Persistence

Test Together: State backup and restore

  • getSnapshot() method

    • Complete state capture
    • Values, errors, touched state
    • Timestamp generation
    • Dirty state inclusion
  • restoreSnapshot(snapshot)

    • State restoration
    • Dirty state recalculation
    • Timestamp validation
    • Partial snapshot handling
  • Snapshot use cases

    • Auto-save functionality
    • Undo/redo implementation
    • Form drafts
    • State persistence

6.2 Field State Queries

Test Together: Detailed field inspection

  • getFieldState(name) method

    • Complete field state object
    • isDirty, isTouched, error properties
    • Field-level state access
    • Non-existent field handling
  • Field state consistency

    • State synchronization
    • Cross-method consistency
    • State update propagation
    • Performance optimization

🧩 Category 7: Context & Component Integration

7.1 Form Context Pattern

Test Together: Context-based form sharing

  • FormProvider component

    • Form instance provision
    • Child component access
    • TypeScript generic preservation
    • Multiple form support
  • useFormContext() hook

    • Context access in components
    • Type-safe form retrieval
    • Error handling for missing context
    • Nested provider support
  • useFormState() convenience hook

    • Direct form instance access
    • Simplified context usage
    • Alternative to useFormContext
    • Performance considerations

7.2 Component Reusability Patterns

Test Together: Form component patterns

  • Context pattern integration

    • Reusable field components
    • Form state sharing
    • Component composition
    • Type safety preservation
  • Form passing pattern

    • Explicit form prop passing
    • Component testability
    • Clear dependencies
    • Prop-based form access
  • Hybrid pattern support

    • Components working both ways
    • Optional context usage
    • Fallback mechanisms
    • Flexible API design

Category 8: Performance & Optimization

8.1 Render Optimization

Test Together: Performance characteristics

  • useCallback optimization

    • Method memoization
    • Dependency array accuracy
    • Re-render prevention
    • Performance measurement
  • Minimal re-renders

    • State change isolation
    • Component update optimization
    • Unnecessary render prevention
    • Large form performance
  • Efficient dirty tracking

    • Set-based dirty field tracking
    • O(1) dirty operations
    • Memory usage optimization
    • Comparison algorithm efficiency
  • Memory leak prevention

    • Event listener cleanup
    • Reference management
    • Component unmounting
    • Subscription cleanup

8.2 Nested & Complex Data

Test Together: Complex data structure handling

  • Nested object handling

    • Deep object property access
    • Dot notation parsing
    • Immutable updates
    • Type preservation
  • Array index notation

    • Bracket notation support
    • Index validation
    • Dynamic array access
    • Mixed notation support
  • Complex data validation

    • Deep object validation
    • Array item validation
    • Nested structure support
    • Performance with complexity

📁 Category 9: File Upload Support

9.1 File Input Registration

Test Together: Basic file input handling

  • register() function for file inputs

    • Detects type="file" automatically
    • Returns files property instead of value
    • Handles single file selection
    • Handles multiple file selection (multiple attribute)
  • File object access

    • Returns FileList object for multiple files
    • Returns single File object for single files
    • Provides access to file properties (name, size, type, lastModified)
    • Handles empty file selection (null/undefined)
  • File state management

    • File selection updates form state
    • File removal clears form state
    • File replacement updates existing selection
    • Files persist through form operations

9.2 File Validation

Test Together: File validation scenarios

  • File type validation

    • Accept attribute validation
    • MIME type checking
    • File extension validation
    • Custom file type validators
  • File size validation

    • Maximum file size limits
    • Minimum file size limits
    • Total size limits for multiple files
    • Human-readable size error messages
  • File count validation

    • Maximum number of files
    • Minimum number of files
    • Single file enforcement
    • Multiple file count limits
  • File content validation

    • File reading for content validation
    • Image dimension validation
    • File header validation
    • Async file content checks

9.3 File Operations

Test Together: File manipulation methods

  • addFile(name, file) method

    • Add file to existing selection
    • Append to multiple file inputs
    • Validate before adding
    • Update form state correctly
  • removeFile(name, index) method

    • Remove specific file by index
    • Remove all files
    • Update indices correctly
    • Maintain file order
  • replaceFile(name, index, file) method

    • Replace specific file
    • Validate replacement file
    • Maintain other files
    • Update form state
  • clearFiles(name) method

    • Clear all selected files
    • Reset file input state
    • Update form validation
    • Trigger change events

9.4 File Preview & Management

Test Together: File display utilities

  • getFilePreview(file) utility

    • Generate image previews
    • Handle different file types
    • Base64 data URL generation
    • Preview cleanup on file removal
  • getFileInfo(file) utility

    • Format file size display
    • Extract file metadata
    • File type categorization
    • Upload progress tracking
  • File drag & drop support

    • Drop zone integration
    • Drag over visual feedback
    • File type filtering on drop
    • Multiple file drop handling

9.5 Zod Schema Integration

Test Together: Schema-based file validation

  • z.instanceof(File) validation

    • Basic File type checking
    • File refinements for size validation
    • File refinements for type validation
    • Custom error messages
  • Array of files validation

    • z.array(z.instanceof(File)) support
    • Array refinements for file validation
    • Min/max file count validation
    • Mixed validation (files + other fields)
  • Progressive validation

    • onChange validation with partial schemas
    • onSubmit validation with full schemas
    • Real-time error display
    • Validation state management

9.6 File Upload Integration

Test Together: Upload workflow

  • File upload progress tracking

    • Upload progress percentage
    • Individual file progress
    • Batch upload progress
    • Error handling during upload
  • Upload state management

    • isUploading state tracking
    • Upload completion status
    • Upload error states
    • Retry upload functionality
  • File upload utilities

    • FormData preparation
    • Multipart form encoding
    • Upload cancellation
    • Upload resume support

🛡️ Category 10: Edge Cases & Error Handling

10.1 Error Boundaries & Edge Cases

Test Together: Robustness testing

  • Invalid field names

    • Non-existent field handling
    • Malformed path strings
    • Empty field names
    • Special character handling
  • Undefined/null values

    • Null defaultValues handling
    • Undefined field values
    • Null validation results
    • Empty object handling
  • Missing validation schema

    • No schema provided
    • Invalid schema objects
    • Schema parsing errors
    • Fallback behavior
  • Async validation errors

    • Network failure handling
    • Timeout scenarios
    • Concurrent validation
    • Error state management
  • Component lifecycle

    • Unmounting during async operations
    • Memory leak prevention
    • State cleanup
    • Event handler cleanup

10.2 Integration Testing

Test Together: Real-world scenarios

  • Complete form workflows

    • User registration forms
    • Multi-step forms
    • Complex validation scenarios
    • Submission flows
  • Popular library integration

    • Zod schema integration
    • Yup schema integration
    • React Hook Form migration
    • Formik comparison
  • Performance under load

    • Large form handling (100+ fields)
    • Rapid user input
    • Concurrent operations
    • Memory usage monitoring
  • Real browser testing

    • Cross-browser compatibility
    • Mobile device testing
    • Accessibility compliance
    • Screen reader support

📝 Testing Implementation Notes

Efficient Test Grouping

✅ Test Together (Same Test Suite):

  • All core initialization features (1.1)
  • All register() functionality (1.2)
  • All validation timing scenarios (2.1)
  • All state query methods (3.1)
  • All dirty state methods (3.2)
  • All touched state methods (3.3)

⚠️ Test Separately (Different Test Suites):

  • Performance tests (separate test runner)
  • Integration tests (require full setup)
  • Async validation (timeout handling)
  • Edge cases (error simulation)

Priority Testing Order

  1. 🔴 Critical Path (Test First)

    • Core Setup (1.1, 1.2)
    • Basic Validation (2.1, 2.2)
    • Form State (3.1, 3.2, 3.3)
  2. 🟡 High Priority (Test Second)

    • Form Control (4.1, 4.2, 4.3)
    • Advanced Features (5.1, 5.2)
    • File Upload Support (9.1, 9.2, 9.3)
  3. 🟢 Medium Priority (Test Last)

    • File Upload Utilities (9.4, 9.5)
    • Context Integration (7.1, 7.2)
    • Performance (8.1, 8.2)
    • Edge Cases (10.1, 10.2)

Test Data Preparation

// Common test form schemas
const simpleUserSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  age: z.number().min(18),
});

const complexNestedSchema = z.object({
  user: z.object({
    profile: z.object({
      name: z.string(),
      settings: z.object({
        notifications: z.boolean(),
      }),
    }),
  }),
  items: z.array(
    z.object({
      title: z.string(),
      tags: z.array(z.string()),
    })
  ),
});

// Default test values
const defaultValues = {
  name: "",
  email: "",
  age: 18,
};

🎯 Success Criteria

✅ Feature Complete: All 100+ test cases passing
✅ Performance: < 16ms for typical operations
✅ Memory: No memory leaks in long-running forms
✅ TypeScript: 100% type coverage
✅ Compatibility: Works with React 16.8+ through 18+
✅ Integration: Works with popular validation libraries


This testing strategy ensures comprehensive coverage of all el-form-react-hooks features while optimizing testing efficiency through logical grouping and prioritization.