我尝试在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
props?
API接口
版本
适配器
发布于 2018-05-24 18:19:30
好吧,我找到了一个解决方案:
expect(wrapper.find('[name="formControl"]').prop('control').props.checked).toEqual(true);
发布于 2019-06-27 07:54:38
const controlComponent = shallow(wrapper.find(FormControlLabel).props().control);
expect(controlComponent.props().checked).toBeTruthy();
https://stackoverflow.com/questions/50505694
复制相似问题