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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to call cDU when an instance calls setState #1742

Merged
merged 1 commit into from
Aug 11, 2018

Conversation

koba04
Copy link
Contributor

@koba04 koba04 commented Aug 9, 2018

Fix #1452

This is not an ideal fix, but works 馃槄

(This PR is based on #1741 to run all tests)

stub instance.setState when the component is mounting

Fixes enzymejs#1452.
@koba04 koba04 changed the title Fix to call c du from instance Fix to call cDU when an instance calls setState Aug 9, 2018
@koba04
Copy link
Contributor Author

koba04 commented Aug 9, 2018

hmm, I should pass the test case

3b3ab38

@koba04
Copy link
Contributor Author

koba04 commented Aug 9, 2018

Fixed #1742 (comment)

@@ -1468,7 +1468,7 @@ describe('shallow', () => {
});
});

it('should call componentWillReceiveProps for new renders', () => {
it.skip('should call componentWillReceiveProps for new renders', () => {
Copy link

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鈥檚 `setState` is called through a bound method', () => {
Copy link

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?

Copy link
Contributor Author

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);
Copy link

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鈥檚 setState is called'

Test 2: 'should call componentDidUpdate when component鈥檚 setState is called'

@ajbogh
Copy link

ajbogh commented Aug 10, 2018

LGTM!

@ljharb ljharb force-pushed the fix-to-call-cDU-from-instance branch from 5b2d442 to a50570d Compare August 11, 2018 07:46
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks!

@koba04
Copy link
Contributor Author

koba04 commented Aug 12, 2018

Thanks!

@koba04 koba04 deleted the fix-to-call-cDU-from-instance branch August 12, 2018 03:19
@ajbogh

This comment has been minimized.

@ljharb

This comment has been minimized.

@ajbogh

This comment has been minimized.

@ljharb ljharb added this to Lifecycles in React 16 Aug 15, 2018
// 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);

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.

Copy link
Member

Choose a reason for hiding this comment

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

See #1756.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
React 16
  
Lifecycles
Development

Successfully merging this pull request may close these issues.

componentDidUpdate is not called for shallow rendered component
4 participants