如何使用jest酶来模拟bindActionCreators。

问题描述 投票:1回答:1

在js文件中

import { bindActionCreators } from "redux";

const mapDispatchToProps = dispatch => {
  const MyActions = bindActionCreators(MyActions, dispatch);

  return {
    actions: {
      method1: () => {
        MyActions.myActionMethod();
      },
}

现在我想测试一下,调用method1是否会派发MyActions.myActionMethod。

我的.test.js代码

 it("Should call myAction", () => {
    jest
      .spyOn(MyActions, "myActionMethod")
      .mockImplementation(() => {
        console.log("calling mock imple");

      });

    enzymeWrapper.props().actions.method1();
  });

谁能告诉我如何模拟bindActionCreators?

reactjs unit-testing redux jestjs enzyme
1个回答
0
投票

我找到了解决方法,我们需要监视或模拟动作之前 enzymeWrapper 创作,也就是说,在调用 shallow 方法。

© www.soinside.com 2019 - 2024. All rights reserved.