|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { renderHook, act } from '@testing-library/react'; |
| 3 | +import { useFormValidation } from '../../hooks/useFormValidation'; |
| 4 | + |
| 5 | +describe('useFormValidation Hook', () => { |
| 6 | + const initialValues = { |
| 7 | + name: '', |
| 8 | + email: '', |
| 9 | + password: '' |
| 10 | + }; |
| 11 | + |
| 12 | + const validationRules = { |
| 13 | + name: { |
| 14 | + required: true, |
| 15 | + requiredMessage: 'Name is required', |
| 16 | + validator: (value) => value.length >= 3, |
| 17 | + message: 'Name must be at least 3 characters' |
| 18 | + }, |
| 19 | + email: { |
| 20 | + required: true, |
| 21 | + requiredMessage: 'Email is required', |
| 22 | + validator: (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value), |
| 23 | + message: 'Please enter a valid email address' |
| 24 | + }, |
| 25 | + password: { |
| 26 | + required: true, |
| 27 | + requiredMessage: 'Password is required', |
| 28 | + validator: (value) => value.length >= 8, |
| 29 | + message: 'Password must be at least 8 characters' |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + const onSubmit = vi.fn(); |
| 34 | + |
| 35 | + it('should initialize with the provided values', () => { |
| 36 | + const { result } = renderHook(() => |
| 37 | + useFormValidation(initialValues, validationRules, onSubmit) |
| 38 | + ); |
| 39 | + |
| 40 | + expect(result.current.values).toEqual(initialValues); |
| 41 | + expect(result.current.errors).toEqual({}); |
| 42 | + expect(result.current.touched).toEqual({}); |
| 43 | + expect(result.current.isSubmitting).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should update values when handleChange is called', () => { |
| 47 | + const { result } = renderHook(() => |
| 48 | + useFormValidation(initialValues, validationRules, onSubmit) |
| 49 | + ); |
| 50 | + |
| 51 | + act(() => { |
| 52 | + result.current.handleChange({ |
| 53 | + target: { name: 'name', value: 'John Doe' } |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + expect(result.current.values.name).toBe('John Doe'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should mark field as touched when handleBlur is called', () => { |
| 61 | + const { result } = renderHook(() => |
| 62 | + useFormValidation(initialValues, validationRules, onSubmit) |
| 63 | + ); |
| 64 | + |
| 65 | + act(() => { |
| 66 | + result.current.handleBlur({ |
| 67 | + target: { name: 'name' } |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + expect(result.current.touched.name).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should validate required fields', () => { |
| 75 | + const { result } = renderHook(() => |
| 76 | + useFormValidation(initialValues, validationRules, onSubmit) |
| 77 | + ); |
| 78 | + |
| 79 | + // Trigger validation by attempting to submit |
| 80 | + act(() => { |
| 81 | + result.current.handleSubmit({ preventDefault: vi.fn() }); |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result.current.errors.name).toBe('Name is required'); |
| 85 | + expect(result.current.errors.email).toBe('Email is required'); |
| 86 | + expect(result.current.errors.password).toBe('Password is required'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should validate field format when values are provided', () => { |
| 90 | + const { result } = renderHook(() => |
| 91 | + useFormValidation( |
| 92 | + { ...initialValues, name: 'Jo', email: 'invalid-email', password: 'short' }, |
| 93 | + validationRules, |
| 94 | + onSubmit |
| 95 | + ) |
| 96 | + ); |
| 97 | + |
| 98 | + // Trigger validation by attempting to submit |
| 99 | + act(() => { |
| 100 | + result.current.handleSubmit({ preventDefault: vi.fn() }); |
| 101 | + }); |
| 102 | + |
| 103 | + expect(result.current.errors.name).toBe('Name must be at least 3 characters'); |
| 104 | + expect(result.current.errors.email).toBe('Please enter a valid email address'); |
| 105 | + expect(result.current.errors.password).toBe('Password must be at least 8 characters'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should call onSubmit when form is valid', async () => { |
| 109 | + const validValues = { |
| 110 | + name: 'John Doe', |
| 111 | + email: 'john@example.com', |
| 112 | + password: 'password123' |
| 113 | + }; |
| 114 | + |
| 115 | + const { result } = renderHook(() => |
| 116 | + useFormValidation(validValues, validationRules, onSubmit) |
| 117 | + ); |
| 118 | + |
| 119 | + await act(async () => { |
| 120 | + await result.current.handleSubmit({ preventDefault: vi.fn() }); |
| 121 | + }); |
| 122 | + |
| 123 | + expect(onSubmit).toHaveBeenCalledWith(validValues); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should set isSubmitting during form submission', async () => { |
| 127 | + // Create a delayed onSubmit function |
| 128 | + const delayedOnSubmit = vi.fn().mockImplementation(() => |
| 129 | + new Promise(resolve => setTimeout(resolve, 100)) |
| 130 | + ); |
| 131 | + |
| 132 | + const validValues = { |
| 133 | + name: 'John Doe', |
| 134 | + email: 'john@example.com', |
| 135 | + password: 'password123' |
| 136 | + }; |
| 137 | + |
| 138 | + const { result } = renderHook(() => |
| 139 | + useFormValidation(validValues, validationRules, delayedOnSubmit) |
| 140 | + ); |
| 141 | + |
| 142 | + let submissionPromise; |
| 143 | + |
| 144 | + act(() => { |
| 145 | + submissionPromise = result.current.handleSubmit({ preventDefault: vi.fn() }); |
| 146 | + }); |
| 147 | + |
| 148 | + // Check that isSubmitting is true during submission |
| 149 | + expect(result.current.isSubmitting).toBe(true); |
| 150 | + |
| 151 | + // Wait for submission to complete |
| 152 | + await act(async () => { |
| 153 | + await submissionPromise; |
| 154 | + }); |
| 155 | + |
| 156 | + // Check that isSubmitting is false after submission |
| 157 | + expect(result.current.isSubmitting).toBe(false); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should reset the form when resetForm is called', () => { |
| 161 | + const { result } = renderHook(() => |
| 162 | + useFormValidation( |
| 163 | + { ...initialValues, name: 'John Doe' }, |
| 164 | + validationRules, |
| 165 | + onSubmit |
| 166 | + ) |
| 167 | + ); |
| 168 | + |
| 169 | + // First, make some changes |
| 170 | + act(() => { |
| 171 | + result.current.handleChange({ |
| 172 | + target: { name: 'email', value: 'john@example.com' } |
| 173 | + }); |
| 174 | + |
| 175 | + result.current.handleBlur({ |
| 176 | + target: { name: 'name' } |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + // Then reset the form |
| 181 | + act(() => { |
| 182 | + result.current.resetForm(); |
| 183 | + }); |
| 184 | + |
| 185 | + // Check that everything is reset |
| 186 | + expect(result.current.values).toEqual(initialValues); |
| 187 | + expect(result.current.errors).toEqual({}); |
| 188 | + expect(result.current.touched).toEqual({}); |
| 189 | + expect(result.current.isSubmitting).toBe(false); |
| 190 | + }); |
| 191 | +}); |
0 commit comments