在安装的酶测试中未调用componentDidUpdate中的道具功能

时间:2018-10-30 22:21:38

标签: reactjs jestjs enzyme

我有一个基于类的组件,该组件具有以下方法:

componentDidUpdate(prevProps) {
  if (prevProps.location.pathname !== this.props.location.pathname) {
    this.props.onDelete();
  }
}

我的以下测试失败:

  it(`should call the 'onDelete' function when 'location.pathName' prop changes`, () => {
    const wrapper = mount(<AlertsList.WrappedComponent {...props} />);

    // test that the deleteFunction wasn't called yet
    expect(deleteFunction).not.toHaveBeenCalled();

    // now update the prop
    wrapper.setProps({ location: { ...props.location, pathName: "/otherpath" } });

    // now check that the deleteFunction was called
    expect(deleteFunction).toHaveBeenCalled();
  });

其中propsbeforeEach语句中初始化,如下所示:

  beforeEach(() => {
    props = {
      ...
      location: { pathName: "/" }
    };
  });

但是在调用setProps之后的第二种情况下,我的测试失败了,我希望在该情况下可以运行生命周期方法。我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

问题是错字,请参阅我的原始帖子下的评论。