猿问

检查是否在 JEST 中调用了 window.location.reload

const ErrorSnackbar = () => {


     const handelePrimaryBtnClick = () => { window.location.reload() }


     return ( <div className = 'sample_class' onClick={handelePrimaryBtnClick}> Sample Code </div> )

}

开玩笑地说,当我单击 div 时,如何检查是否调用了 window.location.reload()。


白衣非少年
浏览 169回答 1
1回答

MMMHUHU

您可以窥探该reload方法并模拟单击div,例如:it("should reload the page", () => {&nbsp; jest.spyOn(window.location, 'reload');&nbsp; const component = mount(ErrorSnackbar());&nbsp; component.find('.sample_class').simulate('click');&nbsp; expect(window.location.reload).toHaveBeenCalled();});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答