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 to call cDU when an instance calls setState #1742

Merged
merged 1 commit into from
Aug 11, 2018
Merged
Show file tree
Hide file tree
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
69 changes: 69 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4905,4 +4905,73 @@ describeWithDOM('mount', () => {
expect(root.childAt(0).children().debug()).to.equal('<span />\n\n\n<span />');
});
});

describe('lifecycles', () => {
it('should call `componentDidUpdate` when component’s `setState` is called', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'init',
};
}

componentDidUpdate() {}

onChange() {
// enzyme can't handle the update because `this` is a ReactComponent instance,
// not a ShallowWrapper instance.
this.setState({ foo: 'onChange update' });
}

render() {
return <div>{this.state.foo}</div>;
}
}
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');

const wrapper = mount(<Foo />);
wrapper.setState({ foo: 'wrapper setState update' });
expect(wrapper.state('foo')).to.equal('wrapper setState update');
expect(spy).to.have.property('callCount', 1);
wrapper.instance().onChange();
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 2);
});

it('should call `componentDidUpdate` when component’s `setState` is called through a bound method', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'init',
};
this.onChange = this.onChange.bind(this);
}

componentDidUpdate() {}

onChange() {
// enzyme can't handle the update because `this` is a ReactComponent instance,
// not a ShallowWrapper instance.
this.setState({ foo: 'onChange update' });
}

render() {
return (
<div>
{this.state.foo}
<button onClick={this.onChange}>click</button>
</div>
);
}
}
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');

const wrapper = mount(<Foo />);
wrapper.find('button').prop('onClick')();
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 1);
});
});
});
69 changes: 69 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4763,6 +4763,75 @@ describe('shallow', () => {
});
});

context('component instance', () => {
it('should call `componentDidUpdate` when component’s `setState` is called', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'init',
};
}

componentDidUpdate() {}

onChange() {
// enzyme can't handle the update because `this` is a ReactComponent instance,
// not a ShallowWrapper instance.
this.setState({ foo: 'onChange update' });
}

render() {
return <div>{this.state.foo}</div>;
}
}
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');

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);
wrapper.instance().onChange();
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 2);
});

it('should call `componentDidUpdate` when component’s `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.

class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'init',
};
this.onChange = this.onChange.bind(this);
}

componentDidUpdate() {}

onChange() {
// enzyme can't handle the update because `this` is a ReactComponent instance,
// not a ShallowWrapper instance.
this.setState({ foo: 'onChange update' });
}

render() {
return (
<div>
{this.state.foo}
<button onClick={this.onChange}>click</button>
</div>
);
}
}
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');

const wrapper = shallow(<Foo />);
wrapper.find('button').prop('onClick')();
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 1);
});
});

describeIf(is('>= 16'), 'support getSnapshotBeforeUpdate', () => {
it('should call getSnapshotBeforeUpdate and pass snapshot to componentDidUpdate', () => {
const spy = sinon.spy();
Expand Down
14 changes: 13 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

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.

}
if (
!options.disableLifecycleMethods
&& instance
Expand Down Expand Up @@ -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();
Expand Down