Skip to content

Commit

Permalink
Merge pull request #1812 from jgzuke/jgzuke-test-mount-simulate-bug
Browse files Browse the repository at this point in the history
[Fix] `mount`: update after `simulateError`
  • Loading branch information
ljharb authored Sep 19, 2018
2 parents 508a648 + 54cb6a6 commit f281fef
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
25 changes: 23 additions & 2 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5033,13 +5033,19 @@ describeWithDOM('mount', () => {
class ErrorBoundary extends React.Component {
constructor(...args) {
super(...args);
this.state = { throws: false };
this.state = {
throws: false,
didThrow: false,
};
}

componentDidCatch(error, info) {
const { spy } = this.props;
spy(error, info);
this.setState({ throws: false });
this.setState({
throws: false,
didThrow: true,
});
}

render() {
Expand All @@ -5049,6 +5055,9 @@ describeWithDOM('mount', () => {
<MaybeFragment>
<span>
<Thrower throws={throws} />
<div>
{this.state.didThrow ? 'HasThrown' : 'HasNotThrown'}
</div>
</span>
</MaybeFragment>
</div>
Expand Down Expand Up @@ -5096,6 +5105,18 @@ describeWithDOM('mount', () => {
});
});

it('rerenders on a simulated error', () => {
const wrapper = mount(<ErrorBoundary spy={sinon.stub()} />);

expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(0);
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(1);

expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();

expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(1);
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(0);
});

it('catches errors during render', () => {
const spy = sinon.spy();
const wrapper = mount(<ErrorBoundary spy={spy} />);
Expand Down
28 changes: 26 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5067,13 +5067,19 @@ describe('shallow', () => {
class ErrorBoundary extends React.Component {
constructor(...args) {
super(...args);
this.state = { throws: false };
this.state = {
throws: false,
didThrow: false,
};
}

componentDidCatch(error, info) {
const { spy } = this.props;
spy(error, info);
this.setState({ throws: false });
this.setState({
throws: false,
didThrow: true,
});
}

render() {
Expand All @@ -5083,6 +5089,9 @@ describe('shallow', () => {
<MaybeFragment>
<span>
<Thrower throws={throws} />
<div>
{this.state.didThrow ? 'HasThrown' : 'HasNotThrown'}
</div>
</span>
</MaybeFragment>
</div>
Expand Down Expand Up @@ -5124,6 +5133,18 @@ describe('shallow', () => {
});
});

it('rerenders on a simulated error', () => {
const wrapper = shallow(<ErrorBoundary spy={sinon.stub()} />);

expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(0);
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(1);

expect(() => wrapper.find(Thrower).simulateError(errorToThrow)).not.to.throw();

expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(1);
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(0);
});

it('does not catch errors during shallow render', () => {
const spy = sinon.spy();
const wrapper = shallow(<ErrorBoundary spy={spy} />);
Expand All @@ -5141,6 +5162,9 @@ describe('shallow', () => {
expect(() => thrower.dive()).to.throw(errorToThrow);

expect(spy).to.have.property('callCount', 0);

expect(wrapper.find({ children: 'HasThrown' })).to.have.lengthOf(0);
expect(wrapper.find({ children: 'HasNotThrown' })).to.have.lengthOf(1);
});
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ class ReactWrapper {
const nodeHierarchy = [thisNode].concat(nodeParents(this, thisNode));
renderer.simulateError(nodeHierarchy, rootNode, error);

this[ROOT].update();
return this;
});
}
Expand Down

0 comments on commit f281fef

Please sign in to comment.