ant-design/components/form/__tests__/list-noStyle.test.tsx

67 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-06-22 14:57:09 +08:00
import React from 'react';
2021-01-19 11:51:13 +08:00
import { act } from 'react-dom/test-utils';
import Form from '..';
import { render, fireEvent, waitFakeTimer } from '../../../tests/utils';
2022-06-22 14:57:09 +08:00
import Input from '../../input';
import type { FormListOperation } from '../FormList';
2021-01-19 11:51:13 +08:00
describe('Form.List.NoStyle', () => {
it('nest error should clean up', async () => {
jest.useFakeTimers();
let operation: FormListOperation;
const { container } = render(
2021-01-19 11:51:13 +08:00
<Form>
<Form.List name="users">
{(fields, op) => {
operation = op;
return fields.map((field) => (
2021-01-19 11:51:13 +08:00
<Form.Item key={field.key}>
<Form.Item
{...field}
name={[field.name, 'first']}
rules={[{ required: true }]}
noStyle
>
<Input />
</Form.Item>
</Form.Item>
));
}}
</Form.List>
</Form>,
);
// Add two
const addItem = async () => {
2021-01-19 11:51:13 +08:00
await act(async () => {
operation?.add();
2021-01-19 11:51:13 +08:00
});
await waitFakeTimer();
};
await addItem();
await addItem();
2021-01-19 11:51:13 +08:00
// Submit
fireEvent.submit(container.querySelector('form')!);
await waitFakeTimer();
2021-01-19 11:51:13 +08:00
// Remove first field
await act(async () => {
operation?.remove(0);
2021-01-19 11:51:13 +08:00
});
await waitFakeTimer();
2021-01-19 11:51:13 +08:00
// Match error message
expect(container.querySelector('.ant-form-item-explain-error')?.textContent).toBe(
2021-01-19 11:51:13 +08:00
"'users.1.first' is required",
);
jest.clearAllTimers();
2021-01-19 11:51:13 +08:00
jest.useRealTimers();
});
});