Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
8 changes: 4 additions & 4 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ class ShallowWrapper {
* @param {Function} cb - callback function
* @returns {ShallowWrapper}
*/
setProps(props, callback = noop) {
setProps(props, callback) {
Copy link
Member

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.

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') {
Copy link
Member

Choose a reason for hiding this comment

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

if you pass undefined, the current code will replace it with noop - this would allow you to silently pass a falsy primitive (false, NaN, 0, empty string, null). What's the utility of this?

Copy link
Author

@oliviertassinari oliviertassinari Aug 18, 2018

Choose a reason for hiding this comment

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

What about null? The default won't kick in. React checks both:
https://github.com/facebook/react/blob/5031ebf6beddf88cac15f4d2c9e91f8dbb91d59d/packages/react-reconciler/src/ReactFiberClassComponent.js#L179

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.

Copy link
Member

Choose a reason for hiding this comment

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

You're right that null won't trigger the default; but i'm confused why you'd ever pass in a null in the first place.

Copy link
Author

@oliviertassinari oliviertassinari Aug 18, 2018

Choose a reason for hiding this comment

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

i'm confused why you'd ever pass in a null in the first place.

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.
https://github.com/facebook/react/blob/5031ebf6beddf88cac15f4d2c9e91f8dbb91d59d/packages/react-reconciler/src/ReactFiberClassComponent.js#L179

Copy link
Member

Choose a reason for hiding this comment

The 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);
Expand All @@ -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', () => {
Expand Down