How to check existence of portalled elements with testing libraries? #1130
-
Hi, folks! Hope you're doing well 👋 I'm using Testing-Library/React to perform integration tests in my application. I would need to validate some internal rules where some user permissions impact what's rendered inside a I need to have it I don't use the portal utility yet. In my DOM, I have a I noticed that the ReproHere's my simplest repro as of now:
Logs
Also, what would be the best practice you'd recommend to properly manage portals inside tests? A few moments Later...Portal's implementation here This specific line made me wonder whether my testing environment doesn't follow the expected structure 😕
Test
Logs
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 16 replies
-
Hey there, you can use the container option upon rendering the dropdown component const {container} = render(<Dropdown {...props} />, {
container: document.body,
}); |
Beta Was this translation helpful? Give feedback.
-
Hey! Facing same problem, did you solve how to get access portal in tests ? |
Beta Was this translation helpful? Give feedback.
-
Mocking the portal worked for me for Dialog and dropdown. We just wanted to check the functionality of the component as to what all get renders inside portal. Below is the mock: import { forwardRef } from "react";
import { PortalProps } from "@radix-ui/react-portal";
jest.mock("@radix-ui/react-portal", () => ({
...jest.requireActual("@radix-ui/react-portal"),
Portal: forwardRef<HTMLDivElement, PortalProps>(function PortalMock(props, ref) {
return <>{props.children}</>;
}),
})); |
Beta Was this translation helpful? Give feedback.
Mocking the portal worked for me for Dialog and dropdown. We just wanted to check the functionality of the component as to what all get renders inside portal. Below is the mock: