Last active
January 3, 2018 05:59
Revisions
-
jondlm renamed this gist
Jul 8, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jondlm renamed this gist
Jul 8, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jondlm revised this gist
Jul 8, 2015 . 2 changed files with 11 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,4 +4,7 @@ TLDR; shallow rendering only invokes the following lifecycle hooks (in order): 1. `getDefaultProps` 2. `getInitialState` 3. `componentWillMount` _stops here until re-render_ 4. `componentWillReceiveProps` 5. `shouldComponentUpdate` 6. `componentWillUpdate` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,13 @@ var shallowRenderer = TestUtils.createRenderer(); var props = { something: true }; // Trigger first render shallowRenderer.render(React.createElement(Component, props)); shallowRenderer.getRenderOutput(); // Update props props.something = false; // Trigger a re-render shallowRenderer.render(React.createElement(Component, props)); shallowRenderer.getRenderOutput(); -
jondlm created this gist
Jul 8, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ React introduced [shallow rendering](http://facebook.github.io/react/docs/test-utils.html#shallow-rendering) in 0.13. This is an excellent feature that I wish was included earlier in React. It aims to solve the problem of unit testing components without going through a real, or jsdom mocked, DOM. I couldn't find any info online about what lifecycle events it actually fires. So I did some testing of my own. To reproduce, put `component.js` and `test.js` into a folder and run `node test.js`. TLDR; shallow rendering only invokes the following lifecycle hooks (in order): 1. `getDefaultProps` 2. `getInitialState` 3. `componentWillMount` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ /*eslint node:1 */ 'use strict'; var React = require('react'); var Component = React.createClass({ getDefaultProps: function () { console.log('getDefaultProps fired'); return { another: true }; }, getInitialState: function () { console.log('getInitialState fired'); return {}; }, // Life cycle methods componentWillMount: function () { console.log('componentWillMount fired'); }, componentDidMount: function () { console.log('componentDidMount fired'); }, componentWillReceiveProps: function () { console.log('componentWillReceiveProps fired'); }, shouldComponentUpdate: function () { console.log('shouldComponentUpdate fired'); return true; }, componentWillUpdate: function () { console.log('componentWillUpdate fired'); }, componentDidUpdate: function () { console.log('componentDidUpdate fired'); }, componentWillUnmount: function () { console.log('componentWillUnmount fired'); }, render: function() { return React.createElement('div'); } }); module.exports = Component; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ /*eslint node:1 */ 'use strict'; var React = require('react/addons'); var TestUtils = React.addons.TestUtils; var Component = require('./component'); var shallowRenderer = TestUtils.createRenderer(); var props = { something: true }; shallowRenderer.render(React.createElement(Component, props)); shallowRenderer.getRenderOutput();