diff --git a/.all-contributorsrc b/.all-contributorsrc index 07aae31b..01f8dcda 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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" + ] } ] } diff --git a/README.md b/README.md index a814fe0a..22a9d3ff 100644 --- a/README.md +++ b/README.md @@ -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] @@ -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) @@ -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() @@ -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 = () =>

Hello World

+const {debug} = render() +debug() +//
+//

Hello World

+//
+``` + +Learn more about [`prettyDOM`](#prettydom) below. + #### `rerender` 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) // 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 = () =>

Hello World

+const {container} = render() +console.log(prettyDOM(container)) +//
+//

Hello World

+//
+``` + ## `TextMatch` 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]): | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [
Anto Aravinth](https://github.com/antoaravinth)
[πŸ’»](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") | [
Jonah Moses](https://github.com/JonahMoses)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | [
Łukasz Gandecki](http://team.thebrain.pro)
[πŸ’»](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") | [
Ivan Babak](https://sompylasar.github.io)
[πŸ›](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [πŸ€”](#ideas-sompylasar "Ideas, Planning, & Feedback") | [
Jesse Day](https://github.com/jday3)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=jday3 "Code") | [
Ernesto GarcΓ­a](http://gnapse.github.io)
[πŸ’¬](#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") | [
Josef Maxx Blake](http://jomaxx.com)
[πŸ’»](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") | | [
Michal Baranowski](https://twitter.com/baranovskim)
[πŸ“](#blog-mbaranovski "Blogposts") [βœ…](#tutorial-mbaranovski "Tutorials") | [
Arthur Puthin](https://github.com/aputhin)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=aputhin "Documentation") | [
Thomas Chia](https://github.com/thchia)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Code") [πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Documentation") | [
Thiago Galvani](http://ilegra.com/)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=thiagopaiva99 "Documentation") | [
Christian](http://Chriswcs.github.io)
[⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=ChrisWcs "Tests") | [
Alex Krolick](https://alexkrolick.com)
[πŸ’¬](#question-alexkrolick "Answering Questions") [πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=alexkrolick "Documentation") [πŸ’‘](#example-alexkrolick "Examples") [πŸ€”](#ideas-alexkrolick "Ideas, Planning, & Feedback") | [
Johann Hubert Sonntagbauer](https://github.com/johann-sonntagbauer)
[πŸ’»](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") | -| [
Maddi Joyce](http://www.maddijoyce.com)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=maddijoyce "Code") | [
Ryan Vice](http://www.vicesoftware.com)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=RyanAtViceSoftware "Documentation") | [
Ian Wilson](https://ianwilson.io)
[πŸ“](#blog-iwilsonq "Blogposts") [βœ…](#tutorial-iwilsonq "Tutorials") | [
Daniel](https://github.com/InExtremaRes)
[πŸ›](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AInExtremaRes "Bug reports") [πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=InExtremaRes "Code") | +| [
Maddi Joyce](http://www.maddijoyce.com)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=maddijoyce "Code") | [
Ryan Vice](http://www.vicesoftware.com)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=RyanAtViceSoftware "Documentation") | [
Ian Wilson](https://ianwilson.io)
[πŸ“](#blog-iwilsonq "Blogposts") [βœ…](#tutorial-iwilsonq "Tutorials") | [
Daniel](https://github.com/InExtremaRes)
[πŸ›](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AInExtremaRes "Bug reports") [πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=InExtremaRes "Code") | [
Giorgio Polvara](https://twitter.com/Gpx)
[πŸ›](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AGpx "Bug reports") [πŸ€”](#ideas-Gpx "Ideas, Planning, & Feedback") | diff --git a/package.json b/package.json index e94aeae6..5a38f46a 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/__tests__/debug.js b/src/__tests__/debug.js new file mode 100644 index 00000000..4cab1ea9 --- /dev/null +++ b/src/__tests__/debug.js @@ -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 = () =>

Hello World

+ const {debug} = render() + debug() + expect(console.log).toHaveBeenCalledTimes(1) + expect(console.log).toHaveBeenCalledWith( + expect.stringContaining('Hello World'), + ) +}) + +/* eslint no-console:0 */ diff --git a/src/index.js b/src/index.js index 4a637b90..8733b67e 100644 --- a/src/index.js +++ b/src/index.js @@ -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}) @@ -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}