diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index e3f7e74aa..29955d4b0 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -3924,7 +3924,6 @@ describeWithDOM('mount', () => { `); expect(bChild).to.have.lengthOf(1); - /* const bChildParents = bChild.parents('.b'); expect(bChildParents.debug()).to.equal(`
@@ -3932,7 +3931,6 @@ describeWithDOM('mount', () => {
`); expect(bChildParents).to.have.lengthOf(1); - */ const aChildParents = aChild.parents('.a'); expect(aChildParents.debug()).to.equal(`
diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 17359a2f6..3a41fb2c0 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -3587,7 +3587,6 @@ describe('shallow', () => {
`); expect(bChild).to.have.lengthOf(1); - /* const bChildParents = bChild.parents('.b'); expect(bChildParents.debug()).to.equal(`
@@ -3595,7 +3594,6 @@ describe('shallow', () => {
`); expect(bChildParents).to.have.lengthOf(1); - */ const aChildParents = aChild.parents('.a'); expect(aChildParents.debug()).to.equal(`
diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index 72479ef48..1fe7aa5e0 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -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 @@ -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) { @@ -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); } @@ -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); diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index b53171963..0b3dad11f 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -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. @@ -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]; } @@ -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]); } }