Skip to content

Commit

Permalink
test getValue
Browse files Browse the repository at this point in the history
  • Loading branch information
wangtao0101 committed Nov 23, 2017
1 parent 61aa3e9 commit 02978c1
Show file tree
Hide file tree
Showing 2 changed files with 49 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 @@ -41,6 +41,7 @@ export default class Form extends Component {
});
return result;
}
invariant(this.components[name], `component with id: ${name} is not exist in form.`);
return this.components[name].state.value;
}

Expand Down
48 changes: 48 additions & 0 deletions test/getValue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { mount } from 'enzyme';
import { Input } from 'antd';
import FormItem from '../src/FormItem';
import Form from '../src/Form';

describe('getValue from form', () => {
it('getValue by name success', () => {
const wrapper = mount(
<Form>
<FormItem id="test" value="test" >
<Input />
</FormItem>
</Form>);
const form = wrapper.instance();
expect(form.getValue('test')).toBe('test');
});

it('getValue by all', () => {
const wrapper = mount(
<Form>
<FormItem id="test" value="test" >
<Input />
</FormItem>
<FormItem id="test1" value="test1" >
<Input />
</FormItem>
</Form>);
const form = wrapper.instance();
expect(form.getValue()).toEqual({
test: 'test',
test1: 'test1',
});
});

it('check id when getValue', () => {
expect(() => {
const wrapper = mount(
<Form>
<FormItem id="test">
<Input />
</FormItem>
</Form>);
const form = wrapper.instance();
form.getValue('test1');
}).toThrow(/component with id: test1 is not exist in form./);
});
});

0 comments on commit 02978c1

Please sign in to comment.