@@ -14,8 +14,9 @@ as these methods:
1414 - [ ` debug ` ] ( #debug )
1515 - [ ` rerender ` ] ( #rerender )
1616 - [ ` emitted ` ] ( #emitted )
17+ - [ ` cleanup ` ] ( #cleanup )
1718 - [ ` container ` ] ( #container-1 )
18- - [ ` cleanup ` ] ( #cleanup )
19+ - [ ` cleanup ` ] ( #cleanup-1 )
1920
2021---
2122
@@ -39,34 +40,26 @@ render(MyTemplate)
3940```
4041
4142``` 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'
4444import Greeting from ' ./greeting.marko'
4545
46- afterEach (cleanup)
47-
4846test (' 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 ()
5149 expect (container .firstChild ).toMatchInlineSnapshot (`
5250 <h1>Hello, Marko!</h1>
5351 ` )
5452})
5553```
5654
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-
6255### ` render ` Options
6356
6457You 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 ` .
6659
6760#### ` container `
6861
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 `
7063and append that ` div ` to the ` document.body ` and this is where your component
7164will be rendered. If you provide your own HTMLElement ` container ` via this
7265option, it will not be appended to the ` document.body ` automatically.
@@ -90,18 +83,28 @@ few properties:
9083### ` ...queries `
9184
9285The 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.
9688
97- See [ Queries] ( dom-testing-library/api- queries.mdx) for a complete list.
89+ See [ Queries] ( queries/about .mdx ) for a complete list.
9890
9991** Example**
10092
10193``` javascript
10294const { getByLabelText , queryAllByTestId } = await render (MyTemplate)
10395```
10496
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+
105108### ` debug `
106109
107110This method is a shortcut for logging the ` prettyDOM ` for all children inside of
@@ -119,13 +122,13 @@ debug()
119122```
120123
121124This 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 ) .
123126
124127### ` rerender `
125128
126129A Marko components ` input ` can change at any time from a parent component.
127130Although 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
129132new data. You can simulate your component receiving new ` input ` by passing new
130133data to the ` rerender ` helper.
131134
@@ -160,13 +163,13 @@ const { getByText, emitted } = await render(Counter)
160163
161164const button = getByText (' Increment' )
162165
163- fireEvent .click (button)
164- fireEvent .click (button)
166+ await fireEvent .click (button)
167+ await fireEvent .click (button)
165168
166169// Assuming the `Counter` component forwards these button clicks as `increment` events
167170expect (emitted (' increment' )).toHaveProperty (' length' , 2 )
168171
169- fireEvent .click (button)
172+ await fireEvent .click (button)
170173
171174// Note: the tracked events are cleared every time you read them.
172175// Below we are snapshoting the events after our last assertion,
@@ -212,11 +215,37 @@ expect(emitted()).toMatchInlineSnapshot(`
212215 ` )
213216```
214217
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+
215244### ` container `
216245
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
218247this 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
220249option.
221250
222251> Tip: To get the root element of your rendered element, use
@@ -227,30 +256,47 @@ option.
227256> changes that will be made to the component you're testing. Avoid using
228257> ` container ` to query for elements!
229258
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+
230270## ` cleanup `
231271
232- With client side tests your components are rendered into a placeholder
272+ With client- side tests your components are rendered into a placeholder
233273HTMLElement. 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.
235277
236278``` 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'
241281
242- To save some typing, you could also import a file with the above.
282+ await render (Greeting, { name : ' Marko ' })
243283
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 ()
246289```
247290
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:
250292
251293``` javascript
252- module .exports = {
253- ... ,
254- setupFilesAfterEnv: [' @marko/testing-library/cleanup-after-each' ]
255- }
294+ import ' @marko/testing-library/dont-cleanup-after-each'
256295```
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