这是我用于用户登录的Redux异步操作
export const loginUser = (userData, history) => (dispatch) => {
dispatch({ type: LOADING_UI });
return axios
.post('/login', userData)
.then((res) => {
setAuthorizationHeader(res.data.token);
dispatch(getUserData());
dispatch({ type: CLEAR_ERRORS });
history.push('/');
})
.catch((err) => {
dispatch({
type: SET_ERRORS,
payload: err.response.data
});
});
};
export const getUserData = () => (dispatch) => {
dispatch({ type: LOADING_USER });
// send get request, if response exist, then dispatch SET_USER action
return axios
.get('/user')
.then((res) => {
dispatch({
type: SET_USER,
payload: res.data
});
})
.catch((err) => console.log(err));
};
我正在测试,当调用loginUser
时,它将调度LOADING_UI
以及具有预期有效负载的LOADING_USER
和SET_USER
,然后调度CLEAR_ERRORS
< / p>
describe('dataActions', () => {
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
beforeEach(() => {
moxios.install();
});
it('should dispatch an action to login a user', () => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200
});
});
const expectedActions = [
{ type: LOADING_UI },
{ type: LOADING_USER },
{ type: SET_USER },
{ type: CLEAR_ERRORS }
]
const store = mockStore({ userData: {}, history: {} })
return store.dispatch(loginUser()).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
})
afterEach(() => {
moxios.uninstall();
})
})
但是,我收到TypeError: (0 , _dataActions.loginUser) is not a function
。错误指向代码return store.dispatch(loginUser()).then(() => {
的这一行。我不熟悉此错误,因此不胜感激。
此外,这是测试用户登录操作的建议逻辑吗?还是我应该测试一下完全不同的东西?谢谢。