Skip to content

Commit

Permalink
Adds test for facebook#15732. (facebook#15747)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer authored and gaearon committed May 28, 2019
1 parent 287ef30 commit 401065f
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
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 401065f

Please sign in to comment.