Skip to content

Fix to call cDU when an instance calls setState#1742

Merged
ljharb merged 1 commit intoenzymejs:masterfrom
koba04:fix-to-call-cDU-from-instance
Aug 11, 2018
Merged

Fix to call cDU when an instance calls setState#1742
ljharb merged 1 commit intoenzymejs:masterfrom
koba04:fix-to-call-cDU-from-instance

Conversation

@koba04
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

koba04 commented Aug 9, 2018

hmm, I should pass the test case

3b3ab38

@koba04
Copy link
Copy Markdown
Contributor Author

koba04 commented Aug 9, 2018

Fixed #1742 (comment)

});

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

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', () => {
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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'

@ajbogh
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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.

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

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
Copy Markdown
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

None yet

Development

Successfully merging this pull request may close these issues.

componentDidUpdate is not called for shallow rendered component

4 participants