我如何测试 history.push inside action?

How can I test history.push inside action?

如何测试 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();
});

});

当前代码给我这个错误:

预期的模拟函数最后一次被调用: [“/登录”] 但是没有调用。

谢谢

您正在创建本地 history 对象并将其 push 属性 设置为间谍,然后再测试是否调用了本地 history.push 间谍。

您需要在 startLogout 中使用的 history.push 上创建间谍,并且它需要在 派遣 [=14] 之前就位=]行动。