import React from 'react'; import type { FormRef } from 'rc-field-form/lib/interface'; import Form, { FormInstance } from '..'; import { fireEvent, render } from '../../../tests/utils'; import Button from '../../button'; import type { InputRef } from '../../input'; import Input from '../../input'; interface TestProps { show?: boolean; onRef: (node: React.ReactNode, originRef: InputRef) => void; } describe('Form.Ref', () => { const Test: React.FC = ({ show, onRef }) => { const [form] = Form.useForm(); const removeRef = React.useRef(null); const testRef = React.useRef(null); const listRef = React.useRef(null); return (
{show && ( )} {(fields) => fields.map((field) => ( )) }
); }; it('should ref work', () => { const onRef = jest.fn(); const { container, rerender } = render(); fireEvent.click(container.querySelector('.ref-item')!); expect(onRef).toHaveBeenCalled(); expect(onRef.mock.calls[0][0]).toBe(onRef.mock.calls[0][1]); onRef.mockReset(); fireEvent.click(container.querySelector('.ref-list')!); expect(onRef).toHaveBeenCalled(); expect(onRef.mock.calls[0][0]).toBe(onRef.mock.calls[0][1]); onRef.mockReset(); rerender(); fireEvent.click(container.querySelector('.ref-remove')!); expect(onRef).toHaveBeenCalledWith(undefined, null); }); it('should have nativeForm', () => { const formRef = React.createRef(); const { container } = render(
); expect(container.querySelector('.ant-form')).toBe(formRef.current?.nativeElement); }); // TODO: this is no need to test in React 19 it('not crash if not support Ref', () => { const NoRefComp = () =>
; const formRef = React.createRef(); render( , ); const ele = formRef.current?.getFieldInstance('bamboo'); expect(ele).toBeFalsy(); }); });