Javascript 开玩笑地在卸载前测试

Javascript 开玩笑地在卸载前测试,javascript,reactjs,unit-testing,jestjs,gatsby,Javascript,Reactjs,Unit Testing,Jestjs,Gatsby,因此,基本上我有一个onbeforeunload函数,当用户决定重新加载或关闭选项卡时,它会注意并警告用户。我的代码如下 useEffect(() => { //this will prevent users from accidently refreshing / closing tab window.onbeforeunload = () => { return "Are you sure you want to do this

因此,基本上我有一个onbeforeunload函数,当用户决定重新加载或关闭选项卡时,它会注意并警告用户。我的代码如下

  useEffect(() => {
   
    //this will prevent users from accidently refreshing / closing tab
    window.onbeforeunload = () => {
      return "Are you sure you want to do this yada yada yada yada?";
    };
  }, []);
我试图开玩笑地测试它,但返回语句从未执行过。我的测试如下

window.location.reload();
expect(window.onbeforeunload).toHaveBeenCalled();
即使我在卸载之前模仿onforeUnload,它也不起作用。非常感谢您的帮助。谢谢