File size: 3,271 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
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<TestProps> = ({ show, onRef }) => {
const [form] = Form.useForm();
const removeRef = React.useRef<InputRef>(null);
const testRef = React.useRef<InputRef>(null);
const listRef = React.useRef<InputRef>(null);
return (
<Form form={form} initialValues={{ list: ['light'] }}>
{show && (
<Form.Item name="remove" label="remove">
<Input ref={removeRef} />
</Form.Item>
)}
<Form.Item name="test" label="test">
<Input ref={testRef} />
</Form.Item>
<Form.List name="list">
{(fields) =>
fields.map((field) => (
<Form.Item {...field} key={field.key}>
<Input ref={listRef} />
</Form.Item>
))
}
</Form.List>
<Button
className="ref-item"
onClick={() => {
onRef(form.getFieldInstance('test'), testRef.current!);
}}
>
Form.Item
</Button>
<Button
className="ref-list"
onClick={() => {
onRef(form.getFieldInstance(['list', 0]), listRef.current!);
}}
>
Form.List
</Button>
<Button
className="ref-remove"
onClick={() => {
onRef(form.getFieldInstance('remove'), removeRef.current!);
}}
>
Removed
</Button>
</Form>
);
};
it('should ref work', () => {
const onRef = jest.fn();
const { container, rerender } = render(<Test onRef={onRef} show />);
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(<Test onRef={onRef} show={false} />);
fireEvent.click(container.querySelector('.ref-remove')!);
expect(onRef).toHaveBeenCalledWith(undefined, null);
});
it('should have nativeForm', () => {
const formRef = React.createRef<FormRef>();
const { container } = render(<Form ref={formRef} />);
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 = () => <div />;
const formRef = React.createRef<FormInstance>();
render(
<Form ref={formRef}>
<Form.Item name="bamboo" label="Bamboo">
<NoRefComp />
</Form.Item>
</Form>,
);
const ele = formRef.current?.getFieldInstance('bamboo');
expect(ele).toBeFalsy();
});
});
|