Skip to content

Commit b093528

Browse files
authored
test: Add regression test for hooks after error boundaries (#20002)
* test: Add regression test for hooks after error boundaries * fix lint
1 parent f668b6c commit b093528

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
'use strict';
11+
12+
let React;
13+
let ReactDOM;
14+
15+
describe('ReactErrorBoundariesHooks', () => {
16+
beforeEach(() => {
17+
jest.resetModules();
18+
ReactDOM = require('react-dom');
19+
React = require('react');
20+
});
21+
22+
it('should preserve hook order if errors are caught', () => {
23+
function ErrorThrower() {
24+
React.useMemo(() => undefined, []);
25+
throw new Error('expected');
26+
}
27+
28+
function StatefulComponent() {
29+
React.useState(null);
30+
return ' | stateful';
31+
}
32+
33+
class ErrorHandler extends React.Component {
34+
state = {error: null};
35+
36+
componentDidCatch(error) {
37+
return this.setState({error});
38+
}
39+
40+
render() {
41+
if (this.state.error !== null) {
42+
return <p>Handled error: {this.state.error.message}</p>;
43+
}
44+
return this.props.children;
45+
}
46+
}
47+
48+
function App(props) {
49+
return (
50+
<React.Fragment>
51+
<ErrorHandler>
52+
<ErrorThrower />
53+
</ErrorHandler>
54+
<StatefulComponent />
55+
</React.Fragment>
56+
);
57+
}
58+
59+
const container = document.createElement('div');
60+
ReactDOM.render(<App />, container);
61+
62+
expect(() => {
63+
ReactDOM.render(<App />, container);
64+
}).not.toThrow();
65+
});
66+
});

0 commit comments

Comments
 (0)