import React from 'react'; import type { FormListFieldData, FormListOperation } from '..'; import Form from '..'; import { fireEvent, render, waitFakeTimer } from '../../../tests/utils'; import Button from '../../button'; import Input from '../../input'; describe('Form.List', () => { // const change = async ( // wrapper: ReturnType['container'], // index: number, // value: string, // ) => { // fireEvent.change(wrapper.getElementsByClassName('ant-input')?.[index], { target: { value } }); // await sleep(); // }; const changeValue = async ( input: HTMLElement | null | number, value: string, advTimer = 1000, ) => { let element: HTMLElement; if (typeof input === 'number') { element = document.querySelectorAll('input')[input]; } expect(element!).toBeTruthy(); fireEvent.change(element!, { target: { value, }, }); if (advTimer) { await waitFakeTimer(advTimer / 20); } }; beforeEach(() => { document.body.innerHTML = ''; jest.useFakeTimers(); }); afterAll(() => { jest.clearAllTimers(); jest.useRealTimers(); }); const testList = ( name: string, renderField: (value: FormListFieldData) => React.ReactNode, ): void => { it(name, async () => { const { container } = render(
{(fields, { add, remove }) => ( <> {fields.map(renderField)} )}
, ); await click(container, '.add'); await changeValue(0, 'input1'); fireEvent.submit(container.querySelector('form')!); await waitFakeTimer(); expect(onFinish).toHaveBeenLastCalledWith({ list: ['input1'] }); await click(container, '.add'); await changeValue(1, 'input2'); await click(container, '.add'); await changeValue(2, 'input3'); fireEvent.submit(container.querySelector('form')!); await waitFakeTimer(); expect(onFinish).toHaveBeenLastCalledWith({ list: ['input1', 'input2', 'input3'] }); await click(container, '.remove'); // will remove first input fireEvent.submit(container.querySelector('form')!); await waitFakeTimer(); expect(onFinish).toHaveBeenLastCalledWith({ list: ['input2', 'input3'] }); }); it('list errors', async () => { let operation: FormListOperation; const { container } = render(
{ if (value.length < 2) { return Promise.reject(new Error('At least 2')); } }, }, ]} > {(_, opt, { errors }) => { operation = opt; return ; }}
, ); async function addItem() { operation.add(); await waitFakeTimer(); } await addItem(); expect(container.querySelector('.ant-form-item-explain div')?.innerHTML).toEqual('At least 2'); await addItem(); expect(container.getElementsByClassName('ant-form-item-explain div')).toHaveLength(0); }); it('should render empty without errors', () => { const { container } = render(); expect(container.firstChild).toMatchSnapshot(); }); it('no warning when reset in validate', async () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const Demo = () => { const [form] = Form.useForm(); React.useEffect(() => { form.setFieldsValue({ list: [1], }); }, []); return (
{(fields) => fields.map((field) => ( )) }
); }; const { container } = render(); fireEvent.click(container.querySelector('button')!); await waitFakeTimer(); expect(errorSpy).not.toHaveBeenCalled(); errorSpy.mockRestore(); }); it('no warning when name is 0', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); render(
{(fields) => fields.map((field) => ( )) }
, ); expect(errorSpy).not.toHaveBeenCalled(); errorSpy.mockRestore(); }); it('warning when name is empty array', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); render(
{(fields) => fields.map((field) => ( )) }
, ); expect(errorSpy).toHaveBeenCalled(); errorSpy.mockRestore(); }); it('warning when name is null', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); render(
{(fields) => fields.map((field) => ( )) }
, ); expect(errorSpy).toHaveBeenCalled(); errorSpy.mockRestore(); }); });