Skip to content

Commit 8068605

Browse files
authored
Merge branch 'master' into docs
2 parents 781e460 + 412e307 commit 8068605

File tree

13 files changed

+165
-14
lines changed

13 files changed

+165
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ node_modules
3333
_book
3434

3535
# Only apps should have lockfiles
36+
npm-shrinkwrap.json
3637
package-lock.json
38+
yarn.lock
3739

3840
packages/*/build/

docs/guides/migration-from-2-to-3.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ The change from enzyme v2.x to v3.x is a more significant change than in previou
44
due to the fact that the internal implementation of enzyme has been almost completely rewritten.
55

66
The goal of this rewrite was to address a lot of the major issues that have plagued enzyme since
7-
its initial release. It was also to simultaneously remove a lot of the dependence that enzyme has
8-
on react internals, and to make enzyme more "pluggable", paving the way for enzyme to be used
7+
its initial release. It was also to simultaneously remove a lot of the dependencies that enzyme has
8+
on React internals, and to make enzyme more "pluggable", paving the way for enzyme to be used
99
with "React-like" libraries such as Preact and Inferno.
1010

1111
We have done our best to make enzyme v3 as API compatible with v2.x as possible, however there are
12-
a hand full of breaking changes that we decided we needed to make, intentionally, in order to
12+
a handful of breaking changes that we decided we needed to make, intentionally, in order to
1313
support this new architecture and also improve the usability of the library long-term.
1414

1515
Airbnb has one of the largest enzyme test suites, coming in at around 30,000 enzyme unit tests.
1616
After upgrading enzyme to v3.x in Airbnb's code base, 99.6% of these tests succeeded with no
1717
modifications at all. Most of the tests that broke we found to be easy to fix, and some we found to
18-
actually be depending on what could arguably be considered a bug in v2.x, and the breakage was
18+
actually depend on what could arguably be considered a bug in v2.x, and the breakage was
1919
actually desired.
2020

2121
In this guide, we will go over a couple of the most common breakages that we ran into, and how to
@@ -27,13 +27,13 @@ find a breakage that doesn't seem to make sense to you, feel free to file an iss
2727

2828
enzyme now has an "Adapter" system. This means that you now need to install enzyme along with
2929
another module that provides the Adapter that tells enzyme how to work with your version of React
30-
(or whatever other react-like library you are using).
30+
(or whatever other React-like library you are using).
3131

3232
At the time of writing this, enzyme publishes "officially supported" adapters for React 0.13.x,
3333
0.14.x, 15.x, and 16.x. These adapters are npm packages of the form `enzyme-adapter-react-{{version}}`.
3434

3535
You will want to configure enzyme with the adapter you'd like to use before using enzyme in your
36-
tests. The way to do this is whith `enzyme.configure(...)`. For example, if your project depends
36+
tests. The way to do this is with `enzyme.configure(...)`. For example, if your project depends
3737
on React 16, you would want to configure enzyme this way:
3838

3939
```js
@@ -83,7 +83,7 @@ import { shallow } from 'enzyme';
8383
import StatusLabel from './path/to/StatusLabel';
8484
import Icon from './path/to/Icon';
8585

86-
const wrapper = shallow(<StatusIcon id="success" label="Success" />);
86+
const wrapper = shallow(<StatusLabel id="success" label="Success" />);
8787

8888
const iconCount = wrapper.find(Icon).length;
8989
```
@@ -386,7 +386,7 @@ account for this.
386386
## Private properties and methods have been removed
387387

388388
There are several properties that are on an enzyme "wrapper" that were considered to be private and
389-
were undocumented as a result. Despite being undocumented, people may haev been relying on them. In
389+
were undocumented as a result. Despite being undocumented, people may have been relying on them. In
390390
an effort to make making changes less likely to be accidentally breaking in the future, we have
391391
decided to make these properties properly "private". The following properties will no longer be
392392
accessible on enzyme `shallow` or `mount` instances:

docs/installation/react-16.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ ES5:
4141
<!-- eslint no-var: 0 -->
4242
```js
4343
// setup file
44-
var Enzyme = require('enzyme');
44+
var enzyme = require('enzyme');
4545
var Adapter = require('enzyme-adapter-react-16');
4646

47-
configure({ adapter: new Adapter() });
47+
enzyme.configure({ adapter: new Adapter() });
4848
```
4949

5050
<!-- eslint no-var: 0 -->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md

packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
createMountWrapper,
2020
propsWithKeysAndRef,
2121
} from 'enzyme-adapter-utils';
22+
import findCurrentFiberUsingSlowPath from './findCurrentFiberUsingSlowPath';
2223

2324
const HostRoot = 3;
2425
const ClassComponent = 2;
@@ -63,15 +64,15 @@ function toTree(vnode) {
6364
// TODO(lmr): I'm not really sure I understand whether or not this is what
6465
// i should be doing, or if this is a hack for something i'm doing wrong
6566
// somewhere else. Should talk to sebastian about this perhaps
66-
const node = vnode.alternate !== null ? vnode.alternate : vnode;
67+
const node = findCurrentFiberUsingSlowPath(vnode);
6768
switch (node.tag) {
6869
case HostRoot: // 3
6970
return toTree(node.child);
7071
case ClassComponent:
7172
return {
7273
nodeType: 'class',
7374
type: node.type,
74-
props: { ...vnode.memoizedProps },
75+
props: { ...node.memoizedProps },
7576
key: node.key,
7677
ref: node.ref,
7778
instance: node.stateNode,
@@ -83,7 +84,7 @@ function toTree(vnode) {
8384
return {
8485
nodeType: 'function',
8586
type: node.type,
86-
props: { ...vnode.memoizedProps },
87+
props: { ...node.memoizedProps },
8788
key: node.key,
8889
ref: node.ref,
8990
instance: null,
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Extracted from https://github.com/facebook/react/blob/7bdf93b17a35a5d8fcf0ceae0bf48ed5e6b16688/src/renderers/shared/fiber/ReactFiberTreeReflection.js#L104-L228
2+
function findCurrentFiberUsingSlowPath(fiber) {
3+
const alternate = fiber.alternate;
4+
if (!alternate) {
5+
return fiber;
6+
}
7+
// If we have two possible branches, we'll walk backwards up to the root
8+
// to see what path the root points to. On the way we may hit one of the
9+
// special cases and we'll deal with them.
10+
let a = fiber;
11+
let b = alternate;
12+
while (true) { // eslint-disable-line
13+
const parentA = a.return;
14+
const parentB = parentA ? parentA.alternate : null;
15+
if (!parentA || !parentB) {
16+
// We're at the root.
17+
break;
18+
}
19+
20+
// If both copies of the parent fiber point to the same child, we can
21+
// assume that the child is current. This happens when we bailout on low
22+
// priority: the bailed out fiber's child reuses the current child.
23+
if (parentA.child === parentB.child) {
24+
let child = parentA.child;
25+
while (child) {
26+
if (child === a) {
27+
// We've determined that A is the current branch.
28+
return fiber;
29+
}
30+
if (child === b) {
31+
// We've determined that B is the current branch.
32+
return alternate;
33+
}
34+
child = child.sibling;
35+
}
36+
// We should never have an alternate for any mounting node. So the only
37+
// way this could possibly happen is if this was unmounted, if at all.
38+
throw new Error('Unable to find node on an unmounted component.');
39+
}
40+
41+
if (a.return !== b.return) {
42+
// The return pointer of A and the return pointer of B point to different
43+
// fibers. We assume that return pointers never criss-cross, so A must
44+
// belong to the child set of A.return, and B must belong to the child
45+
// set of B.return.
46+
a = parentA;
47+
b = parentB;
48+
} else {
49+
// The return pointers point to the same fiber. We'll have to use the
50+
// default, slow path: scan the child sets of each parent alternate to see
51+
// which child belongs to which set.
52+
//
53+
// Search parent A's child set
54+
let didFindChild = false;
55+
let child = parentA.child;
56+
while (child) {
57+
if (child === a) {
58+
didFindChild = true;
59+
a = parentA;
60+
b = parentB;
61+
break;
62+
}
63+
if (child === b) {
64+
didFindChild = true;
65+
b = parentA;
66+
a = parentB;
67+
break;
68+
}
69+
child = child.sibling;
70+
}
71+
if (!didFindChild) {
72+
// Search parent B's child set
73+
child = parentB.child;
74+
while (child) {
75+
if (child === a) {
76+
didFindChild = true;
77+
a = parentB;
78+
b = parentA;
79+
break;
80+
}
81+
if (child === b) {
82+
didFindChild = true;
83+
b = parentB;
84+
a = parentA;
85+
break;
86+
}
87+
child = child.sibling;
88+
}
89+
if (!didFindChild) {
90+
throw new Error('Child was not found in either parent set. This indicates a bug ' +
91+
'in React related to the return pointer. Please file an issue.');
92+
}
93+
}
94+
}
95+
}
96+
if (a.stateNode.current === a) {
97+
// We've determined that A is the current branch.
98+
return fiber;
99+
}
100+
// Otherwise B has to be current branch.
101+
return alternate;
102+
}
103+
104+
module.exports = findCurrentFiberUsingSlowPath;

0 commit comments

Comments
 (0)