Skip to content

Commit

Permalink
add .getNode and .getNodes, deprecate .update
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Oct 24, 2016
1 parent 8e9ace4 commit d6a51b7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 42 deletions.
18 changes: 18 additions & 0 deletions src/ReactWrapper.jsx
Expand Up @@ -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<ReactElement>}
*/
getNodes() {
return this.nodes;
}

/**
* If the root component contained a ref, you can access it here
* and get a wrapper around it.
Expand Down
83 changes: 46 additions & 37 deletions src/ShallowWrapper.js
Expand Up @@ -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));
}

/**
Expand All @@ -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)));
}

/**
Expand All @@ -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<ReactElement>}
*/
getNodes() {
return this.initialNodes || [this.renderer.getRenderOutput()];
}

/**
* Gets the instance of the component being rendered as the root node passed into `shallow()`.
*
Expand Down Expand Up @@ -201,7 +213,6 @@ class ShallowWrapper {
) {
instance.componentDidUpdate(prevProps, state, prevContext);
}
this.update();
}
if (originalComponentWillReceiveProps) {
instance.componentWillReceiveProps = originalComponentWillReceiveProps;
Expand Down Expand Up @@ -255,7 +266,6 @@ class ShallowWrapper {
this.single('setState', () => {
withSetStateAllowed(() => {
this.instance().setState(state, callback);
this.update();
});
});
return this;
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -547,7 +557,6 @@ class ShallowWrapper {
batchedUpdates(() => {
handler(...args);
});
this.root.update();
});
}
return this;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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));
}

/**
Expand All @@ -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
);
Expand All @@ -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
);
Expand All @@ -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);
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -822,7 +831,7 @@ class ShallowWrapper {
*/
every(selector) {
const predicate = buildPredicate(selector);
return this.nodes.every(predicate);
return this.getNodes().every(predicate);
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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);
Expand All @@ -870,7 +879,7 @@ class ShallowWrapper {
* @returns {ReactElement}
*/
get(index) {
return this.nodes[index];
return this.getNodes()[index];
}

/**
Expand All @@ -880,7 +889,7 @@ class ShallowWrapper {
* @returns {ShallowWrapper}
*/
at(index) {
return this.wrap(this.nodes[index]);
return this.wrap(this.getNodes()[index]);
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -949,7 +958,7 @@ class ShallowWrapper {
* @returns {String}
*/
debug() {
return debugNodes(this.nodes);
return debugNodes(this.getNodes());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions test/ShallowWrapper-spec.jsx
Expand Up @@ -2166,7 +2166,7 @@ describe('shallow', () => {
</div>
);

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);
Expand Down Expand Up @@ -2415,10 +2415,10 @@ describe('shallow', () => {
<div className="bar baz" />
</div>
);
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());
});
});

Expand Down

0 comments on commit d6a51b7

Please sign in to comment.