我正在编写测试以测试涉及调度的mapDispatchToProps。
这是我的mapDispatchToProps。
export const mapDispatchToProps = dispatch => {
return {
getListOfYears: url => dispatch(getListOfYears(url))
};
};
分派中的方法如下。
export const getListOfYears = url => {
return getService(url, { actions: getActions(actionNames.GET_LIST_OF_YEARS), shouldRequest });
};
export const shouldRequest = () => {
return true;
};
export const requestActionCreator = actionName => () => ({
type: getRequestActionType(actionName)
});
export const failureActionCreator = actionName => error => ({
type: getFailureActionType(actionName),
payLoad: error
});
export const successActionCreator = actionName => response => {
if (response && response.data && response.status === 200) {
return {
type: getSuccessActionType(actionName),
payLoad: response.data
};
}
return {
type: getFailureActionType(actionName),
payLoad: response
};
};
export const getActions = actionName => ({
requestAction: requestActionCreator(actionName),
successAction: successActionCreator(actionName),
failureAction: failureActionCreator(actionName)
});
export const getRequestActionType = actionName => `${actionName}_REQUEST`;
export const getSuccessActionType = actionName => `${actionName}_SUCCESS`;
export const getFailureActionType = actionName => `${actionName}_FAILURE`;
这是我在Jest中的测试用例。
it('dispatches action getListOfYears', () => {
mapDispatchToProps(dispatch).getListOfYears(mockUrl);
expect(dispatch.mock.calls[0][0]).toEqual(actions.getListOfYears(mockUrl))
});
dispatch.mock.calls [0] [0]给了我一个匿名函数。请提供测试方法的建议。
谢谢。