Skip to content

Commit

Permalink
Pull shallow renderer from react-test-renderer/shallow for React 15.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Apr 8, 2017
1 parent 7c030cb commit dbb029f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"react:clean": "rimraf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils",
"react:13": "rimraf node_modules/.bin/npm && npm run react:clean && npm i react@0.13 && npm install",
"react:14": "rimraf node_modules/.bin/npm && npm run react:clean && npm i react@0.14 react-dom@0.14 react-addons-test-utils@0.14 && npm install",
"react:15": "rimraf node_modules/.bin/npm && npm run react:clean && npm i react@15 react-dom@15 react-addons-test-utils@15 prop-types@15 create-react-class@15 && npm install",
"react:15": "rimraf node_modules/.bin/npm && npm run react:clean && npm i react@15 react-dom@15 react-addons-test-utils@15 prop-types@15 create-react-class@15 react-test-renderer@15 && npm install",
"docs:clean": "rimraf _book",
"docs:prepare": "gitbook install",
"docs:build": "npm run docs:prepare && gitbook build",
Expand Down
23 changes: 22 additions & 1 deletion src/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let unmountComponentAtNode;
let batchedUpdates;
let PropTypes;
let createClass;
let shallowRendererFactory;

const React = require('react');

Expand Down Expand Up @@ -114,6 +115,26 @@ if (REACT013) {
throw e;
}

// Shallow renderer is accessible via the react-test-renderer package for React 15.5+.
// This is a separate package though and may not be installed.
try {
if (REACT155) {
// eslint-disable-next-line import/no-extraneous-dependencies
shallowRendererFactory = require('react-test-renderer/shallow').createRenderer;
} else {
// eslint-disable-next-line import/no-extraneous-dependencies
shallowRendererFactory = TestUtils.createRenderer;
}
} catch (e) {
// eslint-disable-next-line no-console
console.error(
'react-test-renderer is an implicit dependency in order to support react@15.5+. ' +
'Please add the appropriate version to your devDependencies. ' +
'See https://github.com/airbnb/enzyme#installation',
);
throw e;
}

// Shallow rendering changed from 0.13 => 0.14 in such a way that
// 0.14 now does not allow shallow rendering of native DOM elements.
// This is mainly because the result of such a call should not realistically
Expand All @@ -122,7 +143,7 @@ if (REACT013) {
// is essentially a replacement for `TestUtils.createRenderer` that doesn't use
// shallow rendering when it's just a DOM element.
createShallowRenderer = function createRendererCompatible() {
const renderer = TestUtils.createRenderer();
const renderer = shallowRendererFactory();
const originalRender = renderer.render;
const originalRenderOutput = renderer.getRenderOutput;
let isDOM = false;
Expand Down

0 comments on commit dbb029f

Please sign in to comment.