我有一个基于类的组件,它具有以下方法:
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
,我希望生命周期方法已经运行。我在这里做错了什么?