[Bug]: [tests] within(canvasElement)
cannot find elements rendered via portal to the document.body
#27024
Replies: 7 comments
-
Instead of |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I actually seems my types issue are not related to Storybook version. when I use I'm using:
|
Beta Was this translation helpful? Give feedback.
-
UPDATE: @valentinpalkovic - Okay, I'm not sure where did I get that other stackblitz example from; but here is an up-to-date as of today: https://stackblitz.com/edit/github-yrt7dh?file=src%2Fstories%2FPage.stories.ts&preset=node Which is failing the same TypeScript error I'm experiencing locally |
Beta Was this translation helpful? Give feedback.
-
@csantos1113 the error isn't coming from any of the libraries you mentioned but from the nature of DOM being untyped. TypeScript can't guarantee that your I see 2 possible options here:
I had a very similar problem when testing Radix components that use portals. You may find some inspiration in this PR. |
Beta Was this translation helpful? Give feedback.
-
I will close this issue since a solution was provided and the Typescript issue was elaborated above |
Beta Was this translation helpful? Give feedback.
-
For anyone else (or future me) looking for another solution, I've found that this works well: const body = within(canvasElement.ownerDocument.body); |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
https://github.com/storybookjs/storybook/tree/next/code/lib/test says I should be using
within(canvasElement)
in my play functions, instead ofscreen
. but it doesn't work for portal elements rendered to the document.bodyTo Reproduce
screen.getByText
works, but shows a warning in the console saying that I should usewithin(canvasElement)
insteadcanvas.getByText
fails because the element is rendered via portal to the document.bodySystem
Additional context
Real live case: I'm opening a Dialog/Drawer which is rendered via portals.
Any recommendation on how to reach elements rendered via portals to the document.body that adheres to the expected behavior defined by Storybook?
Beta Was this translation helpful? Give feedback.
All reactions