Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render returns empty div as container when passed a portal/modal as component #126

Closed
ekafeel opened this issue Jun 26, 2018 · 2 comments · Fixed by #127
Closed

Render returns empty div as container when passed a portal/modal as component #126

ekafeel opened this issue Jun 26, 2018 · 2 comments · Fixed by #127

Comments

@ekafeel
Copy link

ekafeel commented Jun 26, 2018

  • react-testing-library 4.0.2:
  • react 16.3.2:
  • node 8.10.0:
  • npm 5.6.0:

Relevant code or config:

const renderedComponent = render(
  <SomePortal />
);

What you did:

expect(renderedComponent.container).toMatchSnapshot()

What happened:

renderedComponent.container is an empty div in that case. The portal/modal rendered in the document.body and all the queries were generated from the base element which, in the latest version is baseElement = document.documentElement. So all the queries behave as expected, as they look inside documentElement, but the container is an empty div.

Reproduction:

sandbox url: https://codesandbox.io/s/k98yl70j27

Problem description:

So, when we don't pass document.body as container to the render method, which I believe is to avoid the warnings around the rendering directly in div. The render just returns an empty div as container.

render method, if not passed a container, returns container by creating a div element in the body. It works fine for all the portal less components which don't render anything in the body. But for the components which create portals and render those in the document.body, the container doesn't include all the rendered DOM. If we are just testing a modal box, e.g., then the container will just be empty.

Suggested solution:

I have two possible solutions in mind.

  1. return baseElement as well from the render
  2. make the container same as baseElement and return that
@ekafeel ekafeel changed the title Render returns empty div as container when passed a portal/modal as component Render returns empty div as container when passed a portal/modal as component Jun 26, 2018
@kentcdodds
Copy link
Member

Thanks for this issue.

return baseElement as well from the render

Let's do that 👍 Would you like to open a PR that adds this change as well as simple documentation?

@ekafeel
Copy link
Author

ekafeel commented Jun 26, 2018

sure, would love to do that!

julienw pushed a commit to julienw/react-testing-library that referenced this issue Dec 20, 2018
This fixes code coverage of testing-library#125 by extracting helper functions.

There are still few istanbul ignores in code but they are not mine :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants