i have declared a function named as reloadScreen, which reload the browser window on execution and it gets invoked on clicking a button. Now i want to test this function that on clicking a button window is getting reloaded or not, i am testing in jest.
my html code for button is :
<button class='restartBtn' onclick="reloadScreen()">reset</button>
my javascript function is:
function reloadScreen(){
window.location.reload();
}
my test code is provided below :
describe('should reload the browser on clicking a button having class as "restartBtn". ', ()=> {
let restartButton;
let dom;
let window;
let document;
beforeEach(() => {
dom = new JSDOM(html, { runScripts: 'dangerously' });
document = dom.window.document;
window = dom.window;
restartButton = document.querySelector('.restartBtn');
})
test('should reload the browser.', ()=>{
// Mock the reload method of window.location object
const reloadMock = jest.fn();
window.location.reload = reloadMock;
restartButton.click();
// Expect window.location.reload to be called
expect(reloadMock).toHaveBeenCalled();
})
})
error i am getting is,
[error] : reload property of window.location is read-only cannot be modified.
can anyone help me, to resolve this.