单元测试以下redux异步操作的正确方法如何?如何使用redux对单元测试/存根异步调用
const client = contentful.createClient(clientConfig);
export const fetchNavigation =() => {
return dispatch => {
return client.getEntries({content_type: 'navigation'})
.then((entries) => {
console.log('All entries for content_type = navigation')
dispatch(receiveNavigation(entries))
})
.catch(error => {
\t console.log('Something went wrong');
\t dispatch(fetchNavigationFailure(error));
});
}
}
我不知道如何自定义通过client.getEntries执行的Web请求的响应体。我认为用我自己的代替getEntries函数可以做到这一点。但是,我不知道从哪里开始这样做。
这里是单元测试我写的:
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
describe('fetchNavigation',() => {
it('creates RECEIVE_NAVIGATION when fetching navigation is done',() => {
// Here I should prepare the client.getEntries() returned promise
const expectedBodyResponse = { includes: ['do something', 'yay!'] }
const expectedActions = [
{ type: actions.RECEIVE_NAVIGATION, navigation: expectedBodyResponse }
]
const store = mockStore({ todos: [] })
return store.dispatch(actions.fetchNavigation())
.then(() => {
expect(store.getActions()).toEqual(expectedActions)
})
})
})
非常感谢您的回答,斯特凡。 – Alvaro