您好,我在基于Moui,Enzyme和Chai的基于材料ui的对话框窗口中为取消按钮创建了一个测试,但出现错误,无法读取属性。
我的对话框窗口:
class DialogWindow extends Component {
constructor() {
super();
this.state = {
open: false
};
}
handleRequestClose() {
this.setState({open: false});
}
render() {
return (
<Dialog
onClose={this.handleRequestClose}
open={this.state.open}
render={({
Icon,
getIconProps,
getTitleProps,
getContentProps,
getActionsProps
}) => (
<div>
<Icon {...getIconProps()} />
<DialogTitle {...getTitleProps()}>
entry
</DialogTitle>
<DialogContent {...getContentProps()}>
Are you sure you want to proceed?
</DialogContent>
<DialogActions {...getActionsProps()}>
<Button onClick={this.handleRequestClose.bind(this)}>Cancel</Button>
</DialogActions>
</div>
)}
{...this.props}
/>
);
}
}
export default connect()(DialogWindow);
对于取消按钮的测试,我不确定是否可以像这样访问一个孩子:
describe('Cancel button test', () => {
it('cancel button on the dialog window works', () => {
const wrapper = shallow(
<Provider store = {store}>
<DialogWindow.WrappedComponent />
</Provider>).dive();
wrapper.setState({ open: true });
const dialog = wrapper.find('Dialog').at(0);
const cancelButton = dialog.find('button').at(0);
cancelButton.simulate('click');
expect(wrapper.state().open).to.eql(false);
});
});
错误:
TypeError: Cannot read property 'props' of undefined
at Object.simulateEvent (node_modules\enzyme-adapter-react-16\build\ReactSixteenAdapter.js:326:34)
at ShallowWrapper.<anonymous> (node_modules\enzyme\build\ShallowWrapper.js:932:57)
at ShallowWrapper.single (node_modules\enzyme\build\ShallowWrapper.js:1620:25)
at ShallowWrapper.simulate (node_modules\enzyme\build\ShallowWrapper.js:929:21)
at Context.<anonymous> (src\_tests_\components/D:\projec\src\_tests_\components\DialogWindow.test.js:48:11)