componentWillMount中的测试函数

时间:2017-02-27 10:06:59

标签: javascript reactjs redux

componentWillMount() {
     // redux store
    CrudActions.getUsers();
}

如何使用jest来调用我的减速器?我想编写行为测试,而不是在CrudActions中调用另一个setAll函数。

到目前为止,我只能调用componentWillMount

test('calls `componentWillMount` before rendering', () => {
  const onWillMount = jest.fn();
  CrudList.prototype.componentWillMount = onWillMount;
  mount(<CrudList />);

  expect(onWillMount).toBeCalled();
});

1 个答案:

答案 0 :(得分:0)

我不是100%确定您的问题在这里,所以我将其解释为“如何卸载我的组件以便调用CrudActions.getUsers()?”

安装组件后,您可以调用unmount来卸载它(我假设您使用酶来安装组件)。

test('calls `componentWillUnmount` when unmounted', () => {
    const onWillUnmount = jest.fn();
    CrudList.prototype.componentWillUnmount = onWillUnmount;
    mount(<CrudList />).unmount();

    expect(onWillUnmount).toBeCalled();
});

所以要调用CrudActions.getUsers()只是不要模拟componentWillUnmount,它应该调用实际的函数。