我试图通过测试验证状态组件的状态是否已适当更改componentDidMount,但由于react-router而撞到了墙.
我正在使用Enzyme,所以我用mount它来评估生命周期方法,如componentDidMount.通常情况下,这很好......
it("changes state after mount", () => {
const newValue = "new value";
const testPropertyRetriever = () => newValue;
const wrapper = mount(
<StatefulPage
myProperty="initial value"
propertyRetriever={testPropertyRetriever}
/>
);
// componentDidMount should have executed and changed the state's myProperty value
// from "initial value" to "new value"
expect(wrapper.instance().state.myProperty).toEqual(newValue);
});
Run Code Online (Sandbox Code Playgroud)
...但是有问题的组件是有问题的,因为mount深层渲染了几个孩子,在这种情况下,其中一个后代使用react-router <Link>.因此,运行上述测试会导致错误:TypeError: Cannot read property 'history' of undefined和Failed context type: The context `router` is marked as required in …