Skip to content

Commit

Permalink
Merge pull request #896 from jwalton/master
Browse files Browse the repository at this point in the history
Make enzyme work for react ^15.4.x.
  • Loading branch information
ljharb authored Apr 14, 2017
2 parents 419478d + fcd245a commit 817a76d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
41 changes: 16 additions & 25 deletions src/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
'[email protected]+ is an implicit dependency when using [email protected]+ with enzyme. ' +
'Please add the appropriate version to your devDependencies. ' +
'See https://github.com/airbnb/enzyme#installation',
'[email protected]+ and react-test-renderer are implicit dependencies when using' +
'[email protected]+ 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
Expand All @@ -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 [email protected]+. ' +
'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 Down

0 comments on commit 817a76d

Please sign in to comment.