In my component I check whether the page is reloaded and redirect to another page. I do it by following code.
useEffect(() => {
//this will prevent users from accidently refreshing / closing tab
window.onbeforeunload = () => {
return "";
};
//check whether user reloaded
if (
window.performance.getEntriesByType("navigation")[0].type === "reload"
) {
openExternalURL(process.env.GATSBY_MARKETING_URL);
}
}, []);
problem is I keep getting error in my Jest / react-test-library test cases as below
TypeError: window.performance.getEntriesByType is not a function
Even I tried to mock it like below but had no success.
window.performance = {
getEntriesByType: jest.fn().mockReturnValue([{ type: "reload" }]),
measure: jest.fn()
};
Can someone point me to the right direction? thanks in advance.
