From dbb029f427713dab4ba661638bad60674b880136 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Sat, 8 Apr 2017 09:37:17 -0700 Subject: [PATCH] Pull shallow renderer from react-test-renderer/shallow for React 15.5 --- package.json | 2 +- src/react-compat.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9d5106e31..acb3bb740 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/react-compat.js b/src/react-compat.js index 85a562152..afde69bff 100644 --- a/src/react-compat.js +++ b/src/react-compat.js @@ -20,6 +20,7 @@ let unmountComponentAtNode; let batchedUpdates; let PropTypes; let createClass; +let shallowRendererFactory; const React = require('react'); @@ -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 @@ -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;