我对异步动作的测试存在问题,该动作包含对其他异步动作的调用,我得到了
TypeError:无法读取未定义的属性'then'
动作是:
export function fetchSettings() {
return dispatch => {
dispatch({
type: FETCH_SETTINGS,
});
return getSettings('settings')
.then(response => dispatch(fetchSettingsSuccess(response)))
.catch(error => dispatch(fetchSettingsFailure(error)));
};
}
function fetchSettingsSuccess(response) {
return {
type: FETCH_SETTINGS_SUCCESS,
response,
};
}
export function disconnectAccount(account) {
return dispatch => {
dispatch({
type: DISCONNECT_ACCOUNT,
});
return deleteAccountConnection(account)
.then(response => dispatch(disconnectAccountSuccess(response)))
.catch(error => dispatch(disconnectAccountFailure(error)));
};
}
function disconnectAccountSuccess(response) {
return dispatch => {
dispatch({
type: DISCONNECT_ACCOUNT_SUCCESS,
response,
});
dispatch(fetchSettings());
};
}
我编写的测试是:
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
...
describe('action tests', () => {
it('should dispatch action DISCONNECT_ACCOUNT and then DISCONNECT_ACCOUNT_SUCCESS',
async () => {
deleteAccountConnection.mockResolvedValue('settings');
const expectedActions = [
{
type: DISCONNECT_ACCOUNT,
},
{
type: DISCONNECT_ACCOUNT_SUCCESS,
response: 'settings',
},
{
type: FETCH_SETTINGS,
},
{
type: FETCH_SETTINGS_SUCCESS,
response: 'settings',
},
];
const store = mockStore();
await store.dispatch(disconnectAccount());
await store.dispatch(fetchSettings());
expect(store.getActions()).toEqual(expectedActions);
});
如果我不以操作类型DISCONNECT_ACCOUNT_SUCCESS调用fetchSettings,我将没有问题,但是当我向它添加异步调用时,我得到了一个错误。