0

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

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();
  });

where在如下语句props中初始化:beforeEach

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

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

4

1 回答 1

0

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

于 2018-10-30T22:48:21.837 回答