【问题标题】:How can I test history.push inside action?如何测试 history.push 内部操作?
【发布时间】:2019-03-18 03:14:40
【问题描述】:

如何测试history.push inside action?

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();
});

});

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

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

谢谢

【问题讨论】:

    标签: reactjs redux jestjs enzyme redux-thunk


    【解决方案1】:

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

    您需要在startLogout 中使用的history.push 上创建间谍,并且它需要在您调度startLogout 操作之前就位。

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多