1

我正在编写测试来测试涉及调度的 mapDispatchToProps。

这是我的 mapDispatchToProps。

    export const mapDispatchToProps = dispatch => {
        return {
            getListOfYears: url => dispatch(getListOfYears(url))
        };
    };

dispatch 中的方法如下。

    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] 给了我一个匿名函数。请提供有关测试方法的建议。

谢谢。

4

0 回答 0