因此,我测试了以下子组件,该子组件从其父级中的父级接收函数:
export interface IProps{
parentClick: () => void;
}
export class Child extends React.Component<IProps, {}> {
handleClick = () => {
this.props.parentClick();
}
render () {
return ( <button onClick={this.handleClick}>Click me</button> )
}
}
在我的Jest测试中,我执行以下操作来检查this.props.parentClick
是否被触发:
it("button click fires", () => {
const props = {
parentClick: jest.fn();
}
const Child = enzyme.mount(<Child {...props} />);
Child.find('button').simulate('click'); //shouldn't this trigger parentClick?
expect(props.parentClick).toHaveBeenCalled();
});
但测试失败说:
Expected mock function to have been called one time, but it was called zero times.
我该如何解决这个问题?