4
我终于将我的组件从React.createClass()
更新为ES6类。我看到在我的测试中第一个错误是,我的一些sinon.stub()
调用失败,因为jscodeshift -t react-codemod/transforms/class.js
转换我的组件的方法来使用属性初始化方案,即React - 如何对使用属性初始值设定语法的组件方法进行存根?
class Foo extends React.Component {
componentWillMount() {
this.fetchSomeData(this.props);
}
componentWillReceiveProps(nextProps) {
if (nextProps.someProp !== this.props.someProp) {
this.fetchSomeData(nextProps);
}
}
fetchSomeData = (props) => {
...
};
render() {
...
}
}
我的问题是:我怎么能使用存根fetchSomeData()
这个新的语法?我的测试看起来像sinon.stub(Foo.prototype, 'fetchSomeData');
不再起作用,假设因为fetchSomeData
不在原型上了。
谢谢!
ES7发布*去年*(2016)。属性初始值设定项是*提议*。 –
您的测试是否可以访问组件的实例?如果是这样,你可以做sinon.stub(componentInstance,'fetchSomeData'),然后componentInstance.render(); – jeznag
@jeznag好点,谢谢。没想到这一点。但我让酶处理渲染生命周期,我不认为干预这将是一个很好的解决方案。 – mulleady1