Skip to content

Commit

Permalink
Merge pull request #1811 from jgzuke/jgzuke-freeze-root-nodes
Browse files Browse the repository at this point in the history
[Fix] Freeze ROOT_NODES for child wrappers
  • Loading branch information
ljharb authored Sep 21, 2018
2 parents 4dd2780 + 53be3e8 commit b2672d7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 0 additions & 2 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3924,15 +3924,13 @@ describeWithDOM('mount', () => {
</div>`);
expect(bChild).to.have.lengthOf(1);

/*
const bChildParents = bChild.parents('.b');
expect(bChildParents.debug()).to.equal(`<div className="b">
<div>
B child
</div>
</div>`);
expect(bChildParents).to.have.lengthOf(1);
*/

const aChildParents = aChild.parents('.a');
expect(aChildParents.debug()).to.equal(`<div className="a">
Expand Down
2 changes: 0 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3587,15 +3587,13 @@ describe('shallow', () => {
</div>`);
expect(bChild).to.have.lengthOf(1);

/*
const bChildParents = bChild.parents('.b');
expect(bChildParents.debug()).to.equal(`<div className="b">
<div>
B child
</div>
</div>`);
expect(bChildParents).to.have.lengthOf(1);
*/

const aChildParents = aChild.parents('.a');
expect(aChildParents.debug()).to.equal(`<div className="a">
Expand Down
16 changes: 14 additions & 2 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const RENDERER = sym('__renderer__');
const UNRENDERED = sym('__unrendered__');
const ROOT = sym('__root__');
const OPTIONS = sym('__options__');
const ROOT_NODES = sym('__rootNodes__');

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

function getRootNodeInternal(wrapper) {
if (wrapper[ROOT].length !== 1) {
throw new Error('getRootNodeInternal(wrapper) can only be called when wrapper wraps one node');
}
if (wrapper[ROOT] !== wrapper) {
return wrapper[ROOT_NODES][0];
}
return wrapper[ROOT][NODE];
}

function nodeParents(wrapper, node) {
return parentsOfNode(node, wrapper[ROOT].getNodeInternal());
return parentsOfNode(node, getRootNodeInternal(wrapper));
}

function privateSetNodes(wrapper, nodes) {
Expand Down Expand Up @@ -102,6 +113,7 @@ class ReactWrapper {
privateSet(this, RENDERER, root[RENDERER]);
privateSet(this, ROOT, root);
privateSetNodes(this, nodes);
privateSet(this, ROOT_NODES, root[NODES]);
}
privateSet(this, OPTIONS, root ? root[OPTIONS] : options);
}
Expand Down Expand Up @@ -639,7 +651,7 @@ class ReactWrapper {
throw new TypeError('your adapter does not support `simulateError`. Try upgrading it!');
}

const rootNode = this[ROOT].getNodeInternal();
const rootNode = getRootNodeInternal(this);
const nodeHierarchy = [thisNode].concat(nodeParents(this, thisNode));
renderer.simulateError(nodeHierarchy, rootNode, error);

Expand Down
9 changes: 9 additions & 0 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const UNRENDERED = sym('__unrendered__');
const ROOT = sym('__root__');
const OPTIONS = sym('__options__');
const SET_STATE = sym('__setState__');
const ROOT_NODES = sym('__rootNodes__');

/**
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
* function.
Expand Down Expand Up @@ -144,6 +146,12 @@ function getRootNode(node) {
}

function getRootNodeInternal(wrapper) {
if (wrapper[ROOT].length !== 1) {
throw new Error('getRootNodeInternal(wrapper) can only be called when wrapper wraps one node');
}
if (wrapper[ROOT] !== wrapper) {
return wrapper[ROOT_NODES][0];
}
return wrapper[ROOT][NODE];
}

Expand Down Expand Up @@ -219,6 +227,7 @@ class ShallowWrapper {
privateSet(this, RENDERER, root[RENDERER]);
privateSetNodes(this, nodes);
privateSet(this, OPTIONS, root[OPTIONS]);
privateSet(this, ROOT_NODES, root[NODES]);
}
}

Expand Down

0 comments on commit b2672d7

Please sign in to comment.