Skip to content

Commit

Permalink
feat(debug): add a debug convenience method (#87)
Browse files Browse the repository at this point in the history
Also adds @Gpx to contributors!

Closes #86
  • Loading branch information
Kent C. Dodds committed May 15, 2018
1 parent ad4627a commit de769ec
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@
"bug",
"code"
]
},
{
"login": "Gpx",
"name": "Giorgio Polvara",
"avatar_url": "https://avatars0.githubusercontent.com/u/767959?v=4",
"profile": "https://twitter.com/Gpx",
"contributions": [
"bug",
"ideas"
]
}
]
}
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-25-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-26-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]
[![Join the community on Spectrum][spectrum-badge]][spectrum]
Expand Down Expand Up @@ -88,6 +88,7 @@ facilitate testing implementation details). Read more about this in
* [`wait`](#wait)
* [`waitForElement`](#waitforelement)
* [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
* [`prettyDOM`](#prettydom)
* [`TextMatch`](#textmatch)
* [`query` APIs](#query-apis)
* [Examples](#examples)
Expand Down Expand Up @@ -130,7 +131,7 @@ import Fetch from '../fetch' // see the tests for a full implementation

test('Fetch makes an API call and displays the greeting when load-greeting is clicked', async () => {
// Arrange
axiosMock.get.mockResolvedValueOnce({ data: { greeting: 'hello there' } })
axiosMock.get.mockResolvedValueOnce({data: {greeting: 'hello there'}})
const url = '/greeting'
const {getByText, getByTestId, container} = render(<Fetch url={url} />)

Expand Down Expand Up @@ -164,6 +165,21 @@ The containing DOM node of your rendered React Element (rendered using

> Tip: To get the root element of your rendered element, use `container.firstChild`.
#### `debug`

This method is a shortcut for `console.log(prettyDOM(container))`.

```javascript
const HelloWorld = () => <h1>Hello World</h1>
const {debug} = render(<HelloWorld />)
debug()
// <div>
// <h1>Hello World</h1>
// </div>
```

Learn more about [`prettyDOM`](#prettydom) below.

#### `rerender`

It'd probably be better if you test the component that's doing the prop updating
Expand Down Expand Up @@ -468,6 +484,32 @@ fireEvent.click(getElementByText('Submit'), rightClick)
// default `button` property for click events is set to `0` which is a left click.
```

### `prettyDOM`

This helper function can be used to print out readable representation of the DOM
tree of a node. This can be helpful for instance when debugging tests.

It is defined as:

```typescript
function prettyDOM(node: HTMLElement, maxLength?: number): string
```

It receives the root node to print out, and an optional extra argument to limit
the size of the resulting string, for cases when it becomes too large.

This function is usually used alongside `console.log` to temporarily print out
DOM trees during tests for debugging purposes:

```javascript
const HelloWorld = () => <h1>Hello World</h1>
const {container} = render(<HelloWorld />)
console.log(prettyDOM(container))
// <div>
// <h1>Hello World</h1>
// </div>
```
## `TextMatch`
Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
Expand Down Expand Up @@ -808,7 +850,7 @@ Thanks goes to these people ([emoji key][emojis]):
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<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") |
| [<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") |
| [<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://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") |
<!-- ALL-CONTRIBUTORS-LIST:END -->
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"extends": "./node_modules/kcd-scripts/eslint.js",
"rules": {
"react/prop-types": "off",
"import/no-unassigned-import": "off"
"import/no-unassigned-import": "off",
"import/named": "off"
}
},
"eslintIgnore": [
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react'
import {render} from '../'

beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {})
})

afterEach(() => {
console.log.mockRestore()
})

test('debug pretty prints the container', () => {
const HelloWorld = () => <h1>Hello World</h1>
const {debug} = render(<HelloWorld />)
debug()
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining('Hello World'),
)
})

/* eslint no-console:0 */
16 changes: 6 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import ReactDOM from 'react-dom'
import {Simulate} from 'react-dom/test-utils'
import {bindElementToQueries, wait, fireEvent, waitForElement} from 'dom-testing-library'
import {bindElementToQueries, prettyDOM} from 'dom-testing-library'

function render(ui, {container = document.createElement('div')} = {}) {
ReactDOM.render(ui, container)
return {
container,
// eslint-disable-next-line no-console
debug: () => console.log(prettyDOM(container)),
unmount: () => ReactDOM.unmountComponentAtNode(container),
rerender: rerenderUi => {
render(rerenderUi, {container})
Expand Down Expand Up @@ -40,12 +42,6 @@ syntheticEvents.forEach(eventName => {
})
})

export {
render,
Simulate,
wait,
waitForElement,
fireEvent,
renderIntoDocument,
cleanup,
}
// just re-export everything from dom-testing-library
export * from 'dom-testing-library'
export {render, Simulate, renderIntoDocument, cleanup}

0 comments on commit de769ec

Please sign in to comment.