我有以下代码(为便于理解而简化):
class ClassA extends React.Component {
constructor(props) {
super(props);
this.state = {
clicked: true
}
}
render () {
return (
<Snackbar
clicked={this.state.clicked}
message={
<div> This is the message </div>
}
action={[
<Button variant="contained" color="primary" onClick={functionCall}>
Go
</Button>
]}
/>
);
}
如何测试元素?我尝试使ClassA变浅,然后使Snackbar变浅,但是我仍然无法.simulate('click')
在按钮上调用a 。
基本上,您如何浅化/模拟作为道具传递给更高节点的代码段?
为了测试作为道具传递给Snackbar组件的按钮,您需要将道具传递给Snackbar,然后在模拟点击之前先将动作浅装,因为它仅适用于已安装的组件而不是道具
const component = shallow(<ClassA />);
const SnackbarAction = component.find(Snackbar).prop('action')[0];
const button = shallow(SnackbarAction);
button.simulate('click');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句