javascript - 当 promise 在 Redux 中间件内解析时,为什么我无法调度操作?

标签 javascript node.js ecmascript-6 redux es6-promise

背景

我正在编写一个 Redux 中间件,它发出 axios 请求并使用 Cheerio 解析结果。

问题

当 Axios promise 解决并且我尝试分派(dispatch)已完成的操作时,该操作不会显示在测试中的商店操作日志中。

中间件

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);
    }
}

分派(dispatch)已完成操作的测试失败

   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);
   });

最佳答案

解决方案-mocha测试需要返回Promise

解决方案是重写 mocha 测试,以便返回 Promise。我错误地认为通过使用 nock 拦截 HTTP 请求,promise 就会变成同步的。

工作测试如下:

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);
    })
});

关于javascript - 当 promise 在 Redux 中间件内解析时,为什么我无法调度操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41513380/

相关文章:

javascript - 选择表单选项时创建文本字符串

javascript - 我可以设置在 Chrome 中显示的 PDF 对象的文件名吗?

javascript - 将选项传递给 ES6 模块导入

javascript - 防止 NodeJS 在开发/调试时缩小 JavaScript 代码

javascript - Highcharts 不会导出创建后添加的自定义 SVG 元素

javascript - 如何使用 JavaScript 每秒向右移动 10 个像素并向下移动 10 个像素

javascript - 如何从mountebank响应 block 中的另一个js文件调用函数

node.js - 图片下载后的回调函数

javascript - Node Coffeescript 无法自行注册

html - 如何获取 &lt;script type ="module"> 中的 document.currentScript.ownerDocument?