我正在为Redux操作编写单元测试,其中一个包含JSONP请求,代码:
export function performUsersRequest() {
return dispatch => {
dispatch(requestUser());
return fetchJsonp(APP_CONFIG.URL)
.then(res => {
dispatch(requestUserFulfilled());
return res.json()
})
.catch(ex => {
dispatch(requestUserRejected());
})
}
}
由于它是JSONP请求,看起来像nock库不能嘲笑它,我总是得到请求被拒绝的动作。我试图找到一个解决方案,但最终没有找到有用的东西。我的测试是:
it('make async request to get users', () => {
const store = mockStore({getUsers: ''});
const dispatch = store.dispatch;
nock(APP_CONFIG.URL)
.get('/')
.reply(200, {users: []});
return dispatch(actionsUsers.performUserRequest())
.then(() => {
console.log(store.getActions());
// expect(store.getActions()).toEqual(expectedActions);
})
});