小编Mat*_*att的帖子

使用React,react-router,jest和enzyme测试状态更改

我试图通过测试验证状态组件的状态是否已适当更改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 undefinedFailed context type: The context `router` is marked as required in …

javascript unit-testing reactjs react-router enzyme

3
推荐指数
1
解决办法
1606
查看次数

标签 统计

enzyme ×1

javascript ×1

react-router ×1

reactjs ×1

unit-testing ×1