Skip to content

Commit

Permalink
[Tests] failing tests for .simulateError and .update
Browse files Browse the repository at this point in the history
  • Loading branch information
jgzuke authored and ljharb committed Sep 8, 2018
1 parent ca054b9 commit 3113da3
Show file tree
Hide file tree
Showing 2 changed files with 49 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

0 comments on commit 3113da3

Please sign in to comment.