我怎样才能测试history.push里面的动作?

问题描述 投票:0回答:1

我怎样才能测试history.push里面的动作?

export const startLogout = () => {
return ( dispatch ) => {

    return firebase
        .auth()
        .signOut()
        .then(() => {
            dispatch( logout() );
            history.push( '/login' );
        })
        .catch( ( err ) => {
            // Simple redirect to non existent route
            history.push( '/oops' );
            console.log( err );
        });
};

};

test( 'should start logout', ( done ) => {
const store = createMockStore({});

store.dispatch( startLogout() ).then(() => {
    const actions = store.getActions();

    expect( actions[0] ).toEqual({
        type: LOGOUT
    });

    const history = { push: jest.fn() };
    expect( history.push ).toHaveBeenLastCalledWith( '/login' );

    done();
}).catch((err) => {
    console.log( err );
    done();
});

});

这个当前代码给了我这个错误:

最后调用的预期模拟函数:[“/ login”]但它没有被调用。

谢谢

reactjs redux jestjs enzyme redux-thunk
1个回答
0
投票

您正在测试之前创建一个本地history对象并将其push属性设置为间谍,以查看是否调用了本地history.push间谍。

您需要在history.push中使用的startLogout上创建间谍,并且在您发送startLogout动作之前需要它到位。

© www.soinside.com 2019 - 2024. All rights reserved.