Testing React Portal opening in a new window using useEffect
I'm having a bit of trouble trying to test a reusable React Portal component for my project that has a useEffect that renders the portal in a new window, as follows:
import { useEffect, useRef } from 'react';
import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';
const Portal = ({ onClose, pageTitle, portalIndex, children }) => {
const container = useRef(document.createElement('div'));
useEffect(() => {
const newWindow = window.open('', '_blank', '');
newWindow.document.body.appendChild(container.current);
newWindow.document.title = pageTitle;
newWindow.addEventListener('beforeunload', () =>
onClose(portalIndex),
);
return () => {
newWindow.removeEventListener('beforeunload', onClose);
newWindow.close();
onClose(portalIndex);
};
}, [pageTitle, portalIndex]);
return createPortal(children, container.current);
};
export default Portal;
The code itself executes perfectly in the browser, yet when simply trying to render the component as follows in my test suite:
test('Test portal', () => {
const { container } = render(
<Portal
key={0}
pageTitle="Test Portal"
portalIndex={0}
onClose={mockFn}
>
This is a React Portal
<Button
dataTestId="button-test-id"
text="Toggle Portal"
onClick={mockFn}
/>
</Portal>,
);
expect(container).toHaveTextContent('This is a React Portal');
});
The following error is displayed in the console, which is pretty obvious from the explanation:
TypeError: Cannot read property 'document' of undefined
23 | const newWindow = window.open('', '_blank', '');
24 |
> 25 | newWindow.document.body.appendChild(container.current);
I've looked at countless articles in regards how to mock the window itself using Jest, where I've tried that many different possible solutions but none of which seem to work.
Any help to try and achieve the above would be great :)
@richardansell the way i would approach it is by wrapping const newWindow = window.open('', '_blank', ''); in a helper function, which gives us the ability to mock the newWindow object, with properties needed and complete the test by making sure the properties are called. hope this helps :)
Hi @richardansell,
This is a challenging one. I'd say @renjithabby's suggestion is a reasonable one.
Because this isn't something we can do anything about in React Testing Library (it has more to do with your testing environment) I'm going to go ahead and close this.
Another thing to consider is testing this with a real browser. I'm not sure whether Cypress could do this, but you may be able to do this with TestCafe or puppeteer. (There are testing library implementations for each of those tools).
Good luck!
Thanks for the reply @renjithabby / @kentcdodds .
@renjithabby
I have tried the suggestion regarding mocking the window object, which proved to deliver the same result as before following further testing.
@kentcdodds
Thank you for your response to this as it does appear it's clearly not an issue with the library but something else that I haven't come across about how best to solve it.
I've actually tried testing it using Cypress as that's the e2e tool we use, yet it doesn't open the portal in a new window and instead opens an exact same instance of the current window. I haven't spent a great deal of time investigating this in Cypress as my aim was to unit test the portal itself instead.
Either way, I'll investigate this further in due course and will post here if I come up with a solution for other readers.
Thanks again.
@richardansell
hey, I am having exactly same issue.
did you figure out the solution for this?
if so, could you advise me to indicate right direction?
Thank you
@richardansell hey, I am also having exactly the same issue. I would really appreciate your help if you can indicate the right direction.
Thank you