|
| 1 | +--- |
| 2 | +id: api |
| 3 | +title: API |
| 4 | +sidebar_label: API |
| 5 | +--- |
| 6 | + |
| 7 | +`@noma.to/qwik-testing-library` re-exports everything from |
| 8 | +[`@testing-library/dom`][@testing-library/dom], as well as: |
| 9 | + |
| 10 | +- [`render`](#render) |
| 11 | +- [`cleanup`](#cleanup) |
| 12 | + |
| 13 | +[@testing-library/dom]: ../dom-testing-library/api.mdx |
| 14 | + |
| 15 | +## `render` |
| 16 | + |
| 17 | +Render your component to the DOM with Qwik. By default, when no options are |
| 18 | +provided, the component will be rendered into a `<host>` appended to |
| 19 | +`document.body`. |
| 20 | + |
| 21 | +```tsx |
| 22 | +import {render} from '@noma.to/qwik-testing-library' |
| 23 | +import {MockProvider} from './MockProvider' |
| 24 | +import {MyComponent} from './MyComponent' |
| 25 | + |
| 26 | +const result = await render(<MyComponent />, { |
| 27 | + baseElement: document.body, |
| 28 | + container: document.createElement('host'), |
| 29 | + wrapper: MockProvider, |
| 30 | +}) |
| 31 | +``` |
| 32 | + |
| 33 | +### Render Options |
| 34 | + |
| 35 | +You may also customize how Qwik Testing Library renders your component. Most of |
| 36 | +the time, you shouldn't need to modify these options. |
| 37 | + |
| 38 | +| Option | Description | Default | |
| 39 | +| ------------- | --------------------------------------------------- | -------------------------------- | |
| 40 | +| `container` | The container in which the component is rendered. | `document.createElement('host')` | |
| 41 | +| `baseElement` | The base element for queries and [`debug`](#debug). | `document.body` | |
| 42 | +| `queries` | [Custom Queries][custom-queries]. | N/A | |
| 43 | +| `wrapper` | The wrapper to provide a context to the component. | N/A | |
| 44 | + |
| 45 | +[custom-queries]: ../dom-testing-library/api-custom-queries.mdx |
| 46 | + |
| 47 | +#### `wrapper` |
| 48 | + |
| 49 | +You can wrap your component into a wrapper to provide a context and other |
| 50 | +functionalities needed by the component under test. |
| 51 | + |
| 52 | +```tsx |
| 53 | +import {render} from '@noma.to/qwik-testing-library' |
| 54 | +import {QwikCityMockProvider} from '@builder.io/qwik-city' |
| 55 | + |
| 56 | +await render(<MyComponent />, {wrapper: QwikCityMockProvider}) |
| 57 | +``` |
| 58 | + |
| 59 | +### Render Results |
| 60 | + |
| 61 | +| Result | Description | |
| 62 | +| ----------------------------- | ---------------------------------------------------------- | |
| 63 | +| [`baseElement`](#baseelement) | The base DOM element used for queries. | |
| 64 | +| [`container`](#container) | The DOM element the component is mounted to. | |
| 65 | +| [`asFragment`](#asFragment) | Convert the DOM element to a `DocumentFragment`. | |
| 66 | +| [`debug`](#debug) | Log elements using [`prettyDOM`][pretty-dom]. | |
| 67 | +| [`unmount`](#unmount) | Unmount and destroy the component. | |
| 68 | +| [`...queries`](#queries) | [Query functions][query-functions] bound to `baseElement`. | |
| 69 | + |
| 70 | +[pretty-dom]: ../dom-testing-library/api-debugging.mdx#prettydom |
| 71 | +[query-functions]: ../queries/about.mdx |
| 72 | + |
| 73 | +#### `baseElement` |
| 74 | + |
| 75 | +The base DOM element that queries are bound to. Corresponds to |
| 76 | +`renderOptions.baseElement`. If you do not use `renderOptions.baseElement`, this |
| 77 | +will be `document.body`. |
| 78 | + |
| 79 | +#### `container` |
| 80 | + |
| 81 | +The DOM element the component is mounted in. Corresponds to |
| 82 | +`renderOptions.container`. If you do not use `renderOptions.container`, this |
| 83 | +will be `document.createElement('host')`. In general, avoid using `container` |
| 84 | +directly to query for elements; use [testing-library queries][query-functions] |
| 85 | +instead. |
| 86 | + |
| 87 | +#### `asFragment` |
| 88 | + |
| 89 | +Returns a `DocumentFragment` of your rendered component. This can be useful if |
| 90 | +you need to avoid live bindings and see how your component reacts to events. |
| 91 | + |
| 92 | +```tsx |
| 93 | +import {component$} from '@builder.io/qwik'; |
| 94 | +import {render} from '@testing-library/react'; |
| 95 | +import {userEvent} from "@testing-library/user-event"; |
| 96 | + |
| 97 | +const TestComponent = component$(() => { |
| 98 | + const count = useSignal(0); |
| 99 | + |
| 100 | + return ( |
| 101 | + <button onClick$={() => (count.value++))}> |
| 102 | + Click to increase: {count} |
| 103 | + </button> |
| 104 | + ) |
| 105 | +}); |
| 106 | + |
| 107 | +const {getByText, asFragment} = await render(<TestComponent />) |
| 108 | +const firstRender = asFragment() |
| 109 | + |
| 110 | +userEvent.click(getByText(/Click to increase/)) |
| 111 | + |
| 112 | +// This will snapshot only the difference between the first render, and the |
| 113 | +// state of the DOM after the click event. |
| 114 | +// See https://github.com/jest-community/snapshot-diff |
| 115 | +expect(firstRender).toMatchDiffSnapshot(asFragment()) |
| 116 | +``` |
| 117 | + |
| 118 | +#### `debug` |
| 119 | + |
| 120 | +Log the `baseElement` or a given element using [`prettyDOM`][pretty-dom]. |
| 121 | + |
| 122 | +:::tip |
| 123 | + |
| 124 | +If your `baseElement` is the default `document.body`, we recommend using |
| 125 | +[`screen.debug`][screen-debug]. |
| 126 | + |
| 127 | +::: |
| 128 | + |
| 129 | +```tsx |
| 130 | +import {render, screen} from '@noma.to/qwik-testing-library' |
| 131 | + |
| 132 | +const {debug} = await render(<MyComponent />) |
| 133 | + |
| 134 | +const button = screen.getByRole('button') |
| 135 | + |
| 136 | +// log `document.body` |
| 137 | +screen.debug() |
| 138 | + |
| 139 | +// log your custom the `baseElement` |
| 140 | +debug() |
| 141 | + |
| 142 | +// log a specific element |
| 143 | +screen.debug(button) |
| 144 | +debug(button) |
| 145 | +``` |
| 146 | + |
| 147 | +[screen-debug]: ../dom-testing-library/api-debugging.mdx#screendebug |
| 148 | + |
| 149 | +#### `unmount` |
| 150 | + |
| 151 | +Unmount and destroy the Qwik component. |
| 152 | + |
| 153 | +```tsx |
| 154 | +const {unmount} = await render(<MyComponent />) |
| 155 | + |
| 156 | +unmount() |
| 157 | +``` |
| 158 | + |
| 159 | +#### Queries |
| 160 | + |
| 161 | +[Query functions][query-functions] bound to the [`baseElement`](#baseelement). |
| 162 | +If you passed [custom queries][custom-queries] to `render`, those will be |
| 163 | +available instead of the default queries. |
| 164 | + |
| 165 | +:::tip |
| 166 | + |
| 167 | +If your [`baseElement`](#baseelement) is the default `document.body`, we |
| 168 | +recommend using [`screen`][screen] rather than bound queries. |
| 169 | + |
| 170 | +::: |
| 171 | + |
| 172 | +```tsx |
| 173 | +import {render, screen} from '@noma.to/qwik-testing-library' |
| 174 | + |
| 175 | +const {getByRole} = await render(<MyComponent />) |
| 176 | + |
| 177 | +// query `document.body` |
| 178 | +const button = screen.getByRole('button') |
| 179 | + |
| 180 | +// query using a custom `target` or `baseElement` |
| 181 | +const button = getByRole('button') |
| 182 | +``` |
| 183 | + |
| 184 | +[screen]: ../queries/about.mdx#screen |
| 185 | + |
| 186 | +## `cleanup` |
| 187 | + |
| 188 | +Destroy all components and remove any elements added to `document.body` or |
| 189 | +`renderOptions.baseElement`. |
| 190 | + |
| 191 | +```tsx |
| 192 | +import {render, cleanup} from '@noma.to/qwik-testing-library' |
| 193 | + |
| 194 | +// Default: runs after each test |
| 195 | +afterEach(() => { |
| 196 | + cleanup() |
| 197 | +}) |
| 198 | + |
| 199 | +await render(<MyComponent />) |
| 200 | + |
| 201 | +// Called manually for more control |
| 202 | +cleanup() |
| 203 | +``` |
0 commit comments