From fcd245a2be13caf369b943119296bf297d2fe464 Mon Sep 17 00:00:00 2001 From: Jason Walton Date: Thu, 13 Apr 2017 10:09:11 -0400 Subject: [PATCH] Make enzyme work for react ^15.4.x. --- src/ShallowWrapper.js | 2 +- src/react-compat.js | 41 ++++++++++++++++------------------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index 9d7b74ba9..bd231068a 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -102,7 +102,7 @@ function validateOptions(options) { function performBatchedUpdates(wrapper, fn) { const renderer = wrapper.root.renderer; - if (REACT155) { + if (REACT155 && renderer.unstable_batchedUpdates) { // React 15.5+ exposes batching on shallow renderer itself return renderer.unstable_batchedUpdates(fn); } diff --git a/src/react-compat.js b/src/react-compat.js index 204deab53..01222efec 100644 --- a/src/react-compat.js +++ b/src/react-compat.js @@ -96,19 +96,30 @@ if (REACT013) { // to list this as a dependency in package.json and have 0.13 work properly. // As a result, right now this is basically an implicit dependency. try { - if (REACT155) { + try { + // This is for react v15.5 and up... + // eslint-disable-next-line import/no-extraneous-dependencies TestUtils = require('react-dom/test-utils'); - } else { + // eslint-disable-next-line import/no-extraneous-dependencies + shallowRendererFactory = require('react-test-renderer/shallow').createRenderer; + } catch (e) { + // This is for react < v15.5. Note that users who have `react^15.4.x` in their package.json + // will arrive here, too. They need to upgrade. React will print a nice warning letting + // them know they need to upgrade, though, so we're good. Also note we explicitly do not + // use TestUtils from react-dom/test-utils here, mainly so the user still gets a warning for + // requiring 'react-addons-test-utils', which lets them know there's action required. + // eslint-disable-next-line import/no-extraneous-dependencies TestUtils = require('react-addons-test-utils'); + shallowRendererFactory = TestUtils.createRenderer; } } catch (e) { if (REACT155) { console.error( // eslint-disable-line no-console - 'react-dom@15.5+ is an implicit dependency when using react@15.5+ with enzyme. ' + - 'Please add the appropriate version to your devDependencies. ' + - 'See https://github.com/airbnb/enzyme#installation', + 'react-dom@15.5+ and react-test-renderer are implicit dependencies when using' + + 'react@15.5+ with enzyme. Please add the appropriate version to your' + + 'devDependencies. See https://github.com/airbnb/enzyme#installation', ); } else { console.error( // eslint-disable-line no-console @@ -120,26 +131,6 @@ 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