File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed
packages/react-dom/src/__tests__ Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments