|
1 |
| -import React from 'react'; |
2 | 1 | import { mount } from 'enzyme';
|
3 | 2 | import { resetWarned } from 'rc-util/lib/warning';
|
| 3 | +import React from 'react'; |
4 | 4 | import Form, { Field, useForm } from '../src';
|
5 |
| -import InfoField, { Input } from './common/InfoField'; |
6 | 5 | import { changeValue, getField, matchError } from './common';
|
| 6 | +import InfoField, { Input } from './common/InfoField'; |
7 | 7 | import timeout from './common/timeout';
|
8 | 8 |
|
9 | 9 | describe('Form.Basic', () => {
|
@@ -838,4 +838,46 @@ describe('Form.Basic', () => {
|
838 | 838 |
|
839 | 839 | expect(wrapper.find('input').prop('value')).toEqual('bamboo');
|
840 | 840 | });
|
| 841 | + |
| 842 | + it('setFieldValue', () => { |
| 843 | + const formRef = React.createRef(); |
| 844 | + |
| 845 | + const Demo = () => ( |
| 846 | + <Form ref={formRef} initialValues={{ list: ['bamboo', 'little', 'light'] }}> |
| 847 | + <Form.List name="list"> |
| 848 | + {fields => |
| 849 | + fields.map(field => ( |
| 850 | + <Field key={field.key} {...field}> |
| 851 | + <Input /> |
| 852 | + </Field> |
| 853 | + )) |
| 854 | + } |
| 855 | + </Form.List> |
| 856 | + |
| 857 | + <Field name={['nest', 'target']} initialValue="nested"> |
| 858 | + <Input /> |
| 859 | + </Field> |
| 860 | + </Form> |
| 861 | + ); |
| 862 | + |
| 863 | + const wrapper = mount(<Demo />); |
| 864 | + expect(wrapper.find('input').map(input => input.prop('value'))).toEqual([ |
| 865 | + 'bamboo', |
| 866 | + 'little', |
| 867 | + 'light', |
| 868 | + 'nested', |
| 869 | + ]); |
| 870 | + |
| 871 | + // Set |
| 872 | + formRef.current.setFieldValue(['list', 1], 'tiny'); |
| 873 | + formRef.current.setFieldValue(['nest', 'target'], 'match'); |
| 874 | + wrapper.update(); |
| 875 | + |
| 876 | + expect(wrapper.find('input').map(input => input.prop('value'))).toEqual([ |
| 877 | + 'bamboo', |
| 878 | + 'tiny', |
| 879 | + 'light', |
| 880 | + 'match', |
| 881 | + ]); |
| 882 | + }); |
841 | 883 | });
|
0 commit comments