【发布时间】:2020-10-02 15:47:51
【问题描述】:
如何检查 redux 操作是否有效?并在执行结束时,检查 dataState 的数据?
例如:
在应用程序中测试授权的功能。期望(AuthMethod(登录名,密码));
行动:
return async (dispatch) => {
dispatch(AuthLoading());
try {
var date = new Date().getDate();
var month = new Date().getMonth() + 1;
var year = new Date().getFullYear();
var hours = new Date().getHours();
var min = new Date().getMinutes();
var sec = new Date().getSeconds();
var time =
date + '-' + month + '-' + year + ' ' + hours + ':' + min + ':' + sec;
let response = await fetch(
'http://' + ServerLink + ':' + ServerPort + '/tmo/rest/user',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
login: login,
password: password,
deviceSerialNumber: '1111111',
currentAppVersion: AppVersion,
currentDateTime: time,
}),
},
);
let data = await response.json();
if (data.sessionToken === '') {
dispatch(AuthFailure('Token null'));
} else {
AsyncStorage.setItem('token', data.sessionToken);
dispatch(AuthSuccess(data));
}
} catch (e) {
dispatch(AuthFailure(e.message));
}
};
};
【问题讨论】:
-
可能它返回一个有效的操作对象,即具有
type属性和任何其他数据属性的对象。你有任何尝试测试的测试代码示例吗? -
不幸的是,我不太擅长这个。我所有测试它的尝试都以失败告终。我需要正确调用此操作,然后检查 DataReducer 中的数据。类似的东西:it('auth test', async () => { await AuthMethod(login , password) const value = store.getState().AuthData })
-
我没有使用过 redux-thunk,但通常我认为你会通过针对处理最终调度动作的 reducer 编写的测试来“测试”你的动作创建者。模拟 fetch,将当前状态和正确的参数传递给 action creator 到 reducer,并断言 reducer 的正确状态输出。
-
你能给我举个例子吗?如果我有一个工作示例(例如使用 AuthMethod),我可以继续对其他操作进行测试,那就太酷了
标签: reactjs react-native jestjs redux-thunk