-
-
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 to call cDU when an instance calls setState #1742
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 |
---|---|---|
|
@@ -38,6 +38,7 @@ const RENDERER = sym('__renderer__'); | |
const UNRENDERED = sym('__unrendered__'); | ||
const ROOT = sym('__root__'); | ||
const OPTIONS = sym('__options__'); | ||
const SET_STATE = sym('__setState__'); | ||
/** | ||
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate | ||
* function. | ||
|
@@ -170,6 +171,13 @@ class ShallowWrapper { | |
privateSet(this, RENDERER, renderer); | ||
this[RENDERER].render(nodes, options.context); | ||
const { instance } = this[RENDERER].getNode(); | ||
const adapter = getAdapter(this[OPTIONS]); | ||
const lifecycles = getAdapterLifecycles(adapter); | ||
// Ensure to call componentDidUpdate when instance.setState is called | ||
if (instance && lifecycles.componentDidUpdate.onSetState && !instance[SET_STATE]) { | ||
privateSet(instance, SET_STATE, instance.setState); | ||
instance.setState = (...args) => this.setState(...args); | ||
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. With this change i am now seeing alot of failing tests which have a setState in componentDidMount. 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. See #1756. |
||
} | ||
if ( | ||
!options.disableLifecycleMethods | ||
&& instance | ||
|
@@ -438,7 +446,11 @@ class ShallowWrapper { | |
} | ||
// We don't pass the setState callback here | ||
// to guarantee to call the callback after finishing the render | ||
instance.setState(state); | ||
if (instance[SET_STATE]) { | ||
instance[SET_STATE](state); | ||
} else { | ||
instance.setState(state); | ||
} | ||
if (spy) { | ||
shouldRender = spy.getLastReturnValue(); | ||
spy.restore(); | ||
|
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.
This second test is essentially the same as the first test above, but this one adds a button. Do we need to test that use case since it's not very different than the first?
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.
This test is to verify to work well with a bound function.
this.onChange = this.onChange.bind(this);
My first implementation didn't pass this.