-
-
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
Skip undefined props when comparing nodes #662
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- `.find` with an object with `undefined` values should throw Fixes #656.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,5 +104,6 @@ | |
"sinon": "^2.4.1", | ||
"webpack": "^1.13.3" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,9 @@ export function isCustomComponentElement(inst, adapter) { | |
} | ||
|
||
function propsOfNode(node) { | ||
return (node && node.props) || {}; | ||
return entries((node && node.props) || {}) | ||
.filter(([, value]) => typeof value !== 'undefined') | ||
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is rather ineffecient, creating as many objects as you have keys, I know this is a testing library, but if you have a lot of tests it starts to add up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I could mutate the accumulator since the overall reduce is pure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
export function typeOfNode(node) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { createParser } from 'rst-selector-parser'; | ||
import values from 'object.values'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import flatten from 'lodash/flatten'; | ||
import unique from 'lodash/uniq'; | ||
|
@@ -141,6 +142,10 @@ export function buildPredicate(selector) { | |
// If the selector is an non-empty object, treat the keys/values as props | ||
if (typeof selector === 'object') { | ||
if (!Array.isArray(selector) && selector !== null && !isEmpty(selector)) { | ||
const hasUndefinedValues = values(selector).some(value => value === undefined); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this one a test of the value itself but the others are testing typeof? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My habit is to always use typeof for safety; babel makes this unnecessary. I'll make them all be the same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's now consistent in the project. |
||
if (hasUndefinedValues) { | ||
throw new TypeError('Enzyme::Props can’t have `undefined` values. Try using ‘findWhere()’ instead.'); | ||
} | ||
return node => nodeMatchesObjectProps(node, selector); | ||
} | ||
throw new TypeError( | ||
|
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 don’t understand why we need this change. Don’t we throw if the received props contain undefined 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.
In
find
, yes, but I don't think that's the only place this is called.