Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add regression test for hooks after error boundaries #20002

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

let React;
let ReactDOM;

describe('ReactErrorBoundariesHooks', () => {
beforeEach(() => {
jest.resetModules();
ReactDOM = require('react-dom');
React = require('react');
});

it('should preserve hook order if errors are caught', () => {
function ErrorThrower() {
React.useMemo(() => undefined, []);
throw new Error('expected');
}

function StatefulComponent() {
React.useState(null);
return ' | stateful';
}

class ErrorHandler extends React.Component {
state = {error: null};

componentDidCatch(error) {
return this.setState({error});
}

render() {
if (this.state.error !== null) {
return <p>Handled error: {this.state.error.message}</p>;
}
return this.props.children;
}
}

function App(props) {
return (
<React.Fragment>
<ErrorHandler>
<ErrorThrower />
</ErrorHandler>
<StatefulComponent />
</React.Fragment>
);
}

const container = document.createElement('div');
ReactDOM.render(<App />, container);

expect(() => {
ReactDOM.render(<App />, container);
}).not.toThrow();
});
});