Skip to content

Commit f281fef

Browse files
authored
Merge pull request #1812 from jgzuke/jgzuke-test-mount-simulate-bug
[Fix] `mount`: update after `simulateError`
2 parents 508a648 + 54cb6a6 commit f281fef

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5033,13 +5033,19 @@ describeWithDOM('mount', () => {
50335033
class ErrorBoundary extends React.Component {
50345034
constructor(...args) {
50355035
super(...args);
5036-
this.state = { throws: false };
5036+
this.state = {
5037+
throws: false,
5038+
didThrow: false,
5039+
};
50375040
}
50385041

50395042
componentDidCatch(error, info) {
50405043
const { spy } = this.props;
50415044
spy(error, info);
5042-
this.setState({ throws: false });
5045+
this.setState({
5046+
throws: false,
5047+
didThrow: true,
5048+
});
50435049
}
50445050

50455051
render() {
@@ -5049,6 +5055,9 @@ describeWithDOM('mount', () => {
50495055
<MaybeFragment>
50505056
<span>
50515057
<Thrower throws={throws} />
5058+
<div>
5059+
{this.state.didThrow ? 'HasThrown' : 'HasNotThrown'}
5060+
</div>
50525061
</span>
50535062
</MaybeFragment>
50545063
</div>
@@ -5096,6 +5105,18 @@ describeWithDOM('mount', () => {
50965105
});
50975106
});
50985107

5108+
it('rerenders on a simulated error', () => {
5109+
const wrapper = mount(<ErrorBoundary spy={sinon.stub()} />);
5110+
5111+
expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(0);
5112+
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(1);
5113+
5114+
expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();
5115+
5116+
expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(1);
5117+
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(0);
5118+
});
5119+
50995120
it('catches errors during render', () => {
51005121
const spy = sinon.spy();
51015122
const wrapper = mount(<ErrorBoundary spy={spy} />);

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5067,13 +5067,19 @@ describe('shallow', () => {
50675067
class ErrorBoundary extends React.Component {
50685068
constructor(...args) {
50695069
super(...args);
5070-
this.state = { throws: false };
5070+
this.state = {
5071+
throws: false,
5072+
didThrow: false,
5073+
};
50715074
}
50725075

50735076
componentDidCatch(error, info) {
50745077
const { spy } = this.props;
50755078
spy(error, info);
5076-
this.setState({ throws: false });
5079+
this.setState({
5080+
throws: false,
5081+
didThrow: true,
5082+
});
50775083
}
50785084

50795085
render() {
@@ -5083,6 +5089,9 @@ describe('shallow', () => {
50835089
<MaybeFragment>
50845090
<span>
50855091
<Thrower throws={throws} />
5092+
<div>
5093+
{this.state.didThrow ? 'HasThrown' : 'HasNotThrown'}
5094+
</div>
50865095
</span>
50875096
</MaybeFragment>
50885097
</div>
@@ -5124,6 +5133,18 @@ describe('shallow', () => {
51245133
});
51255134
});
51265135

5136+
it('rerenders on a simulated error', () => {
5137+
const wrapper = shallow(<ErrorBoundary spy={sinon.stub()} />);
5138+
5139+
expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(0);
5140+
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(1);
5141+
5142+
expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();
5143+
5144+
expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(1);
5145+
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(0);
5146+
});
5147+
51275148
it('does not catch errors during shallow render', () => {
51285149
const spy = sinon.spy();
51295150
const wrapper = shallow(<ErrorBoundary spy={spy} />);
@@ -5141,6 +5162,9 @@ describe('shallow', () => {
51415162
expect(() => thrower.dive()).to.throw(errorToThrow);
51425163

51435164
expect(spy).to.have.property('callCount', 0);
5165+
5166+
expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(0);
5167+
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(1);
51445168
});
51455169
});
51465170
});

packages/enzyme/src/ReactWrapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ class ReactWrapper {
643643
const nodeHierarchy = [thisNode].concat(nodeParents(this, thisNode));
644644
renderer.simulateError(nodeHierarchy, rootNode, error);
645645

646+
this[ROOT].update();
646647
return this;
647648
});
648649
}

0 commit comments

Comments
 (0)