Skip to content

Commit d468a41

Browse files
authored
testing through portal (#716)
1 parent a568ba2 commit d468a41

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/routes/guides/testing.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,41 @@ If possible, try to select for accessible attributes (roughly in the following o
239239

240240
For more information, check the [testing-library documentation](https://testing-library.com/docs/queries/about).
241241

242+
#### Testing through Portal
243+
244+
Solid allows components to break through the DOM tree structure using [`<Portal>`](/reference/components/portal). This mechanism will still work in testing, so the content of the portals will break out of the testing container. In order to test this content, make sure to use the `screen` export to query the contents:
245+
246+
<TabsCodeBlocks>
247+
<div id="Toast.test.jsx">
248+
```jsx frame="none"
249+
import { test, expect } from "vitest"
250+
import { render, screen } from "@solidjs/testing-library"
251+
import { Toast } from './Toast'
252+
253+
test("increments value", async () => {
254+
render(() => <Toast><p>This is a toast</p></Toast>)
255+
const toast = screen.getByRole("log")
256+
expect(toast).toHaveTextContent("This is a toast")
257+
})
258+
```
259+
</div>
260+
<div id="Toast.jsx">
261+
```jsx frame="none"
262+
import { Portal } from "solid-js/web";
263+
264+
export const Toast = (props) => {
265+
return (
266+
<Portal>
267+
<div class="toast" role={props.role ?? "log"}>
268+
{props.children}
269+
</div>
270+
</Portal>
271+
);
272+
}
273+
```
274+
</div>
275+
</TabsCodeBlocks>
276+
242277
#### Testing in context
243278
244279
If a component relies on some context, to wrap it use the `wrapper` option:

0 commit comments

Comments
 (0)