I have searched online but I didn't find a way to mock window.close()
in react testing library or even in jest.
const handleClose = () => {
window.opener.location.reload()
window.close()
}
<Button
data-test-id="close"
onClick={handleClose}
/>
How do I achieve test case coverage for the onclick
of button and window.close()
and window.opener.location.reload()
is covered
My test case is as follows:
const wrapper = render( <CloseButton />);
const windowSpy = jest.spyOn(global, 'window', 'get');
const { queryByTestId } = wrapper;
fireEvent.click(queryByTestId('close');
expect(windowSpy.opener.location.relaod).toHaveBeenCalled();
expect(windowSpy.close).toHaveBeenCalled();
for this last line of code
expect(windowSpy.close).toHaveBeenCalled();
I am getting an error that says
received value must be a mock or spy function. received has value undefined
For
expect(windowSpy.opener.location.relaod).toHaveBeenCalled();
it says:
windowSpy.opener
is not defined.
You're only mocking window but you're not providing any implementation.
This should help: