Skip to content

1862/fix setprops on cdu #2007

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

Merged
merged 1 commit into from Feb 6, 2019
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
40 changes: 40 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,46 @@ describeWithDOM('mount', () => {
]);
});

describe('setProps should not call componentDidUpdate twice', () => {
it('first test case', () => {
class Dummy extends React.Component {
constructor(...args) {
super(...args);

this.state = {
someState: '',
};
}

componentWillReceiveProps({ myProp: someState }) {
this.setState({ someState });
}

componentDidUpdate() {}

render() {
const { myProp } = this.props;
const { someState } = this.state;
return (
<div>
myProp: {myProp}
someState: {someState}
</div>
);
}
}

const spy = sinon.spy(Dummy.prototype, 'componentDidUpdate');
const wrapper = mount(<Dummy />);
expect(spy).to.have.property('callCount', 0);
return new Promise((resolve) => {
wrapper.setProps({ myProp: 'Prop Value' }, resolve);
}).then(() => {
expect(spy).to.have.property('callCount', 1);
});
});
});

describeIf(is('> 0.13'), 'stateless function components', () => {
it('sets props for a component multiple times', () => {
const Foo = props => (
Expand Down
40 changes: 40 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,46 @@ describe('shallow', () => {
]);
});

describe('setProps should not call componentDidUpdate twice', () => {
it('first test case', () => {
class Dummy extends React.Component {
constructor(...args) {
super(...args);

this.state = {
someState: '',
};
}

componentWillReceiveProps({ myProp: someState }) {
this.setState({ someState });
}

componentDidUpdate() {}

render() {
const { myProp } = this.props;
const { someState } = this.state;
return (
<div>
myProp: {myProp}
someState: {someState}
</div>
);
}
}

const spy = sinon.spy(Dummy.prototype, 'componentDidUpdate');
const wrapper = shallow(<Dummy />);
expect(spy).to.have.property('callCount', 0);
return new Promise((resolve) => {
wrapper.setProps({ myProp: 'Prop Value' }, resolve);
}).then(() => {
expect(spy).to.have.property('callCount', 1);
});
});
});

describeIf(is('> 0.13'), 'stateless function components', () => {
it('sets props for a component multiple times', () => {
const Foo = props => (
Expand Down
4 changes: 3 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ class ShallowWrapper {
if (
lifecycles.componentDidUpdate
&& typeof instance.componentDidUpdate === 'function'
&& (!state || shallowEqual(state, this.instance().state))
) {
instance.componentDidUpdate(prevProps, state, snapshot);
}
Expand All @@ -498,7 +499,7 @@ class ShallowWrapper {
) {
if (lifecycles.componentDidUpdate.prevContext) {
instance.componentDidUpdate(prevProps, state, prevContext);
} else {
} else if (!state || shallowEqual(this.instance().state, state)) {
instance.componentDidUpdate(prevProps, state);
}
}
Expand Down Expand Up @@ -617,6 +618,7 @@ class ShallowWrapper {
statePayload,
);
}

// We don't pass the setState callback here
// to guarantee to call the callback after finishing the render
if (instance[SET_STATE]) {
Expand Down