-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sure findWhere doesn't get stuck on empty string #1995
Make sure findWhere doesn't get stuck on empty string #1995
Conversation
packages/enzyme/src/RSTTraversal.js
Outdated
@@ -40,7 +40,7 @@ export function hasClassName(node, className) { | |||
} | |||
|
|||
export function treeForEach(tree, fn) { | |||
if (tree !== null && tree !== false && typeof tree !== 'undefined') { | |||
if (tree !== null && tree !== false && typeof tree !== 'undefined' && tree !== '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to just use if (tree)
here, rather than blacklisting specific values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reporting and contributing. I think this is a good start. Though as newer versions of react come out, more and more valid "empty" values become natively supported by react. Ie emptyString, emptyArray, emptyFragments, undefined. Adding a fix for just an empty string seems like a good start and solves your problem but i think we need to consider other use cases including the ones i mentioned above.
@ljharb thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean, realistically here, this is deciding when to bypass calling childrenOfNode
.
A node can only have children if it's an object, right? Could this change be effectively if (isPrimitive(tree)) {
? (with function isPrimitive(val) { return !val || Object(val) !== val; }
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I tried this out and it looks like calling fn(tree)
on things like strings and numbers is a desired functionality. Here are some of the tests that fail (trimmed out some of the output):
1) RSTTraversal treeForEach does not get trapped from empty strings:
2) (uses jsdom) mount .findWhere(predicate) does not pass in null or false nodes:
AssertionError: expected [ Array(3) ] to deeply equal [ Array(4) ]
+ expected - actual
[
"<div>\n foo bar\n</div>"
true
]
+ [
+ "foo bar"
+ [null]
+ ]
]
at Context.<anonymous> (packages/enzyme-test-suite/test/ReactWrapper-spec.jsx:1880:30)
3) (uses jsdom) mount .findWhere(predicate) allows `.text()` to be called on text nodes:
4) shallow .contains(node) works with strings:
5) shallow .contains(node) works with numbers:
6) shallow .contains(node) works with nested strings & numbers:
7) shallow .findWhere(predicate) allows `.text()` to be called on text nodes:
AssertionError: expected [ Array(3) ] to deeply equal [ Array(4) ]
+ expected - actual
[
"<div>\n foo bar\n</div>"
"foo bar"
]
+ [
+ "foo bar"
+ "foo bar"
+ ]
]
at Context.<anonymous> (packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx:1829:31)
8) selectors (uses jsdom) mount :empty pseudo selector:
9) selectors shallow :empty pseudo selector:
AssertionError: expected {} to have a length of 0 but got 1
+ expected - actual
-1
+0
at Context.<anonymous> (packages/enzyme-test-suite/test/selector-spec.jsx:371:52)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it looks like we only want to prevent falsey values. I tried out if (tree) { fn(tree) }
and the tests will still pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, ok - let's do that then :-)
packages/enzyme/src/RSTTraversal.js
Outdated
@@ -40,7 +40,7 @@ export function hasClassName(node, className) { | |||
} | |||
|
|||
export function treeForEach(tree, fn) { | |||
if (tree !== null && tree !== false && typeof tree !== 'undefined') { | |||
if (tree !== null && tree !== false && typeof tree !== 'undefined' && tree !== '') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean, realistically here, this is deciding when to bypass calling childrenOfNode
.
A node can only have children if it's an object, right? Could this change be effectively if (isPrimitive(tree)) {
? (with function isPrimitive(val) { return !val || Object(val) !== val; }
)
5f9e696
to
ec3b89e
Compare
What
findWhere
test case that replicates the use case I ran intoRSTTraversal.treeForEach
to not callfn
on empty stringstreeForEach
test case that references empty stringsWhy
In the event that the DOM has an empty string, make sure
findWhere
can iterate past it and still find elements that are further down the tree.Links