Skip to content

Commit

Permalink
test setValue
Browse files Browse the repository at this point in the history
  • Loading branch information
wangtao0101 committed Nov 23, 2017
1 parent 02978c1 commit e72a527
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
53 changes: 53 additions & 0 deletions test/setValue.test.js
Original file line number Diff line number Diff line change
@@ -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(
<Form>
<FormItem id="test" value="test" >
<Input />
</FormItem>
</Form>);
const form = wrapper.instance();
form.setValue('test', 'a');
expect(form.getValue('test')).toBe('a');
});

it('setValue all', () => {
const wrapper = mount(
<Form>
<FormItem id="test" value="test" >
<Input />
</FormItem>
<FormItem id="test1" value="test1" >
<Input />
</FormItem>
</Form>);
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(
<Form>
<FormItem id="test">
<Input />
</FormItem>
</Form>);
const form = wrapper.instance();
form.setValue('test1');
}).toThrow(/component with id: test1 is not exist in form./);
});
});

0 comments on commit e72a527

Please sign in to comment.