admin管理员组文章数量:1243108
I am modifying an example found here:
.md
class Foo extends React.Component {
render() {
return (
<input className={this.props.name} type="text" value={this.props.name} onChange={()=>{}} />
);
}
}
it('should pass and does not', ()=> {
const wrapper = mount(<Foo name="foo" />);
expect(wrapper.find('.foo').html()).toBe(`<input class="foo" type="text" value="foo">`);
wrapper.setProps({ name: 'bar' });
expect(wrapper.find('.bar').html()).toBe(`<input class="bar" type="text" value="bar">`);
});
Result: Expected '<input class="bar" type="text" value="foo">' to be '<input class="bar" type="text" value="bar">'.
You can see from the result of the test that the className attribute was correctly updated on prop change. But the value of the input remains incorrectly set to 'foo'.
Any ideas on how I can assert that value has been correctly changed on the ponent receiving new props to a value attribute on an input?
I am modifying an example found here:
https://github./airbnb/enzyme/blob/master/docs/api/ReactWrapper/setProps.md
class Foo extends React.Component {
render() {
return (
<input className={this.props.name} type="text" value={this.props.name} onChange={()=>{}} />
);
}
}
it('should pass and does not', ()=> {
const wrapper = mount(<Foo name="foo" />);
expect(wrapper.find('.foo').html()).toBe(`<input class="foo" type="text" value="foo">`);
wrapper.setProps({ name: 'bar' });
expect(wrapper.find('.bar').html()).toBe(`<input class="bar" type="text" value="bar">`);
});
Result: Expected '<input class="bar" type="text" value="foo">' to be '<input class="bar" type="text" value="bar">'.
You can see from the result of the test that the className attribute was correctly updated on prop change. But the value of the input remains incorrectly set to 'foo'.
Any ideas on how I can assert that value has been correctly changed on the ponent receiving new props to a value attribute on an input?
Share Improve this question asked Jan 22, 2016 at 10:51 Daniel BillinghamDaniel Billingham 1,4115 gold badges16 silver badges25 bronze badges1 Answer
Reset to default 11You have to call a method .update()
on a wrapper. (Just after you set new props) This will force update the ponent and the value of the input should change.
You can read more about it here: http://airbnb.io/enzyme/docs/api/ShallowWrapper/update.html
本文标签: javascriptReact testing component prop change with enzymeStack Overflow
版权声明:本文标题:javascript - React testing component prop change with enzyme - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740109790a2225989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论