|
16 | 16 | [![downloads][downloads-badge]][npmtrends]
|
17 | 17 | [![MIT License][license-badge]][license]
|
18 | 18 |
|
19 |
| -[](#contributors) |
| 19 | +[](#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]
|
@@ -88,6 +88,7 @@ facilitate testing implementation details). Read more about this in
|
88 | 88 | * [`wait`](#wait)
|
89 | 89 | * [`waitForElement`](#waitforelement)
|
90 | 90 | * [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
|
| 91 | + * [`prettyDOM`](#prettydom) |
91 | 92 | * [`TextMatch`](#textmatch)
|
92 | 93 | * [`query` APIs](#query-apis)
|
93 | 94 | * [Examples](#examples)
|
@@ -130,7 +131,7 @@ import Fetch from '../fetch' // see the tests for a full implementation
|
130 | 131 |
|
131 | 132 | test('Fetch makes an API call and displays the greeting when load-greeting is clicked', async () => {
|
132 | 133 | // Arrange
|
133 |
| - axiosMock.get.mockResolvedValueOnce({ data: { greeting: 'hello there' } }) |
| 134 | + axiosMock.get.mockResolvedValueOnce({data: {greeting: 'hello there'}}) |
134 | 135 | const url = '/greeting'
|
135 | 136 | const {getByText, getByTestId, container} = render(<Fetch url={url} />)
|
136 | 137 |
|
@@ -164,6 +165,21 @@ The containing DOM node of your rendered React Element (rendered using
|
164 | 165 |
|
165 | 166 | > Tip: To get the root element of your rendered element, use `container.firstChild`.
|
166 | 167 |
|
| 168 | +#### `debug` |
| 169 | + |
| 170 | +This method is a shortcut for `console.log(prettyDOM(container))`. |
| 171 | + |
| 172 | +```javascript |
| 173 | +const HelloWorld = () => <h1>Hello World</h1> |
| 174 | +const {debug} = render(<HelloWorld />) |
| 175 | +debug() |
| 176 | +// <div> |
| 177 | +// <h1>Hello World</h1> |
| 178 | +// </div> |
| 179 | +``` |
| 180 | + |
| 181 | +Learn more about [`prettyDOM`](#prettydom) below. |
| 182 | + |
167 | 183 | #### `rerender`
|
168 | 184 |
|
169 | 185 | It'd probably be better if you test the component that's doing the prop updating
|
@@ -468,6 +484,32 @@ fireEvent.click(getElementByText('Submit'), rightClick)
|
468 | 484 | // default `button` property for click events is set to `0` which is a left click.
|
469 | 485 | ```
|
470 | 486 |
|
| 487 | +### `prettyDOM` |
| 488 | + |
| 489 | +This helper function can be used to print out readable representation of the DOM |
| 490 | +tree of a node. This can be helpful for instance when debugging tests. |
| 491 | + |
| 492 | +It is defined as: |
| 493 | + |
| 494 | +```typescript |
| 495 | +function prettyDOM(node: HTMLElement, maxLength?: number): string |
| 496 | +``` |
| 497 | + |
| 498 | +It receives the root node to print out, and an optional extra argument to limit |
| 499 | +the size of the resulting string, for cases when it becomes too large. |
| 500 | + |
| 501 | +This function is usually used alongside `console.log` to temporarily print out |
| 502 | +DOM trees during tests for debugging purposes: |
| 503 | + |
| 504 | +```javascript |
| 505 | +const HelloWorld = () => <h1>Hello World</h1> |
| 506 | +const {container} = render(<HelloWorld />) |
| 507 | +console.log(prettyDOM(container)) |
| 508 | +// <div> |
| 509 | +// <h1>Hello World</h1> |
| 510 | +// </div> |
| 511 | +``` |
| 512 | +
|
471 | 513 | ## `TextMatch`
|
472 | 514 |
|
473 | 515 | Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
|
@@ -808,7 +850,7 @@ Thanks goes to these people ([emoji key][emojis]):
|
808 | 850 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
809 | 851 | | [<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") |
|
810 | 852 | | [<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") |
|
811 |
| -| [<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") | |
| 853 | +| [<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") | |
812 | 854 |
|
813 | 855 | <!-- ALL-CONTRIBUTORS-LIST:END -->
|
814 | 856 |
|
|
0 commit comments