首页 文章

与redux-mock-store一起使用时,redux-observable流中的重复操作

提问于
浏览
1

我打开了一个问题,但万一我做错了:

使用jest和redux-mock-store编写单个测试时,一切都按预期工作 . 但是如果我多次使用mockStore(在同一个测试中,甚至在另一个测试中),那么在任何创建的商店中调度的动作将在observable中多次发送(但在store中只发送一次,如store.getActions()状态 .

这是一个复制回购:https://framagit.org/jalil/redux-observable-mock-store-duplicate

tldr;

这有效:

const store = mockStore();
store.dispatch({ type: 'FOO' }); // -> Observable get 1 FOO action

这不是:

const store = mockStore();
store2 = mockStore();
mockStore();
store.dispatch({ type: 'FOO' }); // => Observable get 3 FOO actions

要么 :

const store = mockStore();
store2 = mockStore();
mockStore();
mockStore();
mockStore();
store.dispatch({ type: 'FOO' }); // -> Observable get 5 FOO actions

... 等等 ...

我希望,因为我使用replaceEpic和mockStore,并且我使用不同的jest测试,一个测试不应该影响另一个,一个mockStore不应该影响另一个 .

所以,我希望,即使我有多个测试每个调用mockStore(),让我的史诗接收正确的动作流 .

已解决的问题:

1 回答

  • 4

    redux-observable目前假设 createEpicMiddleware() 返回的中间件函数已经检查过't be called multiple times. redux-mock-store appears to call it every time you call mockStore. While this behavior is probably not officially documented, my gut tells me redux-observable should not be making this assumption. Similar libraries like redux-saga used to as well, but they might have stopped, I haven' t .

    它's not immediately clear why this has never been noticed before by anyone. I don'看到任何一个显着的变化,最近会引入这个库 . 我最好的猜测是,它没有过滤任何操作或发出任何操作,而只是始终使用 .do() 进行日志记录 .

    我现在正在度假,所以直到本周晚些时候才能深入研究这个问题 . 抱歉!但是你可以通过创建一个新的epicMiddleware并为每个测试调用configureMockStore而不是重用它来解决它 . 一个例子可能是这样的:

    const mockStore = (...args) => {
      const epicMiddleware = createEpicMiddleware(someEpic);
      const factory = configureMockStore([epicMiddleware]);
      return factory(...args);
    };
    

    根据您的需求调整;例如如果你需要改变根史诗 .

    我们会在您的机票中跟踪这个:https://github.com/redux-observable/redux-observable/issues/389

相关问题