|
1 |
| -import { useState } from 'react'; |
| 1 | +import { waitFor } from '@storybook/test'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { useEffect, useState } from 'react'; |
2 | 4 |
|
3 | 5 | import { renderWithForm, userEvent, act } from '../../../test/index';
|
4 | 6 | import { Radio } from '../../fields/RadioGroup/Radio';
|
5 | 7 | import { TextInput } from '../../fields/TextInput/TextInput';
|
| 8 | +import { Root } from '../../Root'; |
6 | 9 |
|
7 |
| -import { Field } from './Field'; |
| 10 | +import { Field, Form } from './index'; |
8 | 11 |
|
9 | 12 | jest.mock('../../../_internal/hooks/use-warn');
|
10 | 13 |
|
@@ -213,4 +216,124 @@ describe('Legacy <Field />', () => {
|
213 | 216 | ),
|
214 | 217 | ).not.toThrow();
|
215 | 218 | });
|
| 219 | + |
| 220 | + it('should render default values correctly after form re-render', () => { |
| 221 | + const TestForm = ({ defaultValues }) => { |
| 222 | + const [form] = Form.useForm(); |
| 223 | + |
| 224 | + return ( |
| 225 | + <Root> |
| 226 | + <Form form={form} defaultValues={defaultValues}> |
| 227 | + <Field name="test"> |
| 228 | + <TextInput label="test" /> |
| 229 | + </Field> |
| 230 | + {defaultValues.test2 && ( |
| 231 | + <Field name="test2"> |
| 232 | + <TextInput label="test2" /> |
| 233 | + </Field> |
| 234 | + )} |
| 235 | + </Form> |
| 236 | + </Root> |
| 237 | + ); |
| 238 | + }; |
| 239 | + |
| 240 | + // Initial render with a single input and default values |
| 241 | + const { getByRole, rerender } = render( |
| 242 | + <TestForm defaultValues={{ test: 'Hello, world!' }} />, |
| 243 | + ); |
| 244 | + |
| 245 | + // Check initial input value |
| 246 | + const input1 = getByRole('textbox', { name: 'test' }); |
| 247 | + expect(input1).toHaveValue('Hello, world!'); |
| 248 | + |
| 249 | + // Re-render with two inputs and updated default values |
| 250 | + rerender( |
| 251 | + <TestForm |
| 252 | + defaultValues={{ test: 'Hello, world!', test2: 'Goodbye, world!' }} |
| 253 | + />, |
| 254 | + ); |
| 255 | + |
| 256 | + // Check both inputs for default values |
| 257 | + const input2 = getByRole('textbox', { name: 'test2' }); |
| 258 | + expect(input1).toHaveValue('Hello, world!'); |
| 259 | + expect(input2).toHaveValue('Goodbye, world!'); |
| 260 | + }); |
| 261 | + |
| 262 | + it("shouldn't change current value after default values change in form", () => { |
| 263 | + const TestForm = ({ defaultValues }) => { |
| 264 | + const [form] = Form.useForm(); |
| 265 | + |
| 266 | + return ( |
| 267 | + <Root> |
| 268 | + <Form form={form} defaultValues={defaultValues}> |
| 269 | + <Field name="test"> |
| 270 | + <TextInput label="test" /> |
| 271 | + </Field> |
| 272 | + </Form> |
| 273 | + </Root> |
| 274 | + ); |
| 275 | + }; |
| 276 | + |
| 277 | + // Initial render with a single input and default values |
| 278 | + const { getByRole, rerender } = render( |
| 279 | + <TestForm defaultValues={{ test: 'Hello, world!' }} />, |
| 280 | + ); |
| 281 | + |
| 282 | + // Check initial input value |
| 283 | + const input = getByRole('textbox', { name: 'test' }); |
| 284 | + expect(input).toHaveValue('Hello, world!'); |
| 285 | + |
| 286 | + // Re-render with two inputs and updated default values |
| 287 | + rerender(<TestForm defaultValues={{ test: 'Goodbye, world!' }} />); |
| 288 | + |
| 289 | + expect(input).toHaveValue('Hello, world!'); |
| 290 | + }); |
| 291 | + |
| 292 | + it('should set to new default value after its change and form reset', () => { |
| 293 | + const TestForm = ({ defaultValues, forceReset = false }) => { |
| 294 | + const [form] = Form.useForm(); |
| 295 | + |
| 296 | + useEffect(() => { |
| 297 | + if (forceReset) { |
| 298 | + form.resetFields(); |
| 299 | + } |
| 300 | + }, []); |
| 301 | + |
| 302 | + return ( |
| 303 | + <Root> |
| 304 | + <Form form={form} defaultValues={defaultValues}> |
| 305 | + <Field name="test"> |
| 306 | + <TextInput label="test" /> |
| 307 | + </Field> |
| 308 | + </Form> |
| 309 | + </Root> |
| 310 | + ); |
| 311 | + }; |
| 312 | + |
| 313 | + // Initial render with a single input and default values |
| 314 | + const { getByRole, rerender } = render( |
| 315 | + <TestForm defaultValues={{ test: 'Hello, world!' }} />, |
| 316 | + ); |
| 317 | + |
| 318 | + // Check initial input value |
| 319 | + const input = getByRole('textbox', { name: 'test' }); |
| 320 | + expect(input).toHaveValue('Hello, world!'); |
| 321 | + |
| 322 | + // Re-render with two inputs and updated default values |
| 323 | + rerender(<TestForm defaultValues={{ test: 'Goodbye, world!' }} />); |
| 324 | + |
| 325 | + expect(input).toHaveValue('Hello, world!'); |
| 326 | + |
| 327 | + // Re-render with two inputs and updated default values |
| 328 | + rerender( |
| 329 | + <TestForm |
| 330 | + defaultValues={{ test: 'Goodbye, world!', forceReset: true }} |
| 331 | + />, |
| 332 | + ); |
| 333 | + |
| 334 | + waitFor(() => { |
| 335 | + // Check that default value is reset |
| 336 | + expect(input).toHaveValue('Goodbye, world!'); |
| 337 | + }); |
| 338 | + }); |
216 | 339 | });
|
0 commit comments