javascript - 使用酶完整安装找到的组件的实例等于 null
问题描述
我有我想测试的向导组件。
向导组件
class WizardComponent extends React.Component {
render(){
return (
<div>
{this.props.steps.map(({Component, ...props}) => <Component {...props} />)}
</div>
)
}
}
测试
it('Provides props to the component', () => {
const FakeComponent = props => <div>1</div>
const fakeProps = {
test:1,
prop4:'test'
}
const wrapper = mount(<WizardComponent currentStepName="fakeComponent" steps={[
{Component:FakeComponent, name:'fakeComponent'},
]}/>)
const component = wrapper.find(FakeComponent);
console.log(wrapper.contains(<FakeComponent />)) // logs true
console.log(component) // logs ReactWrapper { length: 1 }
console.log(component.instance()) // logs null
expect(component.instance().props).objectContaining(fakeProps)
})
为什么我无法访问组件实例?我想测试它的子组件道具。有人可以帮忙吗?
解决方案
根据enyzme 文档,组件包装器的道具可以使用wrapper.props()
.
你用过wrapper.instance().props
。
推荐阅读
- reactjs - 电子生产构建总是有框架 - macos
- bootstrap-4 - 使用 Bootstrap 显示和隐藏表格?
- javascript - 如何使用 webpack 4 简单地加载具有 src 属性的图像?
- oracle - PLS-00382:从匿名块执行包函数的表达式类型错误
- javascript - AmCharts - 如何理解源代码?
- magnolia - 在木兰中获取用户名
- python - 导入模块函数及其所需的依赖项
- javascript - 我需要根据数组中值的受欢迎程度对字符串数组进行排序
- c# - 从 CSV 创建对象
- python - 是否可以使用 Python 和 cx_Oracle 进行空间查询?