Skip to content

Commit 75d1390

Browse files
authored
Merge pull request #876 from kentcdodds/pr/support-15.5
[Compat] support [email protected]
2 parents a8cf810 + 21f6e7a commit 75d1390

14 files changed

+197
-117
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ matrix:
3535
env: KARMA=true REACT=0.13
3636
- node_js: "6"
3737
env: KARMA=true REACT=0.14
38+
- node_js: "6"
39+
env: KARMA=true REACT=15.4
3840
- node_js: "6"
3941
env: KARMA=true REACT=15
4042
allow_failures:
@@ -43,4 +45,5 @@ matrix:
4345
env:
4446
- REACT=0.13
4547
- REACT=0.14
48+
- REACT=15.4
4649
- REACT=15

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,18 @@ npm i --save-dev enzyme
6060
Enzyme is currently compatible with `React 15.x`, `React 0.14.x` and `React 0.13.x`. In order to
6161
achieve this compatibility, some dependencies cannot be explicitly listed in our `package.json`.
6262

63-
If you are using `React 0.14` or `React 15.x`, in addition to `enzyme`, you will have to ensure that
63+
If you are using `React 0.14` or `React <15.5`, in addition to `enzyme`, you will have to ensure that
6464
you also have the following npm modules installed if they were not already:
6565

6666
```bash
67-
npm i --save-dev react-addons-test-utils
68-
npm i --save-dev react-dom
67+
npm i --save-dev react-addons-test-utils react-dom
68+
```
69+
70+
If you are using `React >=15.5`, in addition to `enzyme`, you will have to ensure that you also have
71+
the following npm modules installed if they were not already:
72+
73+
```bash
74+
npm i --save-dev react-test-renderer react-dom
6975
```
7076

7177

install-relevant-react.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ if [ "$REACT" = "0.14" ]; then
1212
npm run react:14
1313
fi
1414

15+
if [ "$REACT" = "15.4" ]; then
16+
npm run react:15.4
17+
fi
18+
1519
if [ "$REACT" = "15" ]; then
1620
npm run react:15
1721
fi

karma.conf.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,36 @@ require('babel-register');
44

55
var IgnorePlugin = require('webpack').IgnorePlugin;
66
var REACT013 = require('./src/version').REACT013;
7+
var REACT155 = require('./src/version').REACT155;
8+
9+
function getPlugins() {
10+
var plugins = [];
11+
12+
/*
13+
this list of conditional IgnorePlugins mirrors the conditional
14+
requires in src/react-compat.js and exists to avoid error
15+
output from the webpack compilation
16+
*/
17+
18+
if (!REACT013) {
19+
plugins.push(new IgnorePlugin(/react\/lib\/ExecutionEnvironment/));
20+
plugins.push(new IgnorePlugin(/react\/lib\/ReactContext/));
21+
plugins.push(new IgnorePlugin(/react\/addons/));
22+
}
23+
if (REACT013) {
24+
plugins.push(new IgnorePlugin(/react-dom/));
25+
}
26+
if (REACT013 || REACT155) {
27+
plugins.push(new IgnorePlugin(/react-addons-test-utils/));
28+
}
29+
if (!REACT155) {
30+
plugins.push(new IgnorePlugin(/react-test-renderer/));
31+
plugins.push(new IgnorePlugin(/react-dom\/test-utils/));
32+
plugins.push(new IgnorePlugin(/create-react-class/));
33+
}
34+
35+
return plugins;
36+
}
737

838
module.exports = function karma(config) {
939
config.set({
@@ -33,7 +63,7 @@ module.exports = function karma(config) {
3363
],
3464

3565
exclude: [
36-
'test/_*.{jsx,js}',
66+
'test/_helpers/index.jsx',
3767
],
3868

3969
browsers: [
@@ -71,18 +101,7 @@ module.exports = function karma(config) {
71101
},
72102
],
73103
},
74-
plugins: [
75-
/*
76-
this list of conditional IgnorePlugins mirrors the conditional
77-
requires in src/react-compat.js and exists to avoid error
78-
output from the webpack compilation
79-
*/
80-
!REACT013 && new IgnorePlugin(/react\/lib\/ExecutionEnvironment/),
81-
!REACT013 && new IgnorePlugin(/react\/lib\/ReactContext/),
82-
!REACT013 && new IgnorePlugin(/react\/addons/),
83-
REACT013 && new IgnorePlugin(/react-dom/),
84-
REACT013 && new IgnorePlugin(/react-addons-test-utils/),
85-
].filter(function filterPlugins(plugin) { return plugin !== false; }),
104+
plugins: getPlugins(),
86105
},
87106

88107
webpackServer: {

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
"test:watch": "mocha --recursive --watch test",
2020
"test:karma": "karma start",
2121
"test:env": "sh ./example-test.sh",
22-
"test:all": "npm run react:13 && npm run test:only && npm run react:14 && npm run test:only && npm run react:15 && npm run test:only",
23-
"react:clean": "rimraf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils",
22+
"test:all": "npm run react:13 && npm run test:only && npm run react:14 && npm run test:only && npm run react:15.4 && npm run test:only && npm run react:15 && npm run test:only",
23+
"react:clean": "rimraf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils node_modules/react-test-renderer",
2424
"react:13": "rimraf node_modules/.bin/npm && npm run react:clean && npm i [email protected] && npm install",
2525
"react:14": "rimraf node_modules/.bin/npm && npm run react:clean && npm i [email protected] [email protected] [email protected] && npm install",
26-
"react:15": "rimraf node_modules/.bin/npm && npm run react:clean && npm i react@15 react-dom@15 react-addons-test-utils@15 && npm install",
26+
"react:15.4": "rimraf node_modules/.bin/npm && npm run react:clean && npm i [email protected] [email protected] [email protected] && npm install",
27+
"react:15": "rimraf node_modules/.bin/npm && npm run react:clean && npm i react@15 react-dom@15 create-react-class@15 react-test-renderer@^15.5.4 && npm install",
2728
"docs:clean": "rimraf _book",
2829
"docs:prepare": "gitbook install",
2930
"docs:build": "npm run docs:prepare && gitbook build",
3031
"docs:watch": "npm run docs:prepare && gitbook serve",
3132
"docs:publish": "npm run docs:clean && npm run docs:build && cd _book && git init && git commit --allow-empty -m 'update book' && git fetch [email protected]:airbnb/enzyme.git gh-pages && git checkout -b gh-pages && git add . && git commit -am 'update book' && git push [email protected]:airbnb/enzyme.git gh-pages --force",
32-
"travis": "babel-node ./node_modules/.bin/istanbul cover --report html _mocha -- test --recursive"
33+
"travis": "babel-node \"$(which istanbul)\" cover --report html _mocha -- test --recursive"
3334
},
3435
"repository": {
3536
"type": "git",
@@ -60,6 +61,7 @@
6061
"object.assign": "^4.0.4",
6162
"object.entries": "^1.0.3",
6263
"object.values": "^1.0.3",
64+
"prop-types": "^15.5.4",
6365
"uuid": "^2.0.3"
6466
},
6567
"devDependencies": {
@@ -71,6 +73,7 @@
7173
"babel-register": "^6.24.1",
7274
"chai": "^3.5.0",
7375
"coveralls": "^2.13.0",
76+
"create-react-class": "^15.5.2",
7477
"enzyme-example-jest": "^0.1.0",
7578
"enzyme-example-karma": "^0.1.1",
7679
"enzyme-example-karma-webpack": "^0.1.4",

src/ReactWrapperComponent.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { PropTypes } from 'react';
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
23
import objectAssign from 'object.assign';
34

45
/* eslint react/forbid-prop-types: 0 */

src/ShallowWrapper.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
batchedUpdates,
3838
isDOMComponentElement,
3939
} from './react-compat';
40+
import { REACT155 } from './version';
4041

4142
/**
4243
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
@@ -98,6 +99,17 @@ function validateOptions(options) {
9899
}
99100
}
100101

102+
103+
function performBatchedUpdates(wrapper, fn) {
104+
const renderer = wrapper.root.renderer;
105+
if (REACT155) {
106+
// React 15.5+ exposes batching on shallow renderer itself
107+
return renderer.unstable_batchedUpdates(fn);
108+
}
109+
// React <15.5: Fallback to ReactDOM
110+
return batchedUpdates(fn);
111+
}
112+
101113
/**
102114
* @class ShallowWrapper
103115
*/
@@ -110,7 +122,7 @@ class ShallowWrapper {
110122
this.unrendered = nodes;
111123
this.renderer = createShallowRenderer();
112124
withSetStateAllowed(() => {
113-
batchedUpdates(() => {
125+
performBatchedUpdates(this, () => {
114126
this.renderer.render(nodes, options.context);
115127
const instance = this.instance();
116128
if (
@@ -223,7 +235,7 @@ class ShallowWrapper {
223235
const prevContext = instance.context;
224236
const nextProps = props || prevProps;
225237
const nextContext = context || prevContext;
226-
batchedUpdates(() => {
238+
performBatchedUpdates(this, () => {
227239
let shouldRender = true;
228240
// dirty hack:
229241
// make sure that componentWillReceiveProps is called before shouldComponentUpdate
@@ -611,7 +623,7 @@ class ShallowWrapper {
611623
withSetStateAllowed(() => {
612624
// TODO(lmr): create/use synthetic events
613625
// TODO(lmr): emulate React's event propagation
614-
batchedUpdates(() => {
626+
performBatchedUpdates(this, () => {
615627
handler(...args);
616628
});
617629
this.root.update();

src/react-compat.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import objectAssign from 'object.assign';
10-
import { REACT013 } from './version';
10+
import { REACT013, REACT155 } from './version';
1111

1212
let TestUtils;
1313
let createShallowRenderer;
@@ -18,6 +18,7 @@ let childrenToArray;
1818
let renderWithOptions;
1919
let unmountComponentAtNode;
2020
let batchedUpdates;
21+
let shallowRendererFactory;
2122

2223
const React = require('react');
2324

@@ -95,12 +96,44 @@ if (REACT013) {
9596
// to list this as a dependency in package.json and have 0.13 work properly.
9697
// As a result, right now this is basically an implicit dependency.
9798
try {
98-
// eslint-disable-next-line import/no-extraneous-dependencies
99-
TestUtils = require('react-addons-test-utils');
99+
if (REACT155) {
100+
// eslint-disable-next-line import/no-extraneous-dependencies
101+
TestUtils = require('react-dom/test-utils');
102+
} else {
103+
// eslint-disable-next-line import/no-extraneous-dependencies
104+
TestUtils = require('react-addons-test-utils');
105+
}
106+
} catch (e) {
107+
if (REACT155) {
108+
console.error( // eslint-disable-line no-console
109+
'[email protected]+ is an implicit dependency when using [email protected]+ with enzyme. ' +
110+
'Please add the appropriate version to your devDependencies. ' +
111+
'See https://github.com/airbnb/enzyme#installation',
112+
);
113+
} else {
114+
console.error( // eslint-disable-line no-console
115+
'react-addons-test-utils is an implicit dependency in order to support [email protected]. ' +
116+
'Please add the appropriate version to your devDependencies. ' +
117+
'See https://github.com/airbnb/enzyme#installation',
118+
);
119+
}
120+
throw e;
121+
}
122+
123+
// Shallow renderer is accessible via the react-test-renderer package for React 15.5+.
124+
// This is a separate package though and may not be installed.
125+
try {
126+
if (REACT155) {
127+
// eslint-disable-next-line import/no-extraneous-dependencies
128+
shallowRendererFactory = require('react-test-renderer/shallow').createRenderer;
129+
} else {
130+
// eslint-disable-next-line import/no-extraneous-dependencies
131+
shallowRendererFactory = TestUtils.createRenderer;
132+
}
100133
} catch (e) {
101134
// eslint-disable-next-line no-console
102135
console.error(
103-
'react-addons-test-utils is an implicit dependency in order to support react@0.13-14. ' +
136+
'react-test-renderer is an implicit dependency in order to support react@15.5+. ' +
104137
'Please add the appropriate version to your devDependencies. ' +
105138
'See https://github.com/airbnb/enzyme#installation',
106139
);
@@ -115,7 +148,7 @@ if (REACT013) {
115148
// is essentially a replacement for `TestUtils.createRenderer` that doesn't use
116149
// shallow rendering when it's just a DOM element.
117150
createShallowRenderer = function createRendererCompatible() {
118-
const renderer = TestUtils.createRenderer();
151+
const renderer = shallowRendererFactory();
119152
const originalRender = renderer.render;
120153
const originalRenderOutput = renderer.getRenderOutput;
121154
let isDOM = false;

src/version.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from 'react';
22

33
export const VERSION = React.version;
4+
5+
const [major, minor] = VERSION.split('.');
6+
47
export const REACT013 = VERSION.slice(0, 4) === '0.13';
58
export const REACT014 = VERSION.slice(0, 4) === '0.14';
6-
export const REACT15 = VERSION.slice(0, 3) === '15.';
9+
export const REACT15 = major === '15';
10+
export const REACT155 = REACT15 && minor >= 5;

0 commit comments

Comments
 (0)