在我的行动中提出请求。从需要从模拟 redux 存储返回值的 api 中提取。我让它在 redux 模拟商店调度操作时启动该请求,但是当 store.getActions() 时我无法正确显示这些值。
我收到错误store.getActions()
是从模拟商店返回空状态。
例如:
simpleActions.js
export const fetchTodos = () => {
return async (dispatch) => {
const response = await axios.get('https://myweb.com/example.json');
dispatch({ type: types.FETCH_TODOS_REQUEST , payload: response.data });
}
}
simpleAction.test.js
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
describe('async actions', () => {
afterEach(() => {
fetchMock.restore()
})
it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', () => {
fetchMock.getOnce('/todos', {
body: { todos: ['do something'] },
headers: { 'content-type': 'application/json' }
})
const expectedActions = [
{ type: types.FETCH_TODOS_REQUEST },
{ type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something'] } }
]
const store = mockStore({ todos: [] })
const dispatchStored = store.dispatch(actions.fetchTodos());
return dispatchStored.then(() => {
**const values = store.getActions();**
This values is returning the state empty array from the mock store.
expect(store.getActions()).toEqual(expectedActions)
})
})
})
如何调用或模拟需要返回值而不是模拟存储中的状态空数组的异步/等待函数。任何帮助将不胜感激。