diff --git a/src/Form.jsx b/src/Form.jsx index fdd4043..f3299a7 100644 --- a/src/Form.jsx +++ b/src/Form.jsx @@ -47,6 +47,7 @@ export default class Form extends Component { setValue(name, value) { if (typeof name === 'string') { + invariant(this.components[name], `component with id: ${name} is not exist in form.`); this.components[name].setValue(value); return; } diff --git a/test/setValue.test.js b/test/setValue.test.js new file mode 100644 index 0000000..7a0f447 --- /dev/null +++ b/test/setValue.test.js @@ -0,0 +1,53 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import { Input } from 'antd'; +import FormItem from '../src/FormItem'; +import Form from '../src/Form'; + +describe('setValue from form', () => { + it('setValue by name success', () => { + const wrapper = mount( +
+ + + +
); + const form = wrapper.instance(); + form.setValue('test', 'a'); + expect(form.getValue('test')).toBe('a'); + }); + + it('setValue all', () => { + const wrapper = mount( +
+ + + + + + +
); + const form = wrapper.instance(); + form.setValue({ + test: 'a', + test1: 'b', + }); + expect(form.getValue()).toEqual({ + test: 'a', + test1: 'b', + }); + }); + + it('check id when setValue', () => { + expect(() => { + const wrapper = mount( +
+ + + +
); + const form = wrapper.instance(); + form.setValue('test1'); + }).toThrow(/component with id: test1 is not exist in form./); + }); +});