-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Expand file tree
/
Copy pathActivityErrorHandling-test.js
More file actions
99 lines (89 loc) · 2.47 KB
/
ActivityErrorHandling-test.js
File metadata and controls
99 lines (89 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
let React;
let ReactNoop;
let Scheduler;
let act;
let Activity;
let useState;
let assertLog;
describe('Activity error handling', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('internal-test-utils').act;
Activity = React.Activity;
useState = React.useState;
const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
});
function Text({text}) {
Scheduler.log(text);
return text;
}
// @gate enableActivity
it(
'errors inside a hidden Activity do not escape in the visible part ' +
'of the UI',
async () => {
class ErrorBoundary extends React.Component {
state = {error: null};
static getDerivedStateFromError(error) {
return {error};
}
render() {
if (this.state.error) {
return (
<Text text={`Caught an error: ${this.state.error.message}`} />
);
}
return this.props.children;
}
}
function Throws() {
throw new Error('Oops!');
}
let setShowMore;
function App({content, more}) {
const [showMore, _setShowMore] = useState(false);
setShowMore = _setShowMore;
return (
<>
<div>{content}</div>
<div>
<ErrorBoundary>
<Activity mode={showMore ? 'visible' : 'hidden'}>
{more}
</Activity>
</ErrorBoundary>
</div>
</>
);
}
await act(() =>
ReactNoop.render(
<App content={<Text text="Visible" />} more={<Throws />} />,
),
);
// Initial render. An error is thrown when prerendering the hidden
// Activity boundary, but since it's hidden, the UI doesn't observe it.
assertLog(['Visible']);
expect(ReactNoop).toMatchRenderedOutput(
<>
<div>Visible</div>
<div />
</>,
);
// Once the Activity boundary is revealed, the error is thrown and
// captured by the outer ErrorBoundary.
await act(() => setShowMore(true));
assertLog(['Caught an error: Oops!', 'Caught an error: Oops!']);
expect(ReactNoop).toMatchRenderedOutput(
<>
<div>Visible</div>
<div>Caught an error: Oops!</div>
</>,
);
},
);
});