测试mapDispatchToProps异步操作

时间:2017-12-23 00:19:36

标签: reactjs asynchronous testing jest redux-thunk

我正在尝试在调度asyncronous函数时测试我的mapDispatchToProps函数。我已经阅读了Dan Abramov关于如何测试mapDispatchToProps的建议,我正在尝试测试我的代码。

我收到了错误...

 TypeError: Cannot read property 'then' of undefined

这是我的测试...

describe("mapDispatchToProps", () => {

    const dispatchSpy = jest.fn();
    const {signupUser} = mapDispatchToProps(dispatchSpy);

    it("should dispatch signupActions.signupUser()", () => {
        // mockAxios.onPost(endpoints.SIGNUP,{})
        //         .reply(200,'test_success');
        // tried with and without mockAxios

        signupUser({})
        const spyLastCall = dispatchSpy.mock.calls[0][0];
        expect(spyLastCall)
            .toEqual(signupActions.signupUser({}));
    })

})

我想测试的功能......

export const mapDispatchToProps = dispatch => {
    return { signupUser: (user) => {
        dispatch(signupActions.signupUser(user))
            .then((response) => {
                // do something on success
            }, (error) => {
                // do something on failure
            })
    }
}

我已经测试了signupActions.signupUser,我知道它会返回一个promise。这是代码......

export function signupUser(user) {
    return (dispatch) => {
        return dispatch(rest.post(SIGNUP,user))
            .then((response) => {
                return Promise.resolve(response);
            },(error) => {
                return Promise.reject(error)
            }
        )
}}

我做错了什么?

Ps:我也试过了:

 const dispatchSpy = jest.fn().mockImplementation( () => {
    return p = new Promise((reject,resolve) => {
        resolve({})
    }) 
 }

具有相同的结果

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人,我最终使用mergeProps,这使我的测试更加清洁。现在我有......

export const mapDispatchToProps = dispatch => {
    return { dispatchSignupUser: (user) => {
        dispatch(signupActions.signupUser(user))
    }
}

export const mergeProps = (propsFromState,propsFromDispatch,ownProps) => {
    return { 
        signupUser: (values) => {
           return propsFromDispatch.dispatchSignupUser(values)
                .then(() => { // do something on success }, 
                      () => { // do something on failure})
     }
}

我单独测试它们......

describe("signup", () => {

    /// ...  ownProps and propsFromState declared here

    const dispatchSpy = jest.fn((x) => {});

    const {
        dispatchSignupUser,
    } = mapDispatchToProps(dispatchSpy);

    const signupUser = mergeProps(propsFromState,propsFromDispatch,ownProps);

    describe("mapDispatchToProps", () => {
        it("should dispatch signup user on dispatchSignupUser", () => {
            const spyOn = jest.spyOn(signupActions,'signupUser');      
            dispatchSignupUser({test: "test"});   
            expect(spyOn).toHaveBeenCalledWith({test: "test"});
        })
    })

    describe("mergeProps", () => {
        it("should do something on success", () => {
            propsFromDispatch.dispatchSignupUser jest.fn().mockImplementation((x) => {
                return new Promise((resolve,reject) => { return resolve({})} )
            });     
            return signupUser({}).then(() => {
                expect(history.location.pathname).toEqual("/signup/thank-you")
            }, (error) => {})
        })
    })

})

希望这有用!