-
-
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
Conversation
stub instance.setState when the component is mounting Fixes enzymejs#1452.
hmm, I should pass the test case |
Fixed #1742 (comment) |
@@ -1468,7 +1468,7 @@ describe('shallow', () => { | |||
}); | |||
}); | |||
|
|||
it('should call componentWillReceiveProps for new renders', () => { | |||
it.skip('should call componentWillReceiveProps for new renders', () => { |
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.
Do you want to skip this?
expect(spy).to.have.property('callCount', 2); | ||
}); | ||
|
||
it('should call `componentDidUpdate` when component’s `setState` is called through a bound method', () => { |
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.
const wrapper = shallow(<Foo />); | ||
wrapper.setState({ foo: 'wrapper setState update' }); | ||
expect(wrapper.state('foo')).to.equal('wrapper setState update'); | ||
expect(spy).to.have.property('callCount', 1); |
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 believe the lines above this comment should be one test, expecting that a wrapper.setState
call works with componentDidUpdate, while the lines below this should be a separate test to ensure that the instance methods can set their own state and cause a componentDidUpdate as well.
Test 1: 'should call componentDidUpdate
when a wrapper’s setState
is called'
Test 2: 'should call componentDidUpdate
when component’s setState
is called'
f12be61
to
02c6868
Compare
LGTM! |
5b2d442
to
a50570d
Compare
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!
Thanks! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
// 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 comment
The 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. Method "setState" is only meant to be run on a single node. undefined found instead.
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.
See #1756.
Fix #1452
This is not an ideal fix, but works 😅
(This PR is based on #1741 to run all tests)