import React from 'react'; import { mount } from 'enzyme'; import raf from 'raf'; import { act } from 'react-dom/test-utils'; import Form from '..'; import Input from '../../input'; import Button from '../../button'; import { sleep } from '../../../tests/utils'; jest.mock('raf'); describe('Form.List', () => { raf.mockImplementation(callback => window.setTimeout(callback)); afterAll(() => { raf.mockRestore(); }); 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'] }); }); });