我怎样才能测试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”]但它没有被调用。
谢谢
您正在测试之前创建一个本地history
对象并将其push
属性设置为间谍,以查看是否调用了本地history.push
间谍。
您需要在history.push
中使用的startLogout
上创建间谍,并且在您发送startLogout
动作之前需要它到位。