mardi 30 octobre 2018

Prop function in componentDidUpdate not being called in mounted enzyme test

I have a class-based component that has the following method:

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

I have the following test that is failing:

  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 is initialized in a beforeEach statement like so:

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

But my test fails in the second case after the setProps is called, where I would expect the lifecycle method to have run. What am I doing wrong here?

Aucun commentaire:

Enregistrer un commentaire