Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 笑话:如何期望Redux操作已被调度?_Reactjs_Redux_Jestjs_Enzyme - Fatal编程技术网

Reactjs 笑话:如何期望Redux操作已被调度?

Reactjs 笑话:如何期望Redux操作已被调度?,reactjs,redux,jestjs,enzyme,Reactjs,Redux,Jestjs,Enzyme,我尝试的是确保一个Redux操作(作为prop传递)已被调度 我正试图通过jest监视该操作,但一直失败 下面是测试的简化版本 let props; let wrapped; describe("Sample", () => { beforeEach(() => { props = { setResults: jest.fn() // This is the action I'm trying to spy on. }; wrapped

我尝试的是确保一个Redux操作(作为
prop
传递)已被调度

我正试图通过
jest
监视该操作,但一直失败

下面是测试的简化版本

let props;
let wrapped;

describe("Sample", () => {
  beforeEach(() => {
    props = {
      setResults: jest.fn()   // This is the action I'm trying to spy on.
    };
    wrapped = mount(
      <Provider store={createStore(reducers, {})}>
        <Sample {...props} />
      </Provider>
    );
  });

  afterEach(() => {
    wrapped.unmount();
  });


  it("can setResults", () => {
    wrapped.find("input").simulate("change", { target: { value: "example" } });
    wrapped.update();
    wrapped.find("button").simulate("click");

    // setResults is supposed to have been called!
    expect(props.setResults).toHaveBeenCalled();
  });
});

我在操作前后放置了两个
console.log()
,这确认调用了
handleEvent
函数本身。尽管如此,
expect(props.setResults).tohavebeencall()
在测试中失败

当我把
setResults
放在最后一个
console.log()
中时,我看到

What is in setResults? results => dispatch((0, _actions.setResults)(results))
这让我确信,Redux的行为不能以这种方式被嘲笑(或被窥视)

该操作与
react redux
连接

const mapDispatchToProps = dispatch => ({
  setResults: results => dispatch(setResults(results))
});

或者,我在上面的代码中遗漏了什么


如有任何建议,将不胜感激。(目标是确保已调用操作。)

除非在提供程序中包装组件时有一些我不知道的警告,否则这将起作用:

const setResults = jest.fn();

在每次回调之前:

beforeEach(() => {
    wrapped = mount(
      <Provider store={createStore(reducers, {})}>
        <Sample setResults={setResults} />
      </Provider>
    );

除非在提供程序中包装组件时有一些我不知道的警告,否则这将起作用:

const setResults = jest.fn();

在每次回调之前:

beforeEach(() => {
    wrapped = mount(
      <Provider store={createStore(reducers, {})}>
        <Sample setResults={setResults} />
      </Provider>
    );