Skip to content

Commit

Permalink
Adds test for facebook#15732.
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer committed May 27, 2019
1 parent a97b5c0 commit 3980611
Showing 1 changed file with 49 additions and 0 deletions.
Expand Up @@ -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 (
<Wrapper>
{this.state.hasError ? 'Error!' : this.props.children}
</Wrapper>
);
}
}

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(
<ErrorBoundary>
<Thrower />
</ErrorBoundary>,
);

expect(root).toMatchRenderedOutput('Throw!');
act(() => setShouldThrow(true));
expect(root).toMatchRenderedOutput('Error!');
});
});

0 comments on commit 3980611

Please sign in to comment.