-
-
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
Fix setState regression #1771
Fix setState regression #1771
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -399,11 +399,11 @@ class ShallowWrapper { | |
* @param {Function} cb - callback function | ||
* @returns {ShallowWrapper} | ||
*/ | ||
setProps(props, callback = noop) { | ||
setProps(props, callback) { | ||
if (this[ROOT] !== this) { | ||
throw new Error('ShallowWrapper::setProps() can only be called on the root'); | ||
} | ||
if (typeof callback !== 'function') { | ||
if (callback && typeof callback !== 'function') { | ||
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. if you pass 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. What about function b(b = 'b') {
return b
}
b() // 'b'
b(null) // null
b(undefined) // 'b'
b(false) // false But I haven't poor much time in this patch, I could be wrong. 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. You're right that 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.
Good question, I have no idea. I was curious to see how React would behave, turns out, they explicitly support it for some reason I'm not aware of. 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. I've asked for the motivation here |
||
throw new TypeError('ShallowWrapper::setProps() expects a function as its second argument'); | ||
} | ||
this.rerender(props); | ||
|
@@ -424,14 +424,14 @@ class ShallowWrapper { | |
* @param {Function} cb - callback function | ||
* @returns {ShallowWrapper} | ||
*/ | ||
setState(state, callback = undefined) { | ||
setState(state, callback) { | ||
if (this[ROOT] !== this) { | ||
throw new Error('ShallowWrapper::setState() can only be called on the root'); | ||
} | ||
if (this.instance() === null || this[RENDERER].getNode().nodeType === 'function') { | ||
throw new Error('ShallowWrapper::setState() can only be called on class components'); | ||
} | ||
if (arguments.length > 1 && typeof callback !== 'function') { | ||
if (callback && typeof callback !== 'function') { | ||
throw new TypeError('ReactWrapper::setState() expects a function as its second argument'); | ||
} | ||
this.single('setState', () => { | ||
|
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.
either way, the default is required, so the
.length
of the function is correct.