我试图在 MyComponent
中测试的是 Switch
组件的 checked
属性的值,它是 的属性FormControlLabel
组件:
class MyComponent extends Component {
(...)
render() {
return (
<FormControlLabel
name={`formControl`}
control={
<Switch
name={`switch`}
data
checked={this.state.isChecked}
onClick={this.handleChange}
value={checked}
/>
}
/>
);
}
}
我可以像这样访问 FormControlLabel
组件:
const wrapper = shallow(<MyComponent />);
wrapper.find('[name="formControl"]');
我尝试像这样访问 Switch
组件,但它不起作用:
wrapper.find('[name="switch"]');
如何访问 Switch
组件的 checked
属性?
- 浅
- enzyme :3.3.0
- react :16.2.0
- enzyme 适配器-react-16
最佳答案
好吧,我刚刚找到了一个解决方案:
expect(wrapper.find('[name="formControl"]').prop('control').props.checked).toEqual(true);
关于reactjs - Enzyme,如何访问通过 props 传递的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50505694/