Skip to content

Commit cd06c0d

Browse files
authored
chore: add more field tests (#566)
1 parent 3dca96f commit cd06c0d

File tree

1 file changed

+125
-2
lines changed

1 file changed

+125
-2
lines changed

src/components/form/Form/field.test.tsx

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { useState } from 'react';
1+
import { waitFor } from '@storybook/test';
2+
import { render } from '@testing-library/react';
3+
import { useEffect, useState } from 'react';
24

35
import { renderWithForm, userEvent, act } from '../../../test/index';
46
import { Radio } from '../../fields/RadioGroup/Radio';
57
import { TextInput } from '../../fields/TextInput/TextInput';
8+
import { Root } from '../../Root';
69

7-
import { Field } from './Field';
10+
import { Field, Form } from './index';
811

912
jest.mock('../../../_internal/hooks/use-warn');
1013

@@ -213,4 +216,124 @@ describe('Legacy <Field />', () => {
213216
),
214217
).not.toThrow();
215218
});
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+
});
216339
});

0 commit comments

Comments
 (0)