Skip to content

Commit

Permalink
[Refactor] Follow ShallowWrapper contains pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh authored and ljharb committed Dec 9, 2017
1 parent 16ea7d4 commit 7cb7a06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
27 changes: 11 additions & 16 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,15 @@ class ReactWrapper {
* @returns {Boolean}
*/
contains(nodeOrNodes) {
const isArray = Array.isArray(nodeOrNodes);
let predicate;
const adapter = getAdapter(this[OPTIONS]);

if (isArray) {
const rstNodes = nodeOrNodes.map(getAdapter().elementToNode);
predicate = other => containsChildrenSubArray(nodeEqual, other, rstNodes);
} else {
const rstNode = getAdapter().elementToNode(nodeOrNodes);
predicate = other => nodeEqual(rstNode, other);
}
const predicate = Array.isArray(nodeOrNodes)
? other => containsChildrenSubArray(
nodeEqual,
other,
nodeOrNodes.map(adapter.elementToNode),
)
: other => nodeEqual(adapter.elementToNode(nodeOrNodes), other);

return findWhereUnwrapped(this, predicate).length > 0;
}
Expand All @@ -408,7 +407,7 @@ class ReactWrapper {
* @returns {Boolean}
*/
containsMatchingElement(node) {
const rstNode = getAdapter().elementToNode(node);
const rstNode = getAdapter(this[OPTIONS]).elementToNode(node);
const predicate = other => nodeMatches(rstNode, other, (a, b) => a <= b);
return findWhereUnwrapped(this, predicate).length > 0;
}
Expand Down Expand Up @@ -436,9 +435,7 @@ class ReactWrapper {
throw new TypeError('nodes should be an Array');
}

const rstNodes = nodes.map(getAdapter().elementToNode);

return rstNodes.every(rstNode => this.containsMatchingElement(rstNode));
return nodes.every(node => this.containsMatchingElement(node));
}

/**
Expand All @@ -460,9 +457,7 @@ class ReactWrapper {
* @returns {Boolean}
*/
containsAnyMatchingElements(nodes) {
if (!Array.isArray(nodes)) { return false; }
const rstNodes = nodes.map(getAdapter().elementToNode);
return rstNodes.some(rstNode => this.containsMatchingElement(rstNode));
return Array.isArray(nodes) && nodes.some(node => this.containsMatchingElement(node));
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ class ShallowWrapper {
* @returns {Boolean}
*/
containsMatchingElement(node) {
const predicate = other => nodeMatches(node, other, (a, b) => a <= b);
const rstNode = getAdapter().elementToNode(node);
const predicate = other => nodeMatches(rstNode, other, (a, b) => a <= b);
return findWhereUnwrapped(this, predicate).length > 0;
}

Expand Down Expand Up @@ -564,7 +565,10 @@ class ShallowWrapper {
* @returns {Boolean}
*/
matchesElement(node) {
return this.single('matchesElement', () => nodeMatches(node, this.getNodeInternal(), (a, b) => a <= b));
return this.single('matchesElement', () => {
const rstNode = getAdapter().elementToNode(node);
return nodeMatches(rstNode, this.getNodeInternal(), (a, b) => a <= b);
});
}

/**
Expand Down

0 comments on commit 7cb7a06

Please sign in to comment.