From 85ed9369dc82e7dc58c69e5b2343c23126e82906 Mon Sep 17 00:00:00 2001 From: jgzuke Date: Sat, 8 Sep 2018 22:31:28 +0800 Subject: [PATCH 1/2] [Tests] failing tests for `.simulateError` and `.update` --- .../test/ReactWrapper-spec.jsx | 25 +++++++++++++++-- .../test/ShallowWrapper-spec.jsx | 28 +++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index fd4f8c5e9..d88bd7bed 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -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() { @@ -5049,6 +5055,9 @@ describeWithDOM('mount', () => { +
+ {this.state.didThrow ? 'HasThrown' : 'HasNotThrown'} +
@@ -5096,6 +5105,18 @@ describeWithDOM('mount', () => { }); }); + it('rerenders on a simulated error', () => { + const wrapper = mount(); + + 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(); diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 13ce6c5b1..ab4bf9c29 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -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() { @@ -5083,6 +5089,9 @@ describe('shallow', () => { +
+ {this.state.didThrow ? 'HasThrown' : 'HasNotThrown'} +
@@ -5124,6 +5133,18 @@ describe('shallow', () => { }); }); + it('rerenders on a simulated error', () => { + const wrapper = shallow(); + + 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(); @@ -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); }); }); }); From 54cb6a6198cf4d929a21ef43ad90cb380bcf6317 Mon Sep 17 00:00:00 2001 From: jgzuke Date: Sat, 8 Sep 2018 22:31:28 +0800 Subject: [PATCH 2/2] [Fix] `mount`: update after `simulateError` --- packages/enzyme/src/ReactWrapper.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index e3d577334..1ba5fa831 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -643,6 +643,7 @@ class ReactWrapper { const nodeHierarchy = [thisNode].concat(nodeParents(this, thisNode)); renderer.simulateError(nodeHierarchy, rootNode, error); + this[ROOT].update(); return this; }); }