@@ -68,6 +68,10 @@ facilitate testing implementation details). Read more about this in
68
68
2 . Specific to a testing framework (though we recommend Jest as our
69
69
preference, the library works with any framework)
70
70
71
+ > NOTE: This library is built on top of
72
+ > [ ` dom-testing-library ` ] ( https://github.com/kentcdodds/dom-testing-library )
73
+ > which is where most of the logic behind the queries is.
74
+
71
75
## Table of Contents
72
76
73
77
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
@@ -79,17 +83,10 @@ facilitate testing implementation details). Read more about this in
79
83
* [ ` render ` ] ( #render )
80
84
* [ ` Simulate ` ] ( #simulate )
81
85
* [ ` wait ` ] ( #wait )
82
- * [ Custom Jest Matchers] ( #custom-jest-matchers )
83
- * [ ` toBeInTheDOM ` ] ( #tobeinthedom )
84
- * [ ` toHaveTextContent ` ] ( #tohavetextcontent )
85
- * [ ` toHaveAttribute ` ] ( #tohaveattribute )
86
- * [ Custom Jest Matchers - Typescript] ( #custom-jest-matchers---typescript )
87
86
* [ ` TextMatch ` ] ( #textmatch )
88
87
* [ ` query ` APIs] ( #query-apis )
89
88
* [ Examples] ( #examples )
90
89
* [ FAQ] ( #faq )
91
- * [ Deprecated APIs] ( #deprecated-apis )
92
- * [ ` flushPromises ` ] ( #flushpromises )
93
90
* [ Other Solutions] ( #other-solutions )
94
91
* [ Guiding Principles] ( #guiding-principles )
95
92
* [ Contributors] ( #contributors )
@@ -108,13 +105,17 @@ npm install --save-dev react-testing-library
108
105
109
106
This library has a ` peerDependencies ` listing for ` react-dom ` .
110
107
108
+ You may also be interested in installing ` dom-testing-library ` so you can use
109
+ [ the custom jest matchers] ( https://github.com/kentcdodds/dom-testing-library/blob/master/README.md#custom-jest-matchers )
110
+
111
111
## Usage
112
112
113
113
``` javascript
114
114
// __tests__/fetch.js
115
115
import React from ' react'
116
116
import {render , Simulate , wait } from ' react-testing-library'
117
- import ' react-testing-library/extend-expect' // this adds custom expect matchers
117
+ // this add custom expect matchers from dom-testing-library
118
+ import ' dom-testing-library/extend-expect'
118
119
import axiosMock from ' axios' // the mock lives in a __mocks__ directory
119
120
import Fetch from ' ../fetch' // see the tests for a full implementation
120
121
@@ -312,94 +313,6 @@ The default `interval` is `50ms`. However it will run your callback immediately
312
313
on the next tick of the event loop (in a ` setTimeout ` ) before starting the
313
314
intervals .
314
315
315
- ## Custom Jest Matchers
316
-
317
- There are two simple API which extend the ` expect ` API of jest for making assertions easier .
318
-
319
- ### ` toBeInTheDOM `
320
-
321
- This allows you to assert whether an element present in the DOM or not .
322
-
323
- ` ` ` javascript
324
- // add the custom expect matchers
325
- import 'react-testing-library/extend-expect'
326
-
327
- // ...
328
- const {queryByTestId} = render(<span data-testid="count-value">2</span>)
329
- expect(queryByTestId('count-value')).toBeInTheDOM()
330
- expect(queryByTestId('count-value1')).not.toBeInTheDOM()
331
- // ...
332
- ` ` `
333
-
334
- > Note : when using ` toBeInTheDOM ` , make sure you use a query function
335
- > (like ` queryByTestId ` ) rather than a get function (like ` getByTestId ` ).
336
- > Otherwise the `get* ` function could throw an error before your assertion.
337
-
338
- ### `toHaveTextContent`
339
-
340
- This API allows you to check whether the given element has a text content or not.
341
-
342
- ```javascript
343
- // add the custom expect matchers
344
- import 'react-testing-library/extend-expect'
345
-
346
- // ...
347
- const {getByTestId } = render (<span data -testid =" count-value" >2 < / span > )
348
- expect (getByTestId (' count-value' )).toHaveTextContent (' 2' )
349
- expect (getByTestId (' count-value' )).not .toHaveTextContent (' 21' )
350
- // ...
351
- ` ` `
352
-
353
- ### ` toHaveAttribute `
354
-
355
- This allows you to check wether the given element has an attribute or not. You
356
- can also optionally check that the attribute has a specific expected value.
357
-
358
- ` ` ` javascript
359
- // add the custom expect matchers
360
- import ' react-testing-library/extend-expect'
361
-
362
- // ...
363
- const {getByTestId } = render (
364
- < button data - testid = " ok-button" type = " submit" disabled >
365
- OK
366
- < / button > ,
367
- )
368
- expect (getByTestId (' ok-button' )).toHaveAttribute (' disabled' )
369
- expect (getByTestId (' ok-button' )).toHaveAttribute (' type' , ' submit' )
370
- expect (getByTestId (' ok-button' )).not .toHaveAttribute (' type' , ' button' )
371
- // ...
372
- ` ` `
373
-
374
- ### Custom Jest Matchers - Typescript
375
-
376
- When you use custom Jest Matchers with Typescript, you will need to extend the type signature of ` jest .Matchers <void >` , then cast the result of ` expect ` accordingly. Here's a handy usage example:
377
-
378
- ` ` ` typescript
379
- // this adds custom expect matchers
380
- import ' react-testing-library/extend-expect'
381
- interface ExtendedMatchers extends jest .Matchers < void > {
382
- toHaveTextContent : (htmlElement : string ) => object
383
- toBeInTheDOM : () => void
384
- }
385
- test (' renders the tooltip as expected' , async () => {
386
- const {
387
- // getByLabelText,
388
- getByText,
389
- // getByTestId,
390
- container,
391
- } = render (<Tooltip label =" hello world" >Child < / Tooltip > )
392
- // tests rendering of the child
393
- getByText (' Child' )
394
- // tests rendering of tooltip label
395
- ;(expect (getByText (' hello world' )) as ExtendedMatchers ).toHaveTextContent (
396
- ' hello world' ,
397
- )
398
- // snapshots work great with regular DOM nodes!
399
- expect (container .firstChild ).toMatchSnapshot ()
400
- })
401
- ` ` `
402
-
403
316
## ` TextMatch `
404
317
405
318
Several APIs accept a ` TextMatch ` which can be a ` string ` , ` regex ` or a
@@ -681,27 +594,6 @@ react components.
681
594
682
595
</details >
683
596
684
- ## Deprecated APIs
685
-
686
- ### ` flushPromises `
687
-
688
- > This API was deprecated in favor of [ ` wait ` ](#wait). We try to avoid having
689
- > two ways to do the same thing and you can accomplish everything with ` wait `
690
- > that you could with ` flushPromises ` . A big advantage of ` wait ` , is that
691
- > ` flushPromises ` will not flush promises that have not been queued up already,
692
- > for example, if they will queue up as a result of the initial promises. In
693
- > consequence of that, you might have to call ` flushPromises ` multiple times to
694
- > get your components to your desired state. You can accomplish the exact same
695
- > behavior with ` wait ` as you had with ` flushPromises ` by calling ` wait ` with
696
- > no arguments: ` await wait ()`
697
-
698
- This is a simple utility that's useful for when your component is doing some
699
- async work that you've mocked out, but you still need to wait until the next
700
- tick of the event loop before you can continue your assertions. It simply
701
- returns a promise that resolves in a ` setImmediate ` . Especially useful when
702
- you make your test function an ` async ` function and use
703
- ` await flushPromises ()` .
704
-
705
597
## Other Solutions
706
598
707
599
In preparing this project ,
0 commit comments