我有一个函数,utils.fetch_json
它返回一个json的承诺(或json中的某些错误)。我想测试一下,在fetch_json
调度使用的动作updateData
时,在API的json有效的情况下,使用json的有效负载更新了存储状态。
例子:
import {createStore, applyMiddleware} from "redux";
import Store from "../../reducers/foobar";
import { updateData } from "../../actions/foobar";
import utils from '../../utils'
import thunk from 'redux-thunk';
const data = {
a: ["A1", "A2"],
b: [20, 10],
}
describe("Test the reducer-actions", () => {
utils.fetch_json = jest.fn().mockResolvedValue(data);
it("updates the state when updateData is dispatched", () => {
const store = createStore(Store, applyMiddleware(thunk));
store.dispatch(updateData())
expect(store.getState().data).toEqual(data);
});
});
这会失败,因为的执行store.dispatch(updateData())
是异步的,因此expect
会在其之前执行,从而导致初始状态(不是)data
处于存储中。
我试图避免对此的依赖,但是如果表示法与笑话类似,我愿意使用它。
请注意,我并不是要:
我有兴趣在集成中对其进行测试。
如果updateData返回,则可以执行以下操作:
store.dispatch(updateData()).then(()=>test here)
那是假设updateData
返回一个承诺。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句