Skip to content

Commit 864b586

Browse files
committed
fixup: polish and simplify
1 parent 13356d6 commit 864b586

File tree

1 file changed

+60
-75
lines changed

1 file changed

+60
-75
lines changed

docs/svelte-testing-library/api.mdx

+60-75
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,13 @@ title: API
44
sidebar_label: API
55
---
66

7-
## `@testing-library/svelte`
7+
`@testing-library/svelte` re-exports everything from
8+
[`@testing-library/dom`][@testing-library/dom], as well as:
89

9-
This library re-exports everything from DOM Testing Library. Check the
10-
[`@testing-library/dom` API documentation][@testing-library/dom] to see what
11-
goodies you can use.
12-
13-
:::info
14-
15-
Unlike other methods which are passed through unmodified,
16-
[`fireEvent`](#fireevent-async) is an `async` method when imported from
17-
`@testing-library/svelte` to ensure Svelte applies new changes to the DOM after
18-
the event is fired.
19-
20-
:::
21-
22-
```js
23-
import {screen, within, fireEvent /* ... */} from '@testing-library/svelte'
24-
```
10+
- [`render`](#render)
11+
- [`act`](#act)
12+
- [`cleanup`](#cleanup)
13+
- [`fireEvent` (async)](#fireevent-async)
2514

2615
[@testing-library/dom]: ../dom-testing-library/api.mdx
2716

@@ -94,31 +83,32 @@ Prior to `@testing-library/[email protected]`, the `baseElement` option was named
9483

9584
### Render Results
9685

97-
| Result | Description |
98-
| ----------------------------- | ----------------------------------------------------- |
99-
| [`baseElement`](#baseelement) | The base DOM element used for queries. |
100-
| [`component`](#component) | The created Svelte component. |
101-
| [`container`](#container) | The DOM element the component is mounted to. |
102-
| [`debug`](#debug) | Logs the `baseElement` using [prettyDom][pretty-dom]. |
103-
| [`rerender`](#rerender) | Update the component's props . |
104-
| [`unmount`](#unmount) | Unmount and destroy the component. |
105-
| [`...queries`](#queries) | [Query functions][queries] bound to `baseElement`. |
86+
| Result | Description |
87+
| ----------------------------- | ---------------------------------------------------------- |
88+
| [`baseElement`](#baseelement) | The base DOM element used for queries. |
89+
| [`component`](#component) | The mounted Svelte component. |
90+
| [`container`](#container) | The DOM element the component is mounted to. |
91+
| [`debug`](#debug) | Log elements using [`prettyDOM`][pretty-dom]. |
92+
| [`rerender`](#rerender) | Update the component's props. |
93+
| [`unmount`](#unmount) | Unmount and destroy the component. |
94+
| [`...queries`](#queries) | [Query functions][query-functions] bound to `baseElement`. |
10695

10796
[pretty-dom]: ../dom-testing-library/api-debugging.mdx#prettydom
108-
[queries]: ../queries/about.mdx
97+
[query-functions]: ../queries/about.mdx
10998

11099
#### `baseElement`
111100

112101
_Added in `@testing-library/[email protected]`_
113102

114103
The base DOM element that queries are bound to. Corresponds to
115-
`renderOptions.baseElement`.
104+
`renderOptions.baseElement`. If you do not use `componentOptions.target` nor
105+
`renderOptions.baseElement`, this will be `document.body`.
116106

117107
#### `container`
118108

119109
The DOM element the component is mounted in. Corresponds to
120-
`componentOptions.target`. In general, avoid using `container` to query for
121-
elements; use [testing-library queries][queries] instead.
110+
`componentOptions.target`. In general, avoid using `container` directly to query
111+
for elements; use [testing-library queries][query-functions] instead.
122112

123113
:::tip
124114

@@ -129,8 +119,8 @@ Use `container.firstChild` to get the first rendered element of your component.
129119
:::caution
130120

131121
Prior to `@testing-library/[email protected]`, `container` was set to the base
132-
element, which made the first rendered element of the component
133-
`container.firstChild.firstChild`.
122+
elemen. Use `container.firstChild.firstChild` to get the first rendered element
123+
of the component in earlier versions.
134124

135125
:::
136126

@@ -142,51 +132,48 @@ API][svelte-component-api] for more details.
142132
:::tip
143133

144134
Avoid using `component` except to test developer facing API's, like exported
145-
functions. Instead, prefer of interacting with the DOM. Read [Avoid the Test
146-
User][test-user] by Kent C. Dodds to understand the difference between the **end
147-
user** and **developer user**.
135+
functions. Instead, interact with the DOM. Read [Avoid the Test User][test-user]
136+
by Kent C. Dodds to understand the difference between the **end user** and
137+
**developer user**.
148138

149139
:::
150140

151141
[test-user]: https://kentcdodds.com/blog/avoid-the-test-user
152142

153143
#### `debug`
154144

155-
Log any passed element, or the `baseElement` by default, using
156-
[prettyDOM][pretty-dom].
145+
Log the `baseElement` or a given element using [`prettyDOM`][pretty-dom].
157146

158147
:::tip
159148

160149
If your `baseElement` is the default `document.body`, we recommend using
161-
[`screen.debug`][screen-debug] rather than the bound `debug`..
162-
163-
```js
164-
import {render, screen} from '@testing-library/svelte'
165-
166-
render(MyComponent, {myProp: 'value'})
167-
168-
screen.debug()
169-
```
150+
[`screen.debug`][screen-debug].
170151

171152
:::
172153

173154
```js
155+
import {render, screen} from '@testing-library/svelte'
156+
174157
const {debug} = render(MyComponent, {myProp: 'value'})
175158

176159
const button = screen.getByRole('button')
177160

178-
// log the baseElement
161+
// log `document.body`
162+
screen.debug()
163+
164+
// log your custom `target` or `baseElement`
179165
debug()
180166

181167
// log a specific element
168+
screen.debug(button)
182169
debug(button)
183170
```
184171

185172
[screen-debug]: ../dom-testing-library/api-debugging.mdx#screendebug
186173

187174
#### `rerender`
188175

189-
Update one or more of the component's props.
176+
Update one or more of the component's props and wait for Svelte to update.
190177

191178
```js
192179
const {rerender} = render(MyComponent, {myProp: 'value'})
@@ -196,9 +183,9 @@ await rerender({myProp: 'new value'}))
196183

197184
:::caution
198185

199-
Prior to `@testing-library/[email protected]`, `rerender` would umount the component.
200-
To update props witohut unmounting in earlier versions of
201-
`@testing-library/svelte`, use `component.$set` and [`act`](#act-async):
186+
Prior to `@testing-library/[email protected]`, calling `rerender` would destroy and
187+
remount the component. Use `component.$set` and [`act`](#act) to update props in
188+
earlier versions:
202189

203190
```js
204191
const {component} = render(MyComponent, {myProp: 'value'})
@@ -220,54 +207,52 @@ unmount()
220207

221208
#### Queries
222209

223-
[Query functions][queries] bound to the `baseElement`. If you passed [custom
224-
queries][custom-queries] to `render`, those will be available instead of the
225-
default queries.
210+
[Query functions][query-functions] bound to the [`baseElement`](#baseelement).
211+
If you passed [custom queries][custom-queries] to `render`, those will be
212+
available instead of the default queries.
226213

227214
:::tip
228215

229-
If your `baseElement` is the default `document.body`, we recommend using
230-
[`screen`][screen] rather than bound queries.
216+
If your [`baseElement`](#baseelement) is the default `document.body`, we
217+
recommend using [`screen`][screen] rather than bound queries.
218+
219+
:::
231220

232221
```js
233222
import {render, screen} from '@testing-library/svelte'
234223

235-
render(MyComponent, {myProp: 'value'})
224+
const {getByRole} = render(MyComponent, {myProp: 'value'})
236225

226+
// query `document.body`
237227
const button = screen.getByRole('button')
238-
```
239-
240-
:::
241-
242-
```js
243-
const {getByRole} = render(MyComponent, {myProp: 'value'})
244228

229+
// query using a custom `target` or `baseElement`
245230
const button = getByRole('button')
246231
```
247232

248233
[screen]: ../queries/about.mdx#screen
249234

250235
## `cleanup`
251236

252-
Unmount all components and remove any elements added to `document.body`.
237+
Destroy all components and remove any elements added to `document.body`.
253238

254239
:::info
255240

256241
`cleanup` is called automatically if your testing framework adds a global
257242
`afterEach` method (e.g. Mocha, Jest, or Jasmine), or if you use
258243
`import '@testing-library/svelte/vitest'` in your [Vitest setup
259-
file][vitest-setup].
244+
file][vitest-setup]. Usually, you shouldn't need to call `cleanup` yourself.
260245

261-
If you'd like to disable automatic cleanup in a framework that uses the
262-
`afterEach` global method, set `process.env.STL_SKIP_AUTO_CLEANUP`.
246+
If you'd like to disable automatic cleanup in a framework that uses a global
247+
`afterEach` method, set `process.env.STL_SKIP_AUTO_CLEANUP`.
263248

264249
:::
265250

266251
```js
267252
import {render, cleanup} from '@testing-library/svelte'
268253

269254
// Default: runs after each test
270-
afterEach(async () => {
255+
afterEach(() => {
271256
cleanup()
272257
})
273258

@@ -279,20 +264,20 @@ cleanup()
279264

280265
[vitest-setup]: ./setup.mdx#vitest
281266

282-
## `act` (async)
267+
## `act`
283268

284269
Ensure all pending updates from Svelte are applied to the DOM. Optionally, you
285-
may pass a function to be called before flushing updates. If the function
286-
returns a `Promise`, that promise will be resolved before flushing updates.
270+
may pass a function to be called before updates are applied. If the function
271+
returns a `Promise`, it will be resolved first.
287272

288-
Uses Svelte's [`tick`][svelte-tick] method to flush updates.
273+
Uses Svelte's [`tick`][svelte-tick] method to apply updates.
289274

290275
```js
291276
import {act, render} from '@testing-library/svelte'
292277

293278
const {component} = render(MyComponent)
294279

295-
act(() => {
280+
await act(() => {
296281
component.updateSomething()
297282
})
298283
```
@@ -301,8 +286,8 @@ act(() => {
301286

302287
## `fireEvent` (async)
303288

304-
Call DOM Testing Library's [`fireEvent`][fire-event], wrapped in
305-
[`act`](#act-async) to ensure Svelte updates the DOM.
289+
Fire an event and wait for Svelte to update the DOM. An asynchronous wrapper of
290+
DOM Testing Library's [`fireEvent`][fire-event].
306291

307292
:::tip
308293

0 commit comments

Comments
 (0)