简体   繁体   English

当承诺在 Redux 中间件中解决时,为什么我不能分派操作?

[英]Why can't I dispatch an action when a promise resolves inside Redux middleware?

Background背景

I am writing a piece of Redux middleware that makes an axios request and uses Cheerio to parse the result.我正在编写一段 Redux 中间件,它发出 axios 请求并使用 Cheerio 解析结果。

Problem问题

When the Axios promise resolves and I try to dispatch a fulfilled action the action does not show up in the store's action log in the test.当 Axios 承诺解决并且我尝试分派一个已完成的操作时,该操作不会出现在测试中商店的操作日志中。

Middleware中间件

function createMiddleware() {
    return ({ dispatch, getState }) => next => action => {

      if (isCorrectAction(action)===true){

      const pendingAction = {
        type: `${action.type}_PENDING`,
        payload: action.payload
      }

      dispatch(pendingAction)

      axios.get(action.payload.url)
            .then(response => {
              let $ = cheerio.load(response.data)
              let parsedData = action.payload.task($)

              const fulfilledAction = {
                type: `${action.type}_FULFILLED`, 
                payload: { 
                  parsedData 
                }
              }

              dispatch(fulfilledAction) // dispatch that is problematic

            })
            .catch( err => {

            });
       }

        return next(action);
    }
}

Test that fulfilled action is dispatched fails已完成的动作被分派的测试失败

   it('should dispatch ACTION_FULFILLED once', () => {
       nock("http://www.example.com")
           .filteringPath(function(path) {
               return '/';
           })
           .get("/")
           .reply(200, '<!doctype html><html><body><div>text</div></body></html>');

       const expectedActionTypes = ['TASK', 'TASK_PENDING', 'TASK_FULFILLED']

       // Initialize mockstore with empty state
       const initialState = {}
       const store = mockStore(initialState)
       store.dispatch(defaultAction)

       const actionsTypes = store.getActions().map(e => e.type)
       expect(actionsTypes).has.members(expectedActionTypes);
       expect(actionsTypes.length).equal(3);
   });

Solution - Promise needs to be returned in the mocha test解决方案——在 mocha 测试中需要返回 Promise

The solution is to rewrite the mocha test so that the promise is returned.解决方案是重写 mocha 测试,以便返回承诺。 I mistakenly thought that by using nock to intercept the HTTP request that the promise would become synchronous.我错误地认为通过使用nock拦截HTTP请求,promise会变成同步的。

The working test looks like:工作测试看起来像:

it('should dispatch ACTION_FULFILLED once', () => {
    nock("http://www.example.com")
        .filteringPath(function(path) {
            return '/';
        })
        .get("/")
        .reply(200, '<!doctype html><html><body><div>text</div></body></html>');

    const store = mockStore();

    return store.dispatch(defaultScrapingAction)
                .then(res => {   

         const actionsTypes = store.getActions().map(e => e.type)
         expect(actionsTypes).has.members(expectedActionTypes);
         expect(actionsTypes.length).equal(3);
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么每个Redux中间件都可以调用next(action),而不是多次调度一个action? - Why every Redux Middleware can call next(action), wouldn't it dispatch one action multiple times? 如何让redux派遣一个动作并返回一个承诺? - How can I make redux dispatch an action as well as return a promise? Redux Promise中间件如何反应将结果操作发送到调度? - How does react redux promise middleware send the resulting action to the dispatch? 在Redux中间件中调度多个动作 - Dispatch multiple action in redux middleware 如何在Redux中间件中调度动作? - how to dispatch a action in redux middleware? 为什么我不能推送这个调度操作? - Why can't I push in this dispatch action? 为什么我的redux定制中间件需要在操作时返回调度? - why my redux custom middleware need to return dispatch on action? 为什么我会使用Redux Promise Middleware而不是Redux Promise? - Why would I use Redux Promise Middleware over Redux Promise? 未在Promise的“ then”部分内进行动作分派(React / Redux) - An action dispatch inside the 'then' part of a Promise is not being called (React/Redux) 如果我的操作创建者返回 promise,Redux 何时解决调度? - When does Redux resolve a dispatch if my action creator return promise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM