Skip to content

Commit d065042

Browse files
jgzukeljharb
authored andcommitted
[Fix] mount: simulateError: update afterwards
1 parent c6632de commit d065042

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5033,22 +5033,31 @@ 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() {
5046-
const { throws } = this.state;
5052+
const { throws, didThrow } = this.state;
50475053
return (
50485054
<div>
50495055
<MaybeFragment>
50505056
<span>
50515057
<Thrower throws={throws} />
5058+
<main>
5059+
{didThrow ? 'HasThrown' : 'HasNotThrown'}
5060+
</main>
50525061
</span>
50535062
</MaybeFragment>
50545063
</div>
@@ -5096,6 +5105,16 @@ 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('main').text()).to.equal('HasNotThrown');
5112+
5113+
expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();
5114+
5115+
expect(wrapper.find('main').text()).to.equal('HasThrown');
5116+
});
5117+
50995118
it('catches errors during render', () => {
51005119
const spy = sinon.spy();
51015120
const wrapper = mount(<ErrorBoundary spy={spy} />);

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5067,22 +5067,31 @@ 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() {
5080-
const { throws } = this.state;
5086+
const { throws, didThrow } = this.state;
50815087
return (
50825088
<div>
50835089
<MaybeFragment>
50845090
<span>
50855091
<Thrower throws={throws} />
5092+
<main>
5093+
{didThrow ? 'HasThrown' : 'HasNotThrown'}
5094+
</main>
50865095
</span>
50875096
</MaybeFragment>
50885097
</div>
@@ -5124,6 +5133,16 @@ 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('main').text()).to.equal('HasNotThrown');
5140+
5141+
expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();
5142+
5143+
expect(wrapper.find('main').text()).to.equal('HasThrown');
5144+
});
5145+
51275146
it('does not catch errors during shallow render', () => {
51285147
const spy = sinon.spy();
51295148
const wrapper = shallow(<ErrorBoundary spy={spy} />);
@@ -5141,6 +5160,8 @@ describe('shallow', () => {
51415160
expect(() => thrower.dive()).to.throw(errorToThrow);
51425161

51435162
expect(spy).to.have.property('callCount', 0);
5163+
5164+
expect(wrapper.find('main').text()).to.equal('HasNotThrown');
51445165
});
51455166
});
51465167
});

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)