Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Fix] Convert nodes to RST nodes before comparing
  • Loading branch information
eddyerburgh authored and ljharb committed Dec 8, 2017
commit 161172d04a9f18e43cfbf0f00b58585acf164f2a
20 changes: 15 additions & 5 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,13 @@ class ReactWrapper {
* @returns {Boolean}
*/
contains(nodeOrNodes) {
const isArray = Array.isArray(nodeOrNodes);
const rstNodeOrNodes = isArray
? nodeOrNodes.map(getAdapter().elementToNode)
: getAdapter().elementToNode(nodeOrNodes);
const predicate = Array.isArray(nodeOrNodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line can use isArray now, I think?

? other => containsChildrenSubArray(nodeEqual, other, nodeOrNodes)
: other => nodeEqual(nodeOrNodes, other);
? other => containsChildrenSubArray(nodeEqual, other, rstNodeOrNodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably at this point, rather than two paired multiline ternaries, this would be better as an if/else branch.

: other => nodeEqual(rstNodeOrNodes, other);
return findWhereUnwrapped(this, predicate).length > 0;
}

Expand All @@ -397,7 +401,8 @@ class ReactWrapper {
* @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 All @@ -424,7 +429,9 @@ class ReactWrapper {
throw new TypeError('nodes should be an Array');
}

return nodes.every(node => this.containsMatchingElement(node));
const rstNodes = nodes.map(getAdapter().elementToNode);

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

/**
Expand All @@ -446,7 +453,10 @@ class ReactWrapper {
* @returns {Boolean}
*/
containsAnyMatchingElements(nodes) {
return Array.isArray(nodes) && nodes.some(node => this.containsMatchingElement(node));
const rstNodes = nodes.map(getAdapter().elementToNode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change makes .map be called before Array.isArray is called to ensure it's an array.

Let's change this to if (!Array.isArray(nodes)) { return false; } at the top of the function, and then we can do the entire check in a single some


return Array.isArray(rstNodes)
&& rstNodes.some(rstNode => this.containsMatchingElement(rstNode));
}

/**
Expand Down