Skip to content

Commit

Permalink
[Fix] shallow: properly determine "should render" for PureComponents
Browse files Browse the repository at this point in the history
Fixes #2096.
  • Loading branch information
ljharb committed May 4, 2019
1 parent e1b10d9 commit 16b1f52
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -3402,6 +3402,41 @@ describeWithDOM('mount', () => {
});
});
});

it('does not infinitely loop when a PureComponent fires a noop setState in cDU', () => {
class Example extends PureComponent {
constructor(props) {
super(props);

this.renders = 0;
this.state = {
a: false,
b: false,
};
}

componentDidMount() {
this.setState({ b: false });
}

componentDidUpdate() {
this.setState({ b: false }); // eslint-disable-line react/no-did-update-set-state
}

render() {
this.renders += 1;
const { a, b } = this.state;
return <div>{`${a} ${b} ${this.renders}`}</div>;
}
}

const wrapper = mount(<Example />);
expect(wrapper.debug()).to.equal(`<Example>
<div>
false false 1
</div>
</Example>`);
});
});

describe('Own PureComponent implementation', () => {
Expand Down
33 changes: 33 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -3802,6 +3802,39 @@ describe('shallow', () => {
});
});
});

it('does not infinitely loop when a PureComponent fires a noop setState in cDU', () => {
class Example extends PureComponent {
constructor(props) {
super(props);

this.renders = 0;
this.state = {
a: false,
b: false,
};
}

componentDidMount() {
this.setState({ b: false });
}

componentDidUpdate() {
this.setState({ b: false }); // eslint-disable-line react/no-did-update-set-state
}

render() {
this.renders += 1;
const { a, b } = this.state;
return <div>{`${a} ${b} ${this.renders}`}</div>;
}
}

const wrapper = shallow(<Example />);
expect(wrapper.debug()).to.equal(`<div>
false false 1
</div>`);
});
});

describe('Own PureComponent implementation', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Expand Up @@ -778,7 +778,7 @@ class ShallowWrapper {
prevProps,
instance.props,
prevState,
statePayload,
{ ...prevState, ...statePayload },
);
}

Expand Down

0 comments on commit 16b1f52

Please sign in to comment.