forked from react-component/field-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.test.tsx
40 lines (32 loc) · 1.35 KB
/
field.test.tsx
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
import React from 'react';
import Form, { Field } from '../src';
import type { FormInstance } from '../src';
import { render } from '@testing-library/react';
describe('Form.Field', () => {
it('field remount should trigger constructor again', () => {
let formRef: FormInstance;
const Demo = ({ visible }: { visible: boolean }) => {
const [form] = Form.useForm();
formRef = form;
const fieldNode = <Field name="light" initialValue="bamboo" />;
return <Form form={form}>{visible ? fieldNode : null}</Form>;
};
// First mount
const { rerender } = render(<Demo visible />);
// ZombieJ: testing lib can not access instance
// const instance = wrapper.find('Field').instance() as any;
// expect(instance.cancelRegisterFunc).toBeTruthy();
expect(formRef.getFieldsValue()).toEqual({ light: 'bamboo' });
// Hide
// wrapper.setProps({ visible: false });
// expect(instance.cancelRegisterFunc).toBeFalsy();
rerender(<Demo visible={false} />);
expect(formRef.getFieldsValue()).toEqual({});
// Mount again
// wrapper.setProps({ visible: true });
rerender(<Demo visible />);
// expect(instance.cancelRegisterFunc).toBeFalsy();
// expect((wrapper.find('Field').instance() as any).cancelRegisterFunc).toBeTruthy();
expect(formRef.getFieldsValue()).toEqual({ light: 'bamboo' });
});
});