Jest测试没有在子组件中触发父onClick函数

时间:2018-05-14 05:25:48

标签: javascript reactjs enzyme jest

因此,我测试了以下子组件,该子组件从其父级中的父级接收函数:

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.

我该如何解决这个问题?

0 个答案:

没有答案