import React from 'react'; import { mount } from 'enzyme'; import { act } from 'react-dom/test-utils'; import Form from '..'; import Input from '../../input'; import Button from '../../button'; import { sleep } from '../../../tests/utils'; describe('Form.List', () => { async function change(wrapper, index, value) { wrapper.find(Input).at(index).simulate('change', { target: { value } }); await sleep(); wrapper.update(); } function testList(name, renderField) { it(name, async () => { jest.useFakeTimers(); const wrapper = mount(
{(fields, { add, remove }) => ( <> {fields.map(field => renderField(field))} )}
, ); await click(wrapper, '.add'); await change(wrapper, 0, 'input1'); wrapper.find('form').simulate('submit'); await sleep(); expect(onFinish).toHaveBeenLastCalledWith({ list: ['input1'] }); await click(wrapper, '.add'); await change(wrapper, 1, 'input2'); await click(wrapper, '.add'); await change(wrapper, 2, 'input3'); wrapper.find('form').simulate('submit'); await sleep(); expect(onFinish).toHaveBeenLastCalledWith({ list: ['input1', 'input2', 'input3'] }); await click(wrapper, '.remove'); // will remove first input wrapper.find('form').simulate('submit'); await sleep(); expect(onFinish).toHaveBeenLastCalledWith({ list: ['input2', 'input3'] }); }); it('list errors', async () => { jest.useFakeTimers(); let operation; const wrapper = mount(
{ if (value.length < 2) { return Promise.reject(new Error('At least 2')); } }, }, ]} > {(_, opt, { errors }) => { operation = opt; return ; }}
, ); async function addItem() { await act(async () => { operation.add(); await sleep(100); jest.runAllTimers(); wrapper.update(); }); } await addItem(); expect(wrapper.find('.ant-form-item-explain div').text()).toEqual('At least 2'); await addItem(); expect(wrapper.find('.ant-form-item-explain div')).toHaveLength(0); jest.useRealTimers(); }); it('should render empty without errors', () => { const wrapper = mount(); expect(wrapper.render()).toMatchSnapshot(); }); });