16
16
[ ![ downloads] [ downloads-badge ]] [ npmtrends ]
17
17
[ ![ MIT License] [ license-badge ]] [ license ]
18
18
19
- [ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-27 -orange.svg?style=flat-square )] ( #contributors )
19
+ [ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-28 -orange.svg?style=flat-square )] ( #contributors )
20
20
[ ![ PRs Welcome] [ prs-badge ]] [ prs ]
21
21
[ ![ Code of Conduct] [ coc-badge ]] [ coc ]
22
22
[ ![ Join the community on Spectrum] [ spectrum-badge ]] [ spectrum ]
@@ -171,6 +171,8 @@ The containing DOM node of your rendered React Element (rendered using
171
171
This method is a shortcut for ` console.log(prettyDOM(container)) ` .
172
172
173
173
``` javascript
174
+ import {render } from ' react-testing-library'
175
+
174
176
const HelloWorld = () => < h1> Hello World< / h1>
175
177
const {debug } = render (< HelloWorld / > )
176
178
debug ()
@@ -190,6 +192,8 @@ prefer to update the props of a rendered component in your test, this function
190
192
can be used to update props of the rendered component.
191
193
192
194
``` javascript
195
+ import {render } from ' react-testing-library'
196
+
193
197
const {rerender } = render (< NumberDisplay number= {1 } / > )
194
198
195
199
// re-render the same component with different props
@@ -209,6 +213,8 @@ that you don't leave event handlers hanging around causing memory leaks).
209
213
> ` ReactDOM.unmountComponentAtNode `
210
214
211
215
``` javascript
216
+ import {render } from ' react-testing-library'
217
+
212
218
const {container , unmount } = render (< Login / > )
213
219
unmount ()
214
220
// your component has been unmounted and now: container.innerHTML === ''
@@ -220,6 +226,9 @@ This will search for the label that matches the given [`TextMatch`](#textmatch),
220
226
then find the element associated with that label.
221
227
222
228
``` javascript
229
+ import {render } from ' react-testing-library'
230
+
231
+ const {getByLabelText } = render (< Login / > )
223
232
const inputNode = getByLabelText (' Username' )
224
233
225
234
// this would find the input node for the following DOM structures:
@@ -255,7 +264,9 @@ This will search for all elements with a placeholder attribute and find one
255
264
that matches the given [ ` TextMatch ` ] ( #textmatch ) .
256
265
257
266
``` javascript
258
- // <input placeholder="Username" />
267
+ import {render } from ' react-testing-library'
268
+
269
+ const {getByPlaceholderText } = render (< input placeholder= " Username" / > )
259
270
const inputNode = getByPlaceholderText (' Username' )
260
271
```
261
272
@@ -268,7 +279,9 @@ This will search for all elements that have a text node with `textContent`
268
279
matching the given [ ` TextMatch ` ] ( #textmatch ) .
269
280
270
281
``` javascript
271
- // <a href="/about">About ℹ️</a>
282
+ import {render } from ' react-testing-library'
283
+
284
+ const {getByText } = render (< a href= " /about" > About ℹ️< / a> )
272
285
const aboutAnchorNode = getByText (' about' )
273
286
```
274
287
@@ -282,7 +295,11 @@ and [`<area>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area)
282
295
(intentionally excluding [ ` <applet> ` ] ( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/applet ) as it's deprecated).
283
296
284
297
``` javascript
285
- // <img alt="Incredibles 2 Poster" src="/incredibles-2.png" />
298
+ import {render } from ' react-testing-library'
299
+
300
+ const {getByAltText } = render (
301
+ < img alt= " Incredibles 2 Poster" src= " /incredibles-2.png" / > ,
302
+ )
286
303
const incrediblesPosterImg = getByAltText (/ incredibles. * poster$ / i )
287
304
```
288
305
@@ -292,7 +309,9 @@ A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
292
309
also accepts a [ ` TextMatch ` ] ( #textmatch ) ).
293
310
294
311
``` javascript
295
- // <input data-testid="username-input" />
312
+ import {render } from ' react-testing-library'
313
+
314
+ const {getByTestId } = render (< input data- testid= " username-input" / > )
296
315
const usernameInputElement = getByTestId (' username-input' )
297
316
```
298
317
@@ -310,18 +329,22 @@ Render into `document.body`. Should be used with [cleanup](#cleanup).
310
329
` renderIntoDocument ` will return the same object as [ render] ( #render )
311
330
312
331
``` javascript
313
- renderIntoDocument (< div> )
332
+ import {renderIntoDocument } from ' react-testing-library'
333
+
334
+ renderIntoDocument (< div / > )
314
335
```
315
336
316
337
### ` cleanup `
317
338
318
339
Unmounts React trees that were mounted with [ renderIntoDocument] ( #renderintodocument ) .
319
340
320
341
``` javascript
342
+ import {cleanup , renderIntoDocument } from ' react-testing-library'
343
+
321
344
afterEach (cleanup)
322
345
323
346
test (' renders into document' , () => {
324
- renderIntoDocument (< div> )
347
+ renderIntoDocument (< div / > )
325
348
// ...
326
349
})
327
350
```
@@ -366,12 +389,16 @@ around the
366
389
Here ' s a simple example:
367
390
368
391
` ` ` javascript
369
- // ...
370
- // wait until the callback does not throw an error. In this case, that means
371
- // it'll wait until we can get a form control with a label that matches "username"
372
- await wait(() => getByLabelText('username'))
373
- getByLabelText('username').value = 'chucknorris'
374
- // ...
392
+ import {render, wait} from 'react-testing-library'
393
+
394
+ test('waiting for an expectation to pass before proceeding', async () => {
395
+ const {getByLabelText} = render(<Login />)
396
+
397
+ // wait until the callback does not throw an error. In this case, that means
398
+ // it'll wait until we can get a form control with a label that matches "username"
399
+ await wait(() => getByLabelText('username'))
400
+ getByLabelText('username').value = 'chucknorris'
401
+ })
375
402
` ` `
376
403
377
404
This can be useful if you have a unit test that mocks API calls and you need
@@ -396,7 +423,13 @@ intervals.
396
423
See [dom - testing - library #waitForElement ](https :// github.com/kentcdodds/dom-testing-library#waitforelement)
397
424
398
425
` ` ` js
399
- await waitForElement(() => getByText('Search'))
426
+ import {render, waitForElement} from 'react-testing-library'
427
+
428
+ test('waiting for an element', async () => {
429
+ const {getByText} = render(<SearchForm />)
430
+
431
+ await waitForElement(() => getByText('Search'))
432
+ })
400
433
` ` `
401
434
402
435
<details >
@@ -405,6 +438,8 @@ await waitForElement(() => getByText('Search'))
405
438
< / summary >
406
439
407
440
` ` ` diff
441
+ import {render, Simulate, waitForElement} from 'react-testing-library'
442
+
408
443
test('should submit form when valid', async () => {
409
444
const mockSubmit = jest.fn()
410
445
const {
@@ -445,12 +480,7 @@ instead of Synthetic Events. This aligns better with
445
480
> [facebook / react #2043 ](https :// github.com/facebook/react/issues/2043)
446
481
447
482
` ` ` javascript
448
- import {
449
- renderIntoDocument,
450
- cleanup,
451
- render,
452
- fireEvent,
453
- } from 'react-testing-library'
483
+ import {renderIntoDocument, cleanup, fireEvent} from 'react-testing-library'
454
484
455
485
// don't forget to clean up the document.body
456
486
afterEach(cleanup)
@@ -478,6 +508,10 @@ Convenience methods for firing DOM events. Check out
478
508
for a full list as well as default ` eventProperties ` .
479
509
480
510
` ` ` javascript
511
+ import {renderIntoDocument, fireEvent} from 'react-testing-library'
512
+
513
+ const {getElementByText} = renderIntoDocument(<Form />)
514
+
481
515
// similar to the above example
482
516
// click will bubble for React to see it
483
517
const rightClick = {button: 2}
@@ -503,6 +537,8 @@ This function is usually used alongside `console.log` to temporarily print out
503
537
DOM trees during tests for debugging purposes:
504
538
505
539
```javascript
540
+ import {render , prettyDOM } from ' react-testing-library'
541
+
506
542
const HelloWorld = () => <h1 >Hello World < / h1 >
507
543
const {container } = render (<HelloWorld />)
508
544
console .log (prettyDOM (container ))
@@ -521,9 +557,9 @@ See [dom-testing-library#textmatch][dom-testing-lib-textmatch] for options.
521
557
Examples:
522
558
523
559
` ` ` javascript
524
- // <div>
525
- // Hello World
526
- // < /div>
560
+ import { container , getByText } from ' react-testing-library '
561
+
562
+ const { container } = render (< div > Hello World < / div > )
527
563
528
564
// WILL find the div:
529
565
@@ -560,6 +596,9 @@ make an assertion that an element is _not_ present in the DOM, then you can use
560
596
the ` query ` API instead:
561
597
562
598
` ` ` javascript
599
+ import {render } from ' react-testing-library'
600
+
601
+ const {queryByText } = render (<Form />)
563
602
const submitButton = queryByText (' submit' )
564
603
expect (submitButton ).toBeNull () // it doesn't exist
565
604
` ` `
@@ -569,6 +608,9 @@ expect(submitButton).toBeNull() // it doesn't exist
569
608
Each of the ` query ` APIs have a corresponsing ` queryAll ` version that always returns an Array of matching nodes. ` getAll ` is the same but throws when the array has a length of 0.
570
609
571
610
` ` ` javascript
611
+ import {render } from ' react-testing-library'
612
+
613
+ const {queryByText } = render (<Forms />)
572
614
const submitButtons = queryAllByText (' submit' )
573
615
expect (submitButtons ).toHaveLength (3 ) // expect 3 elements
574
616
expect (submitButtons [0 ]).toBeInTheDOM ()
@@ -855,14 +897,12 @@ light-weight, simple, and understandable.
855
897
Thanks goes to these people ([emoji key][emojis]):
856
898
857
899
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
858
-
859
900
<!-- prettier-ignore -->
860
901
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
861
902
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
862
903
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Tests") |
863
904
| [<img src="https://avatars1.githubusercontent.com/u/29602306?v=4" width="100px;"/><br /><sub><b>Michal Baranowski</b></sub>](https://twitter.com/baranovskim)<br />[📝](#blog-mbaranovski "Blogposts") [✅](#tutorial-mbaranovski "Tutorials") | [<img src="https://avatars3.githubusercontent.com/u/13985684?v=4" width="100px;"/><br /><sub><b>Arthur Puthin</b></sub>](https://github.com/aputhin)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=aputhin "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/21194045?v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/20430611?v=4" width="100px;"/><br /><sub><b>Thiago Galvani</b></sub>](http://ilegra.com/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=thiagopaiva99 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/19828824?v=4" width="100px;"/><br /><sub><b>Christian</b></sub>](http://Chriswcs.github.io)<br />[⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=ChrisWcs "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💬](#question-alexkrolick "Answering Questions") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=alexkrolick "Documentation") [💡](#example-alexkrolick "Examples") [🤔](#ideas-alexkrolick "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/1239401?v=4" width="100px;"/><br /><sub><b>Johann Hubert Sonntagbauer</b></sub>](https://github.com/johann-sonntagbauer)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=johann-sonntagbauer "Tests") |
864
- | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars2.githubusercontent.com/u/10080111?v=4" width="100px;"/><br /><sub><b>Ryan Vice</b></sub>](http://www.vicesoftware.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=RyanAtViceSoftware "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/7942604?v=4" width="100px;"/><br /><sub><b>Ian Wilson</b></sub>](https://ianwilson.io)<br />[📝](#blog-iwilsonq "Blogposts") [✅](#tutorial-iwilsonq "Tutorials") | [<img src="https://avatars2.githubusercontent.com/u/1635491?v=4" width="100px;"/><br /><sub><b>Daniel</b></sub>](https://github.com/InExtremaRes)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AInExtremaRes "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=InExtremaRes "Code") | [<img src="https://avatars0.githubusercontent.com/u/767959?v=4" width="100px;"/><br /><sub><b>Giorgio Polvara</b></sub>](https://twitter.com/Gpx)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AGpx "Bug reports") [🤔](#ideas-Gpx "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jgoz "Code") |
865
-
905
+ | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars2.githubusercontent.com/u/10080111?v=4" width="100px;"/><br /><sub><b>Ryan Vice</b></sub>](http://www.vicesoftware.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=RyanAtViceSoftware "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/7942604?v=4" width="100px;"/><br /><sub><b>Ian Wilson</b></sub>](https://ianwilson.io)<br />[📝](#blog-iwilsonq "Blogposts") [✅](#tutorial-iwilsonq "Tutorials") | [<img src="https://avatars2.githubusercontent.com/u/1635491?v=4" width="100px;"/><br /><sub><b>Daniel</b></sub>](https://github.com/InExtremaRes)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AInExtremaRes "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=InExtremaRes "Code") | [<img src="https://avatars0.githubusercontent.com/u/767959?v=4" width="100px;"/><br /><sub><b>Giorgio Polvara</b></sub>](https://twitter.com/Gpx)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AGpx "Bug reports") [🤔](#ideas-Gpx "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jgoz "Code") | [<img src="https://avatars0.githubusercontent.com/u/8203211?v=4" width="100px;"/><br /><sub><b>Sam Horton</b></sub>](https://twitter.com/SavePointSam)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=SavePointSam "Documentation") [💡](#example-SavePointSam "Examples") [🤔](#ideas-SavePointSam "Ideas, Planning, & Feedback") |
866
906
<!-- ALL-CONTRIBUTORS-LIST:END -->
867
907
868
908
This project follows the [all-contributors][all-contributors] specification.
0 commit comments