Baj*_*jju 3 reactjs jestjs react-native enzyme
我有一个有2个输入文本字段的组件.
在componentDidMount()
我打电话的方法中 this.refs.password.focus();
我的内部有一些棘手的业务逻辑,componentDidMount
但在进行浅单元测试时componentDidMount
,我得到了错误
无法访问未定义的密码
我检查了浅组件的实例,看到它this.refs
是未定义的.我的问题是我们如何通过测试来设置它?
Shallow有一个第二个参数,我们可以传递这个参数作为上下文,我们可以设置单元测试的上下文,但它似乎什么都不做.
这个领域的任何帮助将受到高度赞赏.
解决此问题的最简单方法是设置refs
实例.
const component = shallow(<Component />)
const instance = component.instance()
instance.refs = {
password: {
getRenderedComponent: jest.fn(() => ({
focus: jest.fn
}))
}
}
Run Code Online (Sandbox Code Playgroud)
shallow
没有ref
方法。您需要使用mount
来测试整个渲染。
参考:API文档:https : //github.com/airbnb/enzyme/blob/master/docs/api/shallow.md https://github.com/airbnb/enzyme/blob/master/docs/api/mount。 md
问题:https : //github.com/airbnb/enzyme/issues/316
归档时间: |
|
查看次数: |
5909 次 |
最近记录: |