From c6b662232b8e15c38766b8bbcf096c963197479a Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Tue, 12 Sep 2017 18:52:42 +0900 Subject: [PATCH 01/14] Add ReactTestRenderer documentations --- docs/docs/react-test-renderer.md | 213 +++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 docs/docs/react-test-renderer.md diff --git a/docs/docs/react-test-renderer.md b/docs/docs/react-test-renderer.md new file mode 100644 index 0000000000000..d092f43bda6a1 --- /dev/null +++ b/docs/docs/react-test-renderer.md @@ -0,0 +1,213 @@ +--- +id: react-test-renderer +title: React Test Renderer +permalink: docs/react-test-renderer.html +layout: docs +category: Reference +--- + +**Importing** + +```javascript +import ReactTestRenderer from 'react-test-renderer'; // ES6 +const ReactTestRenderer = require('react-test-renderer'); // ES5 with npm +``` + +## Overview + +This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. + +Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom. + +Example: + +```javascript +const ReactTestRenderer = require('react-test-renderer'); + +const testRenderer = ReactTestRenderer.create( + Facebook +); + +console.log(testRenderer.toJSON()); +// { type: 'a', +// props: { href: 'https://www.facebook.com/' }, +// children: [ 'Facebook' ] } +``` + +You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: http://facebook.github.io/jest/blog/2016/07/27/jest-14.html. + +You can traverse a ReactElement by methods ReactTestInstance provides. + +```javascript +const ReactTestRenderer = require('react-test-renderer'); + +function MyComponent() { + return ( +
+ +

Hello

+
+ ) +} + +function SubComponent() { + return ( +

Sub

+ ); +} + +const testRenderer = ReactTestRenderer.create(); +const testInstance = testRenderer.root; + +console.log(testInstance.findByType(SubComponent).props.foo); // bar +console.log(testInstance.findByProps({className: "sub"}).children); // Sub +``` + + +## Reference + +### `ReactTestRenderer.create()` + +```javascript +ReactTestRenderer.create(element, options); +``` + +Create a React Test Render instance, which has the following methods. +You can pass `createNodeMock` function as the option, which allows for custom mocking behavior. +`createNodeMock` accepts the current element and should return a mock ref object. + +### `testRenderer.toJSON()` + +```javascript +testRenderer.toJSON() +``` + +Return a JSON object representing the element. + +### `testRenderer.toTree()` + +```javascript +testRenderer.toTree() +``` + +Return a tree object representing the element. + +### `testRenderer.update()` + +```javascript +testRenderer.update(element) +``` + +Rerender testRenderer with a passed element. + +### `testRenderer.unmount()` + +```javascript +testRenderer.unmount() +``` + +Unmount the element from testRenderer. + +### `testRenderer.getInstance()` + +```javascript +testRenderer.getInstance() +``` + +Return a root container instance. + +### `testRenderer.root` + +```javascript +testRenderer.root +``` + +`root` is a testInstance, which has the following methods. + +### `testInstance.find()` + +```javascript +testInstance.find(predicate) +``` + +Find a testInstance that matches the provided predicate. + +### `testInstance.findByType()` + +```javascript +testInstance.findByType(type) +``` + +Find a testInstance that matches the provided type. + +### `testInstance.findByProps()` + +```javascript +testInstance.findByProps(props) +``` + +Find a testInstance that matches the provided props. + +### `testInstance.findAll()` + +```javascript +testInstance.findAll(predicate) +``` + +Find all testInstances that matches the provided predicate. + +### `testInstance.findAllByType()` + +```javascript +testInstance.findAllByType(type) +``` + +Find all testInstances that matches the provided type. + +### `testInstance.findAllByProps()` + +```javascript +testInstance.findAllByProps(props) +``` + +Find all testInstances that matches the provided props. + +### `testInstance.instance` + +```javascript +testInstance.instance +``` + +`instance` is a public instance of the testInstance. + +### `testInstance.type` + +```javascript +testInstance.type +``` + +`type` is a Component type of the testInstance. + +### `testInstance.props` + +```javascript +testInstance.props +``` + +`props` is a props object of the testInstance. + +### `testInstance.parent` + +```javascript +testInstance.parent +``` + +`parent` is a parent testInstance of the testInstance. + +### `testInstance.children` + +```javascript +testInstance.children +``` + +`children` is a children of the testInstance. From f8ed90d9b994cf6e6ea8b4ccfd6032954975a4af Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Tue, 12 Sep 2017 18:52:42 +0900 Subject: [PATCH 02/14] Add TestRenderer documentations --- docs/docs/reference-test-renderer.md | 275 +++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 docs/docs/reference-test-renderer.md diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md new file mode 100644 index 0000000000000..1545337a0e47b --- /dev/null +++ b/docs/docs/reference-test-renderer.md @@ -0,0 +1,275 @@ +--- +id: reference-test-renderer +title: Test Renderer +permalink: docs/reference-test-renderer.html +layout: docs +category: Reference +--- + +**Importing** + +```javascript +import TestRenderer from 'react-test-renderer'; // ES6 +const TestRenderer = require('react-test-renderer'); // ES5 with npm +``` + +## Overview + +This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. + +Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom. + +Example: + +```javascript +const TestRenderer = require('react-test-renderer'); + +const testRenderer = TestRenderer.create( + Facebook +); + +console.log(testRenderer.toJSON()); +// { type: 'a', +// props: { href: 'https://www.facebook.com/' }, +// children: [ 'Facebook' ] } +``` + +You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: http://facebook.github.io/jest/blog/2016/07/27/jest-14.html. + +You can also traverse a ReactElement by methods ReactTestInstance provides. + +```javascript +const TestRenderer = require('react-test-renderer'); + +function MyComponent() { + return ( +
+ +

Hello

+
+ ) +} + +function SubComponent() { + return ( +

Sub

+ ); +} + +const testRenderer = TestRenderer.create(); +const testInstance = testRenderer.root; + +console.log(testInstance.findByType(SubComponent).props.foo); // bar +console.log(testInstance.findByProps({className: "sub"}).children); // Sub +``` + +### TestRenderer + +* [`TestRenderer.create()`](#TestRenderer.create) + +### TestRenderer instance + +* [`testRenderer.toJSON()`](#testRenderer.toJSON) +* [`testRenderer.toTree()`](#testRenderer.toTree) +* [`testRenderer.update()`](#testRenderer.update) +* [`testRenderer.unmount()`](#testRenderer.unmount) +* [`testRenderer.getInstance()`](#testRenderer.getInstance) +* [`testRenderer.root`](#testRenderer.root) + +### Test Instance + +* [`testInstance.find()`](#testInstance.find) +* [`testInstance.findByType()`](#testInstance.findByType) +* [`testInstance.findByProps()`](#testInstance.findByProps) +* [`testInstance.findAll()`](#testInstance.findAll) +* [`testInstance.findAllByType()`](#testInstance.findAllByType) +* [`testInstance.findAllByProps()`](#testInstance.findAllByProps) +* [`testInstance.instance`](#testInstance.instance) +* [`testInstance.type`](#testInstance.type) +* [`testInstance.props`](#testInstance.props) +* [`testInstance.parent`](#testInstance.parent) +* [`testInstance.children`](#testInstance.children) + +## Reference + +### `TestRenderer.create()` + +```javascript +TestRenderer.create(element, options); +``` + +Create a Test Renderer instance with a passed element, which has the following methods. +You can pass `createNodeMock` function as the option, which allows for custom mocking behavior. +`createNodeMock` accepts the current element and should return a mock ref object. + +```javascript +const TestRenderer = require('react-test-renderer'); + +class MyComponent extends React.Component { + constructor(props) { + super(props); + this.input = null; + } + componentDidMount() { + this.input.focus(); + } + render() { + return this.input = el} /> + } +} + +let focused = false; +TestRenderer.create( + , + { + createNodeMock: (element) => { + if (element.type === 'input') { + // mock a focus function + return { + focus: () => { + focused = true; + } + }; + } + return null; + } + } +); +console.log(focused); // true +``` + +### `testRenderer.toJSON()` + +```javascript +testRenderer.toJSON() +``` + +Return a JSON object representing the element. + +### `testRenderer.toTree()` + +```javascript +testRenderer.toTree() +``` + +Return a tree object representing the element. + +### `testRenderer.update()` + +```javascript +testRenderer.update(element) +``` + +Update the element with a passed element. + +### `testRenderer.unmount()` + +```javascript +testRenderer.unmount() +``` + +Unmount the element from testRenderer. + +### `testRenderer.getInstance()` + +```javascript +testRenderer.getInstance() +``` + +Return a root container instance. + +### `testRenderer.root` + +```javascript +testRenderer.root +``` + +`root` is a testInstance, which has the following methods. + +### `testInstance.find()` + +```javascript +testInstance.find(predicate) +``` + +Find a testInstance that matches the provided predicate. + +### `testInstance.findByType()` + +```javascript +testInstance.findByType(type) +``` + +Find a testInstance that matches the provided type. + +### `testInstance.findByProps()` + +```javascript +testInstance.findByProps(props) +``` + +Find a testInstance that matches the provided props. + +### `testInstance.findAll()` + +```javascript +testInstance.findAll(predicate) +``` + +Find all testInstances that matches the provided predicate. + +### `testInstance.findAllByType()` + +```javascript +testInstance.findAllByType(type) +``` + +Find all testInstances that matches the provided type. + +### `testInstance.findAllByProps()` + +```javascript +testInstance.findAllByProps(props) +``` + +Find all testInstances that matches the provided props. + +### `testInstance.instance` + +```javascript +testInstance.instance +``` + +`instance` is a public instance of the testInstance. + +### `testInstance.type` + +```javascript +testInstance.type +``` + +`type` is a Component type of the testInstance. + +### `testInstance.props` + +```javascript +testInstance.props +``` + +`props` is a props object of the testInstance. + +### `testInstance.parent` + +```javascript +testInstance.parent +``` + +`parent` is a parent testInstance of the testInstance. + +### `testInstance.children` + +```javascript +testInstance.children +``` + +`children` is a children of the testInstance. From a5ffe0ad8270d09a5117984fb7aa90f933bec74d Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 13:37:35 +0900 Subject: [PATCH 03/14] TestRenderer is not experiment --- docs/docs/reference-test-renderer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 1545337a0e47b..7148c839721ac 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -15,7 +15,7 @@ const TestRenderer = require('react-test-renderer'); // ES5 with npm ## Overview -This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. +This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom. From cd13d94fa7a29c1fde630f4176de90985a35a2f8 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 13:38:23 +0900 Subject: [PATCH 04/14] Add a link for jsdom --- docs/docs/reference-test-renderer.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 7148c839721ac..843eef43ad29b 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -17,7 +17,7 @@ const TestRenderer = require('react-test-renderer'); // ES5 with npm This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. -Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom. +Essentially, this package makes it easy to grab a snapshot of the platform view hierarchy (similar to a DOM tree) rendered by a React DOM or React Native component without using a browser or [jsdom](https://github.com/tmpvar/jsdom). Example: From 2a3f78a8fd72b2a1fa828b1d71153f263b0d1c6a Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 13:38:49 +0900 Subject: [PATCH 05/14] Use ES Modules syntax --- docs/docs/reference-test-renderer.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 843eef43ad29b..5e84a5bc23244 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -22,7 +22,7 @@ Essentially, this package makes it easy to grab a snapshot of the platform view Example: ```javascript -const TestRenderer = require('react-test-renderer'); +import TestRenderer from 'react-test-renderer'; const testRenderer = TestRenderer.create( Facebook @@ -39,7 +39,7 @@ You can use Jest's snapshot testing feature to automatically save a copy of the You can also traverse a ReactElement by methods ReactTestInstance provides. ```javascript -const TestRenderer = require('react-test-renderer'); +import TestRenderer from 'react-test-renderer'; function MyComponent() { return ( @@ -103,7 +103,7 @@ You can pass `createNodeMock` function as the option, which allows for custom mo `createNodeMock` accepts the current element and should return a mock ref object. ```javascript -const TestRenderer = require('react-test-renderer'); +import TestRenderer from 'react-test-renderer'; class MyComponent extends React.Component { constructor(props) { From 7080bf946c6727f6c1ee24de5e104a8fe370ad49 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 13:40:29 +0900 Subject: [PATCH 06/14] Twaek --- docs/docs/reference-test-renderer.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 5e84a5bc23244..a450799ae732a 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -34,9 +34,9 @@ console.log(testRenderer.toJSON()); // children: [ 'Facebook' ] } ``` -You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: http://facebook.github.io/jest/blog/2016/07/27/jest-14.html. +You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: [Learn more about it](http://facebook.github.io/jest/blog/2016/07/27/jest-14.html). -You can also traverse a ReactElement by methods ReactTestInstance provides. +You can also traverse the output to find specific nodes and make assertions about them. ```javascript import TestRenderer from 'react-test-renderer'; From 794e823d03ca606c1d613ce93722612bcb54741d Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 13:46:31 +0900 Subject: [PATCH 07/14] Add a Link component --- docs/docs/reference-test-renderer.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index a450799ae732a..6200c7d8ea2a9 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -24,6 +24,10 @@ Example: ```javascript import TestRenderer from 'react-test-renderer'; +function Link(props) { + return {props.children}; +} + const testRenderer = TestRenderer.create( Facebook ); From 1ad37d16e10a1d93671eec1ccb068431d9bb0f27 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 13:46:55 +0900 Subject: [PATCH 08/14] Use Jest assertions --- docs/docs/reference-test-renderer.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 6200c7d8ea2a9..3cac035e5a01f 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -63,8 +63,8 @@ function SubComponent() { const testRenderer = TestRenderer.create(); const testInstance = testRenderer.root; -console.log(testInstance.findByType(SubComponent).props.foo); // bar -console.log(testInstance.findByProps({className: "sub"}).children); // Sub +expect(testInstance.findByType(SubComponent).props.foo).toBe('bar'); +expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']); ``` ### TestRenderer From a101d63779dc09529392fffa28586372ba3fdf5d Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 13:57:53 +0900 Subject: [PATCH 09/14] Move a documentation for createNodeMock to Idea section --- docs/docs/reference-test-renderer.md | 79 +++++++++++++++------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 3cac035e5a01f..7613d32c953a8 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -103,44 +103,6 @@ TestRenderer.create(element, options); ``` Create a Test Renderer instance with a passed element, which has the following methods. -You can pass `createNodeMock` function as the option, which allows for custom mocking behavior. -`createNodeMock` accepts the current element and should return a mock ref object. - -```javascript -import TestRenderer from 'react-test-renderer'; - -class MyComponent extends React.Component { - constructor(props) { - super(props); - this.input = null; - } - componentDidMount() { - this.input.focus(); - } - render() { - return this.input = el} /> - } -} - -let focused = false; -TestRenderer.create( - , - { - createNodeMock: (element) => { - if (element.type === 'input') { - // mock a focus function - return { - focus: () => { - focused = true; - } - }; - } - return null; - } - } -); -console.log(focused); // true -``` ### `testRenderer.toJSON()` @@ -277,3 +239,44 @@ testInstance.children ``` `children` is a children of the testInstance. + +## Ideas + +You can pass `createNodeMock` function to `TestRenderer.create` as the option, which allows for custom mocking behavior. +`createNodeMock` accepts the current element and should return a mock ref object. + +```javascript +import TestRenderer from 'react-test-renderer'; + +class MyComponent extends React.Component { + constructor(props) { + super(props); + this.input = null; + } + componentDidMount() { + this.input.focus(); + } + render() { + return this.input = el} /> + } +} + +let focused = false; +TestRenderer.create( + , + { + createNodeMock: (element) => { + if (element.type === 'input') { + // mock a focus function + return { + focus: () => { + focused = true; + } + }; + } + return null; + } + } +); +expect(focused).toBe(true); +``` From 929b15b1377e4d3288059036a8d21bb045ee7411 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 15:50:19 +0900 Subject: [PATCH 10/14] Renamed --- docs/_data/nav_docs.yml | 2 ++ docs/docs/reference-test-renderer.md | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/_data/nav_docs.yml b/docs/_data/nav_docs.yml index 0e00373acc378..c0ba59fd24c64 100644 --- a/docs/_data/nav_docs.yml +++ b/docs/_data/nav_docs.yml @@ -75,3 +75,5 @@ title: Test Utilities - id: shallow-renderer title: Shallow Renderer + - id: test-renderer + title: Test Renderer diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 7613d32c953a8..c6c99436ffca2 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -1,7 +1,7 @@ --- -id: reference-test-renderer +id: test-renderer title: Test Renderer -permalink: docs/reference-test-renderer.html +permalink: docs/test-renderer.html layout: docs category: Reference --- From 66a3a278906e72e29541d4029418ca53a6ef7228 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 16:20:39 +0900 Subject: [PATCH 11/14] Tweak --- docs/docs/reference-test-renderer.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index c6c99436ffca2..643b70fbf7f6a 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -80,7 +80,7 @@ expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']); * [`testRenderer.getInstance()`](#testRenderer.getInstance) * [`testRenderer.root`](#testRenderer.root) -### Test Instance +### TestInstance * [`testInstance.find()`](#testInstance.find) * [`testInstance.findByType()`](#testInstance.findByType) @@ -102,7 +102,7 @@ expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']); TestRenderer.create(element, options); ``` -Create a Test Renderer instance with a passed element, which has the following methods. +Create a TestRenderer instance with a passed element, which has the following methods and properties. ### `testRenderer.toJSON()` @@ -150,7 +150,7 @@ Return a root container instance. testRenderer.root ``` -`root` is a testInstance, which has the following methods. +`root` is a testInstance, which has the following methods and properties. ### `testInstance.find()` @@ -206,7 +206,7 @@ Find all testInstances that matches the provided props. testInstance.instance ``` -`instance` is a public instance of the testInstance. +`instance` is a component instance of the testInstance. ### `testInstance.type` @@ -230,7 +230,7 @@ testInstance.props testInstance.parent ``` -`parent` is a parent testInstance of the testInstance. +`parent` is a parent testInstance. ### `testInstance.children` @@ -238,7 +238,7 @@ testInstance.parent testInstance.children ``` -`children` is a children of the testInstance. +`children` is children of the testInstance. ## Ideas From 8c45c77c613b3f8468c67d2c3b814171555f1101 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 14 Sep 2017 16:26:53 +0900 Subject: [PATCH 12/14] Rename --- docs/docs/react-test-renderer.md | 213 ------------------------------- 1 file changed, 213 deletions(-) delete mode 100644 docs/docs/react-test-renderer.md diff --git a/docs/docs/react-test-renderer.md b/docs/docs/react-test-renderer.md deleted file mode 100644 index d092f43bda6a1..0000000000000 --- a/docs/docs/react-test-renderer.md +++ /dev/null @@ -1,213 +0,0 @@ ---- -id: react-test-renderer -title: React Test Renderer -permalink: docs/react-test-renderer.html -layout: docs -category: Reference ---- - -**Importing** - -```javascript -import ReactTestRenderer from 'react-test-renderer'; // ES6 -const ReactTestRenderer = require('react-test-renderer'); // ES5 with npm -``` - -## Overview - -This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. - -Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom. - -Example: - -```javascript -const ReactTestRenderer = require('react-test-renderer'); - -const testRenderer = ReactTestRenderer.create( - Facebook -); - -console.log(testRenderer.toJSON()); -// { type: 'a', -// props: { href: 'https://www.facebook.com/' }, -// children: [ 'Facebook' ] } -``` - -You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: http://facebook.github.io/jest/blog/2016/07/27/jest-14.html. - -You can traverse a ReactElement by methods ReactTestInstance provides. - -```javascript -const ReactTestRenderer = require('react-test-renderer'); - -function MyComponent() { - return ( -
- -

Hello

-
- ) -} - -function SubComponent() { - return ( -

Sub

- ); -} - -const testRenderer = ReactTestRenderer.create(); -const testInstance = testRenderer.root; - -console.log(testInstance.findByType(SubComponent).props.foo); // bar -console.log(testInstance.findByProps({className: "sub"}).children); // Sub -``` - - -## Reference - -### `ReactTestRenderer.create()` - -```javascript -ReactTestRenderer.create(element, options); -``` - -Create a React Test Render instance, which has the following methods. -You can pass `createNodeMock` function as the option, which allows for custom mocking behavior. -`createNodeMock` accepts the current element and should return a mock ref object. - -### `testRenderer.toJSON()` - -```javascript -testRenderer.toJSON() -``` - -Return a JSON object representing the element. - -### `testRenderer.toTree()` - -```javascript -testRenderer.toTree() -``` - -Return a tree object representing the element. - -### `testRenderer.update()` - -```javascript -testRenderer.update(element) -``` - -Rerender testRenderer with a passed element. - -### `testRenderer.unmount()` - -```javascript -testRenderer.unmount() -``` - -Unmount the element from testRenderer. - -### `testRenderer.getInstance()` - -```javascript -testRenderer.getInstance() -``` - -Return a root container instance. - -### `testRenderer.root` - -```javascript -testRenderer.root -``` - -`root` is a testInstance, which has the following methods. - -### `testInstance.find()` - -```javascript -testInstance.find(predicate) -``` - -Find a testInstance that matches the provided predicate. - -### `testInstance.findByType()` - -```javascript -testInstance.findByType(type) -``` - -Find a testInstance that matches the provided type. - -### `testInstance.findByProps()` - -```javascript -testInstance.findByProps(props) -``` - -Find a testInstance that matches the provided props. - -### `testInstance.findAll()` - -```javascript -testInstance.findAll(predicate) -``` - -Find all testInstances that matches the provided predicate. - -### `testInstance.findAllByType()` - -```javascript -testInstance.findAllByType(type) -``` - -Find all testInstances that matches the provided type. - -### `testInstance.findAllByProps()` - -```javascript -testInstance.findAllByProps(props) -``` - -Find all testInstances that matches the provided props. - -### `testInstance.instance` - -```javascript -testInstance.instance -``` - -`instance` is a public instance of the testInstance. - -### `testInstance.type` - -```javascript -testInstance.type -``` - -`type` is a Component type of the testInstance. - -### `testInstance.props` - -```javascript -testInstance.props -``` - -`props` is a props object of the testInstance. - -### `testInstance.parent` - -```javascript -testInstance.parent -``` - -`parent` is a parent testInstance of the testInstance. - -### `testInstance.children` - -```javascript -testInstance.children -``` - -`children` is a children of the testInstance. From 40444c4281615bd57b56d0fce3b0109248bb75ce Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Fri, 15 Sep 2017 15:27:35 +0900 Subject: [PATCH 13/14] More explicit --- docs/docs/reference-test-renderer.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 643b70fbf7f6a..496879975ecae 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -155,10 +155,10 @@ testRenderer.root ### `testInstance.find()` ```javascript -testInstance.find(predicate) +testInstance.find(test) ``` -Find a testInstance that matches the provided predicate. +Find a descendant testInstance that `test(testInstance)` is `true`. ### `testInstance.findByType()` @@ -166,7 +166,7 @@ Find a testInstance that matches the provided predicate. testInstance.findByType(type) ``` -Find a testInstance that matches the provided type. +Find a descendant testInstance that matches the provided type. ### `testInstance.findByProps()` @@ -174,15 +174,15 @@ Find a testInstance that matches the provided type. testInstance.findByProps(props) ``` -Find a testInstance that matches the provided props. +Find a descendant testInstance that matches the provided props. ### `testInstance.findAll()` ```javascript -testInstance.findAll(predicate) +testInstance.findAll(test) ``` -Find all testInstances that matches the provided predicate. +Find all descendant testInstances that `test(testInstance)` is `true`. ### `testInstance.findAllByType()` @@ -190,7 +190,7 @@ Find all testInstances that matches the provided predicate. testInstance.findAllByType(type) ``` -Find all testInstances that matches the provided type. +Find all descendant testInstances that matches the provided type. ### `testInstance.findAllByProps()` @@ -198,7 +198,7 @@ Find all testInstances that matches the provided type. testInstance.findAllByProps(props) ``` -Find all testInstances that matches the provided props. +Find all descendant testInstances that matches the provided props. ### `testInstance.instance` From 11012b394c7c45657bc26f6739a0e18760cf447a Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Fri, 15 Sep 2017 15:55:01 +0900 Subject: [PATCH 14/14] Add a usecase for createNodeMock --- docs/docs/reference-test-renderer.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/docs/reference-test-renderer.md b/docs/docs/reference-test-renderer.md index 496879975ecae..528c8c0234be2 100644 --- a/docs/docs/reference-test-renderer.md +++ b/docs/docs/reference-test-renderer.md @@ -242,8 +242,9 @@ testInstance.children ## Ideas -You can pass `createNodeMock` function to `TestRenderer.create` as the option, which allows for custom mocking behavior. +You can pass `createNodeMock` function to `TestRenderer.create` as the option, which allows for custom mock refs. `createNodeMock` accepts the current element and should return a mock ref object. +This is useful when you test a component rely on refs. ```javascript import TestRenderer from 'react-test-renderer';