From 401065fe5c822d39a80f2ce4b2821596bdb5f99a Mon Sep 17 00:00:00 2001 From: Caleb Meredith Date: Tue, 28 May 2019 06:25:22 -0700 Subject: [PATCH] Adds test for #15732. (#15747) --- .../src/__tests__/ReactHooks-test.internal.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js b/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js index 447103d85e..74152cbcc6 100644 --- a/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js +++ b/packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js @@ -1869,4 +1869,53 @@ describe('ReactHooks', () => { Scheduler.flushAll(); expect(root).toMatchRenderedOutput('hello'); }); + + // Regression test for https://github.com/facebook/react/issues/15732 + it('resets hooks when an error is thrown in the middle of a list of hooks', async () => { + const {useEffect, useState} = React; + + class ErrorBoundary extends React.Component { + state = {hasError: false}; + + static getDerivedStateFromError() { + return {hasError: true}; + } + + render() { + return ( + + {this.state.hasError ? 'Error!' : this.props.children} + + ); + } + } + + function Wrapper({children}) { + return children; + } + + let setShouldThrow; + function Thrower() { + const [shouldThrow, _setShouldThrow] = useState(false); + setShouldThrow = _setShouldThrow; + + if (shouldThrow) { + throw new Error('Throw!'); + } + + useEffect(() => {}, []); + + return 'Throw!'; + } + + const root = ReactTestRenderer.create( + + + , + ); + + expect(root).toMatchRenderedOutput('Throw!'); + act(() => setShouldThrow(true)); + expect(root).toMatchRenderedOutput('Error!'); + }); });