如何在redux-thunk中断言在异步操作内部调度的异步操作
how to assert an async action dispatched inside an async action in redux-thunk
我试图断言异步动作是由异步动作分派的,像这样:
// synchronous actions
export const getObjects = () => ({ type: 'GET_OBJECTS' });
export const addObject = object => ({ type: 'ADD_OBJECT', object });
// an async action
export const getObjectsAsync = () =>
dispatch =>
axios.get(URL).then((data) => {
dispatch(getObjects());
});
// another async action that dispatches the previous async action
export const postObjectAsync = newObject =>
dispatch =>
axios.post(URL, newObject)
.then(() => { dispatch(addObject(newObject)); })
.then(() => { dispatch(getObjectAsync()); });
// the test
describe('postObjectAsync()', () => {
it('should return ADD_OBJECT and GET_OBJECT actions', () => {
const object = mockedObject;
const store = mockedStore;
const expectedActions = [
{ type: 'ADD_OBJECT', object },
{ type: 'GET_OBJECTS', objects }, // I expected to see this object on store.getActions()[1]
];
return store.dispatch(postObjectAsync(object))
.then(() => {
store.getActions().should.deep.equal(expectedActions);
// AssertionError: expected [ Array(1) ] to deeply equal [ Array(2) ]
});
});
});
我期望store.getActions()包含一个数组,其中包含GET_OBJECTS和ADD_OBJECT操作,但它只包含ADD_OBJECT操作
谁能发表意见吗?
弄明白了,问题不在测试中,
// another async action that dispatches the previous async action
export const postObjectAsync = newObject =>
dispatch =>
axios.post(URL, newObject)
.then(() => { dispatch(addObject(newObject)); })
.then(() => { dispatch(getObjectAsync()); });
应该// another async action that dispatches the previous async action
export const postObjectAsync = newObject =>
dispatch =>
axios.post(URL, newObject)
.then(() => {
dispatch(addObject(newObject));
return dispatch(getObjectAsync());
});
我刚刚意识到不应该在同步函数上使用。then()。如何在redux中处理两个连续且依赖的异步调用
相关文章:
- 如何指示“;熟度;在异步操作中
- 使用jquery更改异步操作中的文本值
- 节点.js中的嵌套异步操作
- 阻止chrome.webRequest.onBeforeSendHeaders侦听器中的异步操作
- 节点.js和表达式:如何在异步操作后返回响应
- 无法在Redux中测试异步操作
- 当我在异步操作Redux上开始单元测试时,没有定义错误承诺
- 循环中的异步操作
- 回流如何侦听异步操作已完成
- 我们可以使用Jasmine在测试中执行异步操作吗
- jQuery:如何检查以确保所有异步操作都已完成
- JavaScript ES6 承诺在 while 循环中调用异步操作
- 请等待异步操作 (firebase) 调用完成,然后再加载网页
- 具有 Javascript 同步接口的异步操作
- 如何在异步操作期间使 Express.js 日志错误
- 如何在 $ 中执行异步操作.延迟,然后失败过滤回调并传递回成功链
- Redux 中的异步操作
- Redux - 智能组件 - 在挂载组件之前如何处理异步操作
- 如何转换for循环与异步操作内部
- 如何在redux-thunk中断言在异步操作内部调度的异步操作