Skip to content

Commit b2672d7

Browse files
authored
Merge pull request #1811 from jgzuke/jgzuke-freeze-root-nodes
[Fix] Freeze ROOT_NODES for child wrappers
2 parents 4dd2780 + 53be3e8 commit b2672d7

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3924,15 +3924,13 @@ describeWithDOM('mount', () => {
39243924
</div>`);
39253925
expect(bChild).to.have.lengthOf(1);
39263926

3927-
/*
39283927
const bChildParents = bChild.parents('.b');
39293928
expect(bChildParents.debug()).to.equal(`<div className="b">
39303929
<div>
39313930
B child
39323931
</div>
39333932
</div>`);
39343933
expect(bChildParents).to.have.lengthOf(1);
3935-
*/
39363934

39373935
const aChildParents = aChild.parents('.a');
39383936
expect(aChildParents.debug()).to.equal(`<div className="a">

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,15 +3587,13 @@ describe('shallow', () => {
35873587
</div>`);
35883588
expect(bChild).to.have.lengthOf(1);
35893589

3590-
/*
35913590
const bChildParents = bChild.parents('.b');
35923591
expect(bChildParents.debug()).to.equal(`<div className="b">
35933592
<div>
35943593
B child
35953594
</div>
35963595
</div>`);
35973596
expect(bChildParents).to.have.lengthOf(1);
3598-
*/
35993597

36003598
const aChildParents = aChild.parents('.a');
36013599
expect(aChildParents.debug()).to.equal(`<div className="a">

packages/enzyme/src/ReactWrapper.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const RENDERER = sym('__renderer__');
3131
const UNRENDERED = sym('__unrendered__');
3232
const ROOT = sym('__root__');
3333
const OPTIONS = sym('__options__');
34+
const ROOT_NODES = sym('__rootNodes__');
3435

3536
/**
3637
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
@@ -57,8 +58,18 @@ function filterWhereUnwrapped(wrapper, predicate) {
5758
return wrapper.wrap(wrapper.getNodesInternal().filter(predicate).filter(Boolean));
5859
}
5960

61+
function getRootNodeInternal(wrapper) {
62+
if (wrapper[ROOT].length !== 1) {
63+
throw new Error('getRootNodeInternal(wrapper) can only be called when wrapper wraps one node');
64+
}
65+
if (wrapper[ROOT] !== wrapper) {
66+
return wrapper[ROOT_NODES][0];
67+
}
68+
return wrapper[ROOT][NODE];
69+
}
70+
6071
function nodeParents(wrapper, node) {
61-
return parentsOfNode(node, wrapper[ROOT].getNodeInternal());
72+
return parentsOfNode(node, getRootNodeInternal(wrapper));
6273
}
6374

6475
function privateSetNodes(wrapper, nodes) {
@@ -102,6 +113,7 @@ class ReactWrapper {
102113
privateSet(this, RENDERER, root[RENDERER]);
103114
privateSet(this, ROOT, root);
104115
privateSetNodes(this, nodes);
116+
privateSet(this, ROOT_NODES, root[NODES]);
105117
}
106118
privateSet(this, OPTIONS, root ? root[OPTIONS] : options);
107119
}
@@ -639,7 +651,7 @@ class ReactWrapper {
639651
throw new TypeError('your adapter does not support `simulateError`. Try upgrading it!');
640652
}
641653

642-
const rootNode = this[ROOT].getNodeInternal();
654+
const rootNode = getRootNodeInternal(this);
643655
const nodeHierarchy = [thisNode].concat(nodeParents(this, thisNode));
644656
renderer.simulateError(nodeHierarchy, rootNode, error);
645657

packages/enzyme/src/ShallowWrapper.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const UNRENDERED = sym('__unrendered__');
3737
const ROOT = sym('__root__');
3838
const OPTIONS = sym('__options__');
3939
const SET_STATE = sym('__setState__');
40+
const ROOT_NODES = sym('__rootNodes__');
41+
4042
/**
4143
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
4244
* function.
@@ -144,6 +146,12 @@ function getRootNode(node) {
144146
}
145147

146148
function getRootNodeInternal(wrapper) {
149+
if (wrapper[ROOT].length !== 1) {
150+
throw new Error('getRootNodeInternal(wrapper) can only be called when wrapper wraps one node');
151+
}
152+
if (wrapper[ROOT] !== wrapper) {
153+
return wrapper[ROOT_NODES][0];
154+
}
147155
return wrapper[ROOT][NODE];
148156
}
149157

@@ -219,6 +227,7 @@ class ShallowWrapper {
219227
privateSet(this, RENDERER, root[RENDERER]);
220228
privateSetNodes(this, nodes);
221229
privateSet(this, OPTIONS, root[OPTIONS]);
230+
privateSet(this, ROOT_NODES, root[NODES]);
222231
}
223232
}
224233

0 commit comments

Comments
 (0)