diff --git a/src/ReactWrapper.jsx b/src/ReactWrapper.jsx index 6af2990c1..877608cef 100644 --- a/src/ReactWrapper.jsx +++ b/src/ReactWrapper.jsx @@ -107,6 +107,24 @@ class ReactWrapper { ); } + /** + * Returns the wrapper's underlying node. + * + * @return {ReactElement} + */ + getNode() { + return this.node; + } + + /** + * Returns the wrapper's underlying nodes. + * + * @return {Array} + */ + getNodes() { + return this.nodes; + } + /** * If the root component contained a ref, you can access it here * and get a wrapper around it. diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index 7ae106f2a..d86e9f3e5 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -46,7 +46,7 @@ import { * @returns {ShallowWrapper} */ function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) { - return wrapper.flatMap(n => filter(n.node, predicate)); + return wrapper.flatMap(n => filter(n.getNode(), predicate)); } /** @@ -58,7 +58,7 @@ function findWhereUnwrapped(wrapper, predicate, filter = treeFilter) { * @returns {ShallowWrapper} */ function filterWhereUnwrapped(wrapper, predicate) { - return wrapper.wrap(compact(wrapper.nodes.filter(predicate))); + return wrapper.wrap(compact(wrapper.getNodes().filter(predicate))); } /** @@ -84,34 +84,46 @@ class ShallowWrapper { } }); }); - Object.defineProperty(this, 'node', { - get() { - return this.renderer.getRenderOutput(); - }, - }); - Object.defineProperty(this, 'nodes', { - get() { - return [this.renderer.getRenderOutput()]; - }, - }); this.length = 1; } else { this.root = root; this.unrendered = null; this.renderer = null; if (!Array.isArray(nodes)) { - this.node = nodes; - this.nodes = [nodes]; + this.initialNode = nodes; + this.initialNodes = [nodes]; } else { - this.node = nodes[0]; - this.nodes = nodes; + this.initialNode = nodes[0]; + this.initialNodes = nodes; } - this.length = this.nodes.length; + this.length = this.initialNodes.length; } this.options = options; this.complexSelector = new ComplexSelector(buildPredicate, findWhereUnwrapped, childrenOfNode); } + /** + * If the current wrapper is wrapping the root component, returns the + * root component's latest render output. Otherwise, returns the node + * the wrapper was created with. + * + * @return {ReactElement} + */ + getNode() { + return this.initialNode || this.renderer.getRenderOutput(); + } + + /** + * If the current wrapper is wrapping the root component, returns the + * root component's latest render output wrapped in an array. Otherwise, + * returns the nodes the wrapper was created with. + * + * @return {Array} + */ + getNodes() { + return this.initialNodes || [this.renderer.getRenderOutput()]; + } + /** * Gets the instance of the component being rendered as the root node passed into `shallow()`. * @@ -201,7 +213,6 @@ class ShallowWrapper { ) { instance.componentDidUpdate(prevProps, state, prevContext); } - this.update(); } if (originalComponentWillReceiveProps) { instance.componentWillReceiveProps = originalComponentWillReceiveProps; @@ -255,7 +266,6 @@ class ShallowWrapper { this.single('setState', () => { withSetStateAllowed(() => { this.instance().setState(state, callback); - this.update(); }); }); return this; @@ -393,7 +403,7 @@ class ShallowWrapper { * @returns {Boolean} */ equals(node) { - return this.single('equals', () => nodeEqual(this.node, node)); + return this.single('equals', () => nodeEqual(this.getNode(), node)); } /** @@ -414,7 +424,7 @@ class ShallowWrapper { * @returns {Boolean} */ matchesElement(node) { - return this.single('matchesElement', () => nodeEqual(node, this.node, (a, b) => a <= b)); + return this.single('matchesElement', () => nodeEqual(node, this.getNode(), (a, b) => a <= b)); } /** @@ -547,7 +557,6 @@ class ShallowWrapper { batchedUpdates(() => { handler(...args); }); - this.root.update(); }); } return this; @@ -620,7 +629,7 @@ class ShallowWrapper { * @returns {ShallowWrapper} */ children(selector) { - const allChildren = this.flatMap(n => childrenOfNode(n.node)); + const allChildren = this.flatMap(n => childrenOfNode(n.getNode())); return selector ? allChildren.filter(selector) : allChildren; } @@ -644,7 +653,7 @@ class ShallowWrapper { * @returns {ShallowWrapper} */ parents(selector) { - const allParents = this.wrap(this.single('parents', n => parentsOfNode(n, this.root.node))); + const allParents = this.wrap(this.single('parents', n => parentsOfNode(n, this.root.getNode()))); return selector ? allParents.filter(selector) : allParents; } @@ -745,7 +754,7 @@ class ShallowWrapper { * @returns {ShallowWrapper} */ forEach(fn) { - this.nodes.forEach((n, i) => fn.call(this, this.wrap(n), i)); + this.getNodes().forEach((n, i) => fn.call(this, this.wrap(n), i)); return this; } @@ -757,7 +766,7 @@ class ShallowWrapper { * @returns {Array} */ map(fn) { - return this.nodes.map((n, i) => fn.call(this, this.wrap(n), i)); + return this.getNodes().map((n, i) => fn.call(this, this.wrap(n), i)); } /** @@ -769,7 +778,7 @@ class ShallowWrapper { * @returns {*} */ reduce(fn, initialValue) { - return this.nodes.reduce( + return this.getNodes().reduce( (accum, n, i) => fn.call(this, accum, this.wrap(n), i), initialValue ); @@ -784,7 +793,7 @@ class ShallowWrapper { * @returns {*} */ reduceRight(fn, initialValue) { - return this.nodes.reduceRight( + return this.getNodes().reduceRight( (accum, n, i) => fn.call(this, accum, this.wrap(n), i), initialValue ); @@ -801,7 +810,7 @@ class ShallowWrapper { throw new Error('ShallowWrapper::some() can not be called on the root'); } const predicate = buildPredicate(selector); - return this.nodes.some(predicate); + return this.getNodes().some(predicate); } /** @@ -811,7 +820,7 @@ class ShallowWrapper { * @returns {Boolean} */ someWhere(predicate) { - return this.nodes.some((n, i) => predicate.call(this, this.wrap(n), i)); + return this.getNodes().some((n, i) => predicate.call(this, this.wrap(n), i)); } /** @@ -822,7 +831,7 @@ class ShallowWrapper { */ every(selector) { const predicate = buildPredicate(selector); - return this.nodes.every(predicate); + return this.getNodes().every(predicate); } /** @@ -832,7 +841,7 @@ class ShallowWrapper { * @returns {Boolean} */ everyWhere(predicate) { - return this.nodes.every((n, i) => predicate.call(this, this.wrap(n), i)); + return this.getNodes().every((n, i) => predicate.call(this, this.wrap(n), i)); } /** @@ -844,7 +853,7 @@ class ShallowWrapper { * @returns {ShallowWrapper} */ flatMap(fn) { - const nodes = this.nodes.map((n, i) => fn.call(this, this.wrap(n), i)); + const nodes = this.getNodes().map((n, i) => fn.call(this, this.wrap(n), i)); const flattened = flatten(nodes, true); const uniques = unique(flattened); const compacted = compact(uniques); @@ -870,7 +879,7 @@ class ShallowWrapper { * @returns {ReactElement} */ get(index) { - return this.nodes[index]; + return this.getNodes()[index]; } /** @@ -880,7 +889,7 @@ class ShallowWrapper { * @returns {ShallowWrapper} */ at(index) { - return this.wrap(this.nodes[index]); + return this.wrap(this.getNodes()[index]); } /** @@ -926,7 +935,7 @@ class ShallowWrapper { `Method “${fnName}” is only meant to be run on a single node. ${this.length} found instead.` ); } - return callback.call(this, this.node); + return callback.call(this, this.getNode()); } /** @@ -949,7 +958,7 @@ class ShallowWrapper { * @returns {String} */ debug() { - return debugNodes(this.nodes); + return debugNodes(this.getNodes()); } /** diff --git a/test/ShallowWrapper-spec.jsx b/test/ShallowWrapper-spec.jsx index 688ca5b38..47ff4c660 100644 --- a/test/ShallowWrapper-spec.jsx +++ b/test/ShallowWrapper-spec.jsx @@ -2166,7 +2166,7 @@ describe('shallow', () => { ); - const nodes = wrapper.find('.foo').flatMap(w => w.children().nodes); + const nodes = wrapper.find('.foo').flatMap(w => w.children().getNodes()); expect(nodes.length).to.equal(6); expect(nodes.at(0).hasClass('bar')).to.equal(true); @@ -2415,10 +2415,10 @@ describe('shallow', () => {
); - expect(wrapper.find('.bar').get(0)).to.equal(wrapper.find('.foo').node); - expect(wrapper.find('.bar').get(1)).to.equal(wrapper.find('.bax').node); - expect(wrapper.find('.bar').get(2)).to.equal(wrapper.find('.bux').node); - expect(wrapper.find('.bar').get(3)).to.equal(wrapper.find('.baz').node); + expect(wrapper.find('.bar').get(0)).to.equal(wrapper.find('.foo').getNode()); + expect(wrapper.find('.bar').get(1)).to.equal(wrapper.find('.bax').getNode()); + expect(wrapper.find('.bar').get(2)).to.equal(wrapper.find('.bux').getNode()); + expect(wrapper.find('.bar').get(3)).to.equal(wrapper.find('.baz').getNode()); }); });