|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + act, |
| 4 | + fireEvent, |
| 5 | + render, |
| 6 | + screen, |
| 7 | + waitFor, |
| 8 | +} from '@testing-library/react'; |
| 9 | + |
| 10 | +import { useForm } from '../../useForm'; |
| 11 | + |
| 12 | +jest.useFakeTimers(); |
| 13 | + |
| 14 | +describe('resetDefaultValues', () => { |
| 15 | + it('should update default values and recompute dirtyFields/isDirty without changing form values', async () => { |
| 16 | + let formRef: ReturnType< |
| 17 | + typeof useForm<{ firstName: string; lastName: string }> |
| 18 | + >; |
| 19 | + |
| 20 | + const App = () => { |
| 21 | + const methods = useForm({ |
| 22 | + defaultValues: { firstName: 'John', lastName: 'Doe' }, |
| 23 | + }); |
| 24 | + formRef = methods; |
| 25 | + const { |
| 26 | + register, |
| 27 | + formState: { isDirty, dirtyFields }, |
| 28 | + } = methods; |
| 29 | + return ( |
| 30 | + <form> |
| 31 | + <input {...register('firstName')} placeholder="firstName" /> |
| 32 | + <input {...register('lastName')} placeholder="lastName" /> |
| 33 | + <p data-testid="isDirty">{isDirty ? 'dirty' : 'clean'}</p> |
| 34 | + <p data-testid="dirtyFields">{JSON.stringify(dirtyFields)}</p> |
| 35 | + </form> |
| 36 | + ); |
| 37 | + }; |
| 38 | + |
| 39 | + render(<App />); |
| 40 | + |
| 41 | + // Change firstName so it's dirty |
| 42 | + fireEvent.change(screen.getByPlaceholderText('firstName'), { |
| 43 | + target: { value: 'Jane' }, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(screen.getByTestId('isDirty').textContent).toBe('dirty'); |
| 47 | + expect(screen.getByTestId('dirtyFields').textContent).toBe( |
| 48 | + '{"firstName":true}', |
| 49 | + ); |
| 50 | + |
| 51 | + // Reset default values to match the current "Jane" value |
| 52 | + act(() => { |
| 53 | + formRef.resetDefaultValues({ firstName: 'Jane', lastName: 'Doe' }); |
| 54 | + }); |
| 55 | + |
| 56 | + // The input value should not change |
| 57 | + expect( |
| 58 | + (screen.getByPlaceholderText('firstName') as HTMLInputElement).value, |
| 59 | + ).toBe('Jane'); |
| 60 | + |
| 61 | + // isDirty and dirtyFields should be recomputed: firstName is no longer dirty |
| 62 | + expect(screen.getByTestId('isDirty').textContent).toBe('clean'); |
| 63 | + expect(screen.getByTestId('dirtyFields').textContent).toBe('{}'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should keep dirty state when keepDirty option is true', async () => { |
| 67 | + let formRef: ReturnType<typeof useForm<{ firstName: string }>>; |
| 68 | + |
| 69 | + const App = () => { |
| 70 | + const methods = useForm({ defaultValues: { firstName: 'John' } }); |
| 71 | + formRef = methods; |
| 72 | + const { |
| 73 | + register, |
| 74 | + formState: { isDirty, dirtyFields }, |
| 75 | + } = methods; |
| 76 | + return ( |
| 77 | + <form> |
| 78 | + <input {...register('firstName')} placeholder="firstName" /> |
| 79 | + <p data-testid="isDirty">{isDirty ? 'dirty' : 'clean'}</p> |
| 80 | + <p data-testid="dirtyFields">{JSON.stringify(dirtyFields)}</p> |
| 81 | + </form> |
| 82 | + ); |
| 83 | + }; |
| 84 | + |
| 85 | + render(<App />); |
| 86 | + |
| 87 | + fireEvent.change(screen.getByPlaceholderText('firstName'), { |
| 88 | + target: { value: 'Jane' }, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(screen.getByTestId('isDirty').textContent).toBe('dirty'); |
| 92 | + |
| 93 | + // Reset defaults to the new value but keep dirty state |
| 94 | + act(() => { |
| 95 | + formRef.resetDefaultValues({ firstName: 'Jane' }, { keepDirty: true }); |
| 96 | + }); |
| 97 | + |
| 98 | + // Input value should not change |
| 99 | + expect( |
| 100 | + (screen.getByPlaceholderText('firstName') as HTMLInputElement).value, |
| 101 | + ).toBe('Jane'); |
| 102 | + |
| 103 | + // Dirty state should be preserved because keepDirty: true |
| 104 | + expect(screen.getByTestId('isDirty').textContent).toBe('dirty'); |
| 105 | + expect(screen.getByTestId('dirtyFields').textContent).toBe( |
| 106 | + '{"firstName":true}', |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should update formState.defaultValues when resetDefaultValues is called', async () => { |
| 111 | + let formRef: ReturnType<typeof useForm<{ name: string }>>; |
| 112 | + |
| 113 | + const App = () => { |
| 114 | + const methods = useForm({ defaultValues: { name: 'initial' } }); |
| 115 | + formRef = methods; |
| 116 | + const { |
| 117 | + register, |
| 118 | + formState: { defaultValues }, |
| 119 | + } = methods; |
| 120 | + return ( |
| 121 | + <form> |
| 122 | + <input {...register('name')} /> |
| 123 | + <p data-testid="defaultValues">{JSON.stringify(defaultValues)}</p> |
| 124 | + </form> |
| 125 | + ); |
| 126 | + }; |
| 127 | + |
| 128 | + render(<App />); |
| 129 | + |
| 130 | + expect(screen.getByTestId('defaultValues').textContent).toBe( |
| 131 | + '{"name":"initial"}', |
| 132 | + ); |
| 133 | + |
| 134 | + act(() => { |
| 135 | + formRef.resetDefaultValues({ name: 'updated' }); |
| 136 | + }); |
| 137 | + |
| 138 | + expect(screen.getByTestId('defaultValues').textContent).toBe( |
| 139 | + '{"name":"updated"}', |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should work with nested objects', async () => { |
| 144 | + let formRef: ReturnType< |
| 145 | + typeof useForm<{ user: { name: string; age: number } }> |
| 146 | + >; |
| 147 | + |
| 148 | + const App = () => { |
| 149 | + const methods = useForm({ |
| 150 | + defaultValues: { user: { name: 'John', age: 30 } }, |
| 151 | + }); |
| 152 | + formRef = methods; |
| 153 | + const { |
| 154 | + register, |
| 155 | + formState: { isDirty, dirtyFields }, |
| 156 | + } = methods; |
| 157 | + return ( |
| 158 | + <form> |
| 159 | + <input {...register('user.name')} placeholder="name" /> |
| 160 | + <input {...register('user.age')} placeholder="age" type="number" /> |
| 161 | + <p data-testid="isDirty">{isDirty ? 'dirty' : 'clean'}</p> |
| 162 | + <p data-testid="dirtyFields">{JSON.stringify(dirtyFields)}</p> |
| 163 | + </form> |
| 164 | + ); |
| 165 | + }; |
| 166 | + |
| 167 | + render(<App />); |
| 168 | + |
| 169 | + fireEvent.change(screen.getByPlaceholderText('name'), { |
| 170 | + target: { value: 'Jane' }, |
| 171 | + }); |
| 172 | + |
| 173 | + expect(screen.getByTestId('dirtyFields').textContent).toContain( |
| 174 | + '"name":true', |
| 175 | + ); |
| 176 | + |
| 177 | + // Reset defaults to the current user value (submitted state) |
| 178 | + act(() => { |
| 179 | + formRef.resetDefaultValues({ user: { name: 'Jane', age: 30 } }); |
| 180 | + }); |
| 181 | + |
| 182 | + expect(screen.getByTestId('isDirty').textContent).toBe('clean'); |
| 183 | + expect(screen.getByTestId('dirtyFields').textContent).toBe('{}'); |
| 184 | + |
| 185 | + // Input values should not change |
| 186 | + expect( |
| 187 | + (screen.getByPlaceholderText('name') as HTMLInputElement).value, |
| 188 | + ).toBe('Jane'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should recompute dirty fields correctly when some fields still differ from new defaults', async () => { |
| 192 | + let formRef: ReturnType< |
| 193 | + typeof useForm<{ a: string; b: string; c: string }> |
| 194 | + >; |
| 195 | + |
| 196 | + const App = () => { |
| 197 | + const methods = useForm({ |
| 198 | + defaultValues: { a: '1', b: '2', c: '3' }, |
| 199 | + }); |
| 200 | + formRef = methods; |
| 201 | + const { |
| 202 | + register, |
| 203 | + formState: { isDirty, dirtyFields }, |
| 204 | + } = methods; |
| 205 | + return ( |
| 206 | + <form> |
| 207 | + <input {...register('a')} placeholder="a" /> |
| 208 | + <input {...register('b')} placeholder="b" /> |
| 209 | + <input {...register('c')} placeholder="c" /> |
| 210 | + <p data-testid="isDirty">{isDirty ? 'dirty' : 'clean'}</p> |
| 211 | + <p data-testid="dirtyFields">{JSON.stringify(dirtyFields)}</p> |
| 212 | + </form> |
| 213 | + ); |
| 214 | + }; |
| 215 | + |
| 216 | + render(<App />); |
| 217 | + |
| 218 | + // Dirty all three fields |
| 219 | + fireEvent.change(screen.getByPlaceholderText('a'), { |
| 220 | + target: { value: 'A' }, |
| 221 | + }); |
| 222 | + fireEvent.change(screen.getByPlaceholderText('b'), { |
| 223 | + target: { value: 'B' }, |
| 224 | + }); |
| 225 | + fireEvent.change(screen.getByPlaceholderText('c'), { |
| 226 | + target: { value: 'C' }, |
| 227 | + }); |
| 228 | + |
| 229 | + await waitFor(() => |
| 230 | + expect(screen.getByTestId('dirtyFields').textContent).toBe( |
| 231 | + '{"a":true,"b":true,"c":true}', |
| 232 | + ), |
| 233 | + ); |
| 234 | + |
| 235 | + // Reset defaults so that 'a' matches the current value ('A'), |
| 236 | + // but 'b' and 'c' still differ |
| 237 | + act(() => { |
| 238 | + formRef.resetDefaultValues({ a: 'A', b: '2', c: '3' }); |
| 239 | + }); |
| 240 | + |
| 241 | + // 'a' is no longer dirty, 'b' and 'c' remain dirty |
| 242 | + expect(screen.getByTestId('isDirty').textContent).toBe('dirty'); |
| 243 | + expect(JSON.parse(screen.getByTestId('dirtyFields').textContent!)).toEqual({ |
| 244 | + b: true, |
| 245 | + c: true, |
| 246 | + }); |
| 247 | + }); |
| 248 | +}); |
0 commit comments