@@ -14,8 +14,9 @@ as these methods:
14
14
- [ ` debug ` ] ( #debug )
15
15
- [ ` rerender ` ] ( #rerender )
16
16
- [ ` emitted ` ] ( #emitted )
17
+ - [ ` cleanup ` ] ( #cleanup )
17
18
- [ ` container ` ] ( #container-1 )
18
- - [ ` cleanup ` ] ( #cleanup )
19
+ - [ ` cleanup ` ] ( #cleanup-1 )
19
20
20
21
---
21
22
@@ -39,34 +40,26 @@ render(MyTemplate)
39
40
```
40
41
41
42
``` javascript
42
- import { render , cleanup } from ' @marko/testing-library'
43
- import ' @testing-library/jest-dom/extend-expect'
43
+ import { render , screen } from ' @marko/testing-library'
44
44
import Greeting from ' ./greeting.marko'
45
45
46
- afterEach (cleanup)
47
-
48
46
test (' renders a message' , async () => {
49
- const { container , getByText } = await render (Greeting, { name: ' Marko' })
50
- expect (getByText (/ Marko/ )).toBeInTheDocument ()
47
+ const { container } = await render (Greeting, { name: ' Marko' })
48
+ expect (screen . getByText (/ Marko/ )).toBeInTheDocument ()
51
49
expect (container .firstChild ).toMatchInlineSnapshot (`
52
50
<h1>Hello, Marko!</h1>
53
51
` )
54
52
})
55
53
```
56
54
57
- > Note
58
- >
59
- > The [ cleanup] ( #cleanup ) function should be called between tests to remove the
60
- > created DOM nodes and keep the tests isolated.
61
-
62
55
### ` render ` Options
63
56
64
57
You won't often need to specify options, but if you ever do, here are the
65
- available options which you could provide as the third argument to ` render ` .
58
+ available options which you can provide as the third argument to ` render ` .
66
59
67
60
#### ` container `
68
61
69
- By default for client side tests, ` Marko Testing Library ` will create a ` div `
62
+ By default for client- side tests, ` Marko Testing Library ` will create a ` div `
70
63
and append that ` div ` to the ` document.body ` and this is where your component
71
64
will be rendered. If you provide your own HTMLElement ` container ` via this
72
65
option, it will not be appended to the ` document.body ` automatically.
@@ -90,18 +83,28 @@ few properties:
90
83
### ` ...queries `
91
84
92
85
The most important feature of ` render ` is that the queries from
93
- [ DOM Testing Library] ( dom-testing-library/api-queries.mdx ) are automatically
94
- returned with their first argument bound to the results of rendering your
95
- component.
86
+ [ DOM Testing Library] ( queries/about.mdx ) are automatically returned with their
87
+ first argument bound to the results of rendering your component.
96
88
97
- See [ Queries] ( dom-testing-library/api- queries.mdx) for a complete list.
89
+ See [ Queries] ( queries/about .mdx ) for a complete list.
98
90
99
91
** Example**
100
92
101
93
``` javascript
102
94
const { getByLabelText , queryAllByTestId } = await render (MyTemplate)
103
95
```
104
96
97
+ Alternatively, you can use the
98
+ [ top-level ` screen ` method] ( queries/about.mdx#screen ) to query into all
99
+ currently rendered components in the ` document.body ` , eg:
100
+
101
+ ``` javascript
102
+ import { render , screen } from " @marko/testing-library"
103
+
104
+ await render (MyTemplate)
105
+ const el = screen .getByText (... )
106
+ ```
107
+
105
108
### ` debug `
106
109
107
110
This method is a shortcut for logging the ` prettyDOM ` for all children inside of
@@ -119,13 +122,13 @@ debug()
119
122
```
120
123
121
124
This is a simple wrapper around ` prettyDOM ` which is also exposed and comes from
122
- [ ` DOM Testing Library ` ] ( https://github.com/testing-library/ dom-testing-library/blob/master/README.md #prettydom) .
125
+ [ ` DOM Testing Library ` ] ( dom-testing-library/api-debugging.mdx #prettydom ) .
123
126
124
127
### ` rerender `
125
128
126
129
A Marko components ` input ` can change at any time from a parent component.
127
130
Although often this input is passed through your component declaratively,
128
- sometimes it is necessary to ensure that your components reacts appropriately to
131
+ sometimes it is necessary to ensure that your components react appropriately to
129
132
new data. You can simulate your component receiving new ` input ` by passing new
130
133
data to the ` rerender ` helper.
131
134
@@ -160,13 +163,13 @@ const { getByText, emitted } = await render(Counter)
160
163
161
164
const button = getByText (' Increment' )
162
165
163
- fireEvent .click (button)
164
- fireEvent .click (button)
166
+ await fireEvent .click (button)
167
+ await fireEvent .click (button)
165
168
166
169
// Assuming the `Counter` component forwards these button clicks as `increment` events
167
170
expect (emitted (' increment' )).toHaveProperty (' length' , 2 )
168
171
169
- fireEvent .click (button)
172
+ await fireEvent .click (button)
170
173
171
174
// Note: the tracked events are cleared every time you read them.
172
175
// Below we are snapshoting the events after our last assertion,
@@ -212,11 +215,37 @@ expect(emitted()).toMatchInlineSnapshot(`
212
215
` )
213
216
```
214
217
218
+ ### ` cleanup `
219
+
220
+ Like the [ top-level cleanup method] ( #cleanup-1 ) , this allows you to remove and
221
+ destroy the currently rendered component before the test has been completed.
222
+
223
+ This can be useful to validate that a component properly cleans up any DOM
224
+ mutations once it has been destroyed.
225
+
226
+ ``` javascript
227
+ import { render , screen , getRoles } from ' @marko/testing-library'
228
+ import Main from ' ./main.marko'
229
+ import Dialog from ' ./dialog.marko'
230
+
231
+ await render (Main)
232
+
233
+ const main = screen .getByRole (' main' )
234
+ expect (main).not .toHaveAttribute (' aria-hidden' )
235
+
236
+ const { cleanup } = await render (Dialog)
237
+ expect (main).toHaveAttribute (' aria-hidden' ) // assert added attribute
238
+
239
+ cleanup () // destroy the dialog
240
+
241
+ expect (main).not .toHaveAttribute (' aria-hidden' ) // assert attribute removed
242
+ ```
243
+
215
244
### ` container `
216
245
217
- The containing DOM node of your rendered Marko Component. For server side tests
246
+ The containing DOM node of your rendered Marko Component. For server- side tests
218
247
this is a [ JSDOM.fragment] ( https://github.com/jsdom/jsdom#fragment ) , and for
219
- client side tests this will be whatever is passed as the ` container ` render
248
+ client- side tests this will be whatever is passed as the ` container ` render
220
249
option.
221
250
222
251
> Tip: To get the root element of your rendered element, use
@@ -227,30 +256,47 @@ option.
227
256
> changes that will be made to the component you're testing. Avoid using
228
257
> ` container ` to query for elements!
229
258
259
+ ## ` fireEvent `
260
+
261
+ Because Marko batches DOM updates to avoid unnecessary re-renders, the
262
+ [ fireEvent] ( dom-testing-library/api-events.mdx ) helpers are re-exported as
263
+ ` async ` functions. Awaiting this ensures that the DOM has properly updated in
264
+ response to the event triggered in the test.
265
+
266
+ ``` js
267
+ await fireEvent .click (getByText (' Click me' ))
268
+ ```
269
+
230
270
## ` cleanup `
231
271
232
- With client side tests your components are rendered into a placeholder
272
+ With client- side tests your components are rendered into a placeholder
233
273
HTMLElement. To ensure that your components are properly removed, and destroyed,
234
- you can call ` cleanup ` at any time which will remove any attached components.
274
+ after each test the ` cleanup ` method is called for you automatically by hooking
275
+ into ` afterEach ` in supported test frameworks. You can also manually call
276
+ ` cleanup ` at any time which will remove all attached components.
235
277
236
278
``` javascript
237
- import { cleanup } from ' @marko/testing-library'
238
- // automatically unmount and cleanup DOM after the test is finished.
239
- afterEach (cleanup)
240
- ```
279
+ import { render , cleanup , screen } from ' @marko/testing-library'
280
+ import Greeting from ' ./greeting.marko'
241
281
242
- To save some typing, you could also import a file with the above.
282
+ await render (Greeting, { name : ' Marko ' })
243
283
244
- ``` javascript
245
- import ' @marko/testing-library/cleanup-after-each'
284
+ expect (screen .getByText (/ Marko/ )).toBeInTheDocument ()
285
+
286
+ // manually cleanup the component before the test is finished
287
+ cleanup ()
288
+ expect (screen .queryByText (/ Marko/ )).toBeNull ()
246
289
```
247
290
248
- If you are using Jest, you can simply include the following to your Jest config
249
- to avoid doing this in each file.
291
+ You can turn off the automatic test cleanup by importing the following module:
250
292
251
293
``` javascript
252
- module .exports = {
253
- ... ,
254
- setupFilesAfterEnv: [' @marko/testing-library/cleanup-after-each' ]
255
- }
294
+ import ' @marko/testing-library/dont-cleanup-after-each'
256
295
```
296
+
297
+ With mocha you can use ` mocha -r @marko/testing-library/dont-cleanup-after-each `
298
+ as a shorthand.
299
+
300
+ If you are using Jest, you can include
301
+ ` setupFilesAfterEnv: ["@marko/testing-library/dont-cleanup-after-each"] ` in your
302
+ Jest config to avoid doing this in each file.
0 commit comments