forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index7.html
371 lines (354 loc) · 11.2 KB
/
index7.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<script type='text/javascript' src="./react.js"></script>
<script type='text/javascript' src="./react-dom.js"></script>
<script type='text/javascript' src="./lib/ReactTestUtils.js"></script>
<script type='text/javascript' src="./lib/babel.js"></script>
</head>
<body>
<pre>React 15 </pre>
<div id='root' class="root">
</div>
<script type='text/babel'>
var container = document.getElementById('root');
var div = container;
if (!window.ReactDOM) {
window.ReactDOM = React;
}
var expect = function(a) {
return {
toBe: function(b) {
console.log(a, 'vs', b, a === b);
},
toEqual(b) {
console.log(a, 'vs', b, a + '' === b + '');
},
toThrow(){
try{
a()
}catch(e){
console.log(e,"catch")
}
}
};
};
var log = {
push(a){
console.log(a)
}
}
// var log = []
var BrokenRender = class extends React.Component {
constructor(props) {
super(props);
log.push('BrokenRender constructor');
}
render() {
log.push('BrokenRender render [!]');
throw new Error('Hello');
}
UNSAFE_componentWillMount() {
log.push('BrokenRender componentWillMount');
}
componentDidMount() {
log.push('BrokenRender componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenRender componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenRender componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenRender componentDidUpdate');
}
componentWillUnmount() {
log.push('BrokenRender componentWillUnmount');
}
};
var BrokenComponentWillMount = class extends React.Component {
constructor(props) {
super(props);
log.push('BrokenComponentWillMount constructor');
}
render() {
log.push('BrokenComponentWillMount render');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenComponentWillMount componentWillMount [!]');
throw new Error('Hello');
}
componentDidMount() {
log.push('BrokenComponentWillMount componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenComponentWillMount componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenComponentWillMount componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenComponentWillMount componentDidUpdate');
}
componentWillUnmount() {
log.push('BrokenComponentWillMount componentWillUnmount');
}
};
var ErrorMessage = class extends React.Component {
constructor(props) {
super(props);
log.push('ErrorMessage constructor');
}
UNSAFE_componentWillMount() {
log.push('ErrorMessage componentWillMount');
}
componentDidMount() {
log.push('ErrorMessage componentDidMount');
}
componentWillUnmount() {
log.push('ErrorMessage componentWillUnmount');
}
render() {
log.push('ErrorMessage render');
return <div>Caught an error: {this.props.message}.</div>;
}
};
var BrokenComponentWillUnmount = class extends React.Component {
static defaultProps = {
errorText: 'Hello',
};
constructor(props) {
super(props);
log.push('BrokenComponentWillUnmount constructor');
}
render() {
log.push('BrokenComponentWillUnmount render');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenComponentWillUnmount componentWillMount');
}
componentDidMount() {
log.push('BrokenComponentWillUnmount componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenComponentWillUnmount componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenComponentWillUnmount componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenComponentWillUnmount componentDidUpdate');
}
componentWillUnmount() {
log.push('BrokenComponentWillUnmount componentWillUnmount [!]');
console.log("unmount",this.props.errorText)
throw new Error(this.props.errorText);
}
};
var ErrorBoundary = class extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
log.push(`${this.props.logName} constructor`);
}
render() {
if (this.state.error && !this.props.forceRetry) {
log.push(`${this.props.logName} render error`);
return this.props.renderError(this.state.error, this.props);
}
log.push(`${this.props.logName} render success`);
return <div>{this.props.children}</div>;
}
componentDidCatch(error) {
log.push(`${this.props.logName} componentDidCatch`);
this.setState({ error });
}
UNSAFE_componentWillMount() {
log.push(`${this.props.logName} componentWillMount`);
}
componentDidMount() {
log.push(`${this.props.logName} componentDidMount`);
}
UNSAFE_componentWillReceiveProps() {
log.push(`${this.props.logName} componentWillReceiveProps`);
}
UNSAFE_componentWillUpdate() {
log.push(`${this.props.logName} componentWillUpdate`);
}
componentDidUpdate() {
log.push(`${this.props.logName} componentDidUpdate`);
}
componentWillUnmount() {
log.push(`${this.props.logName} componentWillUnmount`);
}
};
ErrorBoundary.defaultProps = {
logName: 'ErrorBoundary',
renderError(error, props) {
return <div ref={props.errorMessageRef}>Caught an error: {error.message}.</div>;
},
};
BrokenRenderErrorBoundary = class extends React.Component {
constructor(props) {
super(props);
this.state = { error: null };
log.push('BrokenRenderErrorBoundary constructor');
}
render() {
if (this.state.error) {
log.push('BrokenRenderErrorBoundary render error [!]');
throw new Error('Hello');
}
log.push('BrokenRenderErrorBoundary render success');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenRenderErrorBoundary componentWillMount');
}
componentDidMount() {
log.push('BrokenRenderErrorBoundary componentDidMount');
}
componentWillUnmount() {
log.push('BrokenRenderErrorBoundary componentWillUnmount');
}
componentDidCatch(error) {
log.push('BrokenRenderErrorBoundary componentDidCatch');
this.setState({ error });
}
};
var BrokenComponentDidUpdate = class extends React.Component {
static defaultProps = {
errorText: 'Hello',
};
constructor(props) {
super(props);
log.push('BrokenComponentDidUpdate constructor');
}
render() {
log.push('BrokenComponentDidUpdate render');
return <div>{this.props.children}</div>;
}
UNSAFE_componentWillMount() {
log.push('BrokenComponentDidUpdate componentWillMount');
}
componentDidMount() {
log.push('BrokenComponentDidUpdate componentDidMount');
}
UNSAFE_componentWillReceiveProps() {
log.push('BrokenComponentDidUpdate componentWillReceiveProps');
}
UNSAFE_componentWillUpdate() {
log.push('BrokenComponentDidUpdate componentWillUpdate');
}
componentDidUpdate() {
log.push('BrokenComponentDidUpdate componentDidUpdate [!]');
console.log("update",this.props.errorText)
throw new Error(this.props.errorText);
}
componentWillUnmount() {
log.push('BrokenComponentDidUpdate componentWillUnmount');
}
};
function renderUnmountError(error) {
return <div>Caught an unmounting error: {error.message}.</div>;
}
function renderUpdateError(error) {
return <div>Caught an updating error: {error.message}.</div>;
}
ReactDOM.render(
<ErrorBoundary logName="OuterErrorBoundary">
<ErrorBoundary
logName="InnerUnmountBoundary"
renderError={renderUnmountError}>
<BrokenComponentWillUnmount errorText="E1" />
<BrokenComponentWillUnmount errorText="E2" />
</ErrorBoundary>
<ErrorBoundary
logName="InnerUpdateBoundary"
renderError={renderUpdateError}>
<BrokenComponentDidUpdate errorText="E3" />
<BrokenComponentDidUpdate errorText="E4" />
</ErrorBoundary>
</ErrorBoundary>,
container,
);
log.length = 0;
ReactDOM.render(
<ErrorBoundary logName="OuterErrorBoundary">
<ErrorBoundary
logName="InnerUnmountBoundary"
renderError={renderUnmountError}
/>
<ErrorBoundary
logName="InnerUpdateBoundary"
renderError={renderUpdateError}>
<BrokenComponentDidUpdate errorText="E3" />
<BrokenComponentDidUpdate errorText="E4" />
</ErrorBoundary>
</ErrorBoundary>,
container,
);
expect(container.firstChild.textContent).toBe(
'Caught an unmounting error: E2.' + 'Caught an updating error: E4.',
);
expect(log).toEqual([
// Begin update phase
'OuterErrorBoundary componentWillReceiveProps',
'OuterErrorBoundary componentWillUpdate',
'OuterErrorBoundary render success',
'InnerUnmountBoundary componentWillReceiveProps',
'InnerUnmountBoundary componentWillUpdate',
'InnerUnmountBoundary render success',
'InnerUpdateBoundary componentWillReceiveProps',
'InnerUpdateBoundary componentWillUpdate',
'InnerUpdateBoundary render success',
// First come the updates
'BrokenComponentDidUpdate componentWillReceiveProps',
'BrokenComponentDidUpdate componentWillUpdate',
'BrokenComponentDidUpdate render',
'BrokenComponentDidUpdate componentWillReceiveProps',
'BrokenComponentDidUpdate componentWillUpdate',
'BrokenComponentDidUpdate render',
// We're in commit phase now, deleting
'BrokenComponentWillUnmount componentWillUnmount [!]',
'BrokenComponentWillUnmount componentWillUnmount [!]',
// Continue despite errors, handle them after commit is done
'InnerUnmountBoundary componentDidUpdate',
// We're still in commit phase, now calling update lifecycles
'BrokenComponentDidUpdate componentDidUpdate [!]',
// Again, continue despite errors, we'll handle them later
'BrokenComponentDidUpdate componentDidUpdate [!]',
'InnerUpdateBoundary componentDidUpdate',
'OuterErrorBoundary componentDidUpdate',
// After the commit phase, attempt to recover from any errors that
// were captured
'BrokenComponentDidUpdate componentWillUnmount',
'BrokenComponentDidUpdate componentWillUnmount',
'InnerUnmountBoundary componentDidCatch',
'InnerUnmountBoundary componentDidCatch',
'InnerUpdateBoundary componentDidCatch',
'InnerUpdateBoundary componentDidCatch',
'InnerUnmountBoundary componentWillUpdate',
'InnerUnmountBoundary render error',
'InnerUpdateBoundary componentWillUpdate',
'InnerUpdateBoundary render error',
'InnerUnmountBoundary componentDidUpdate',
'InnerUpdateBoundary componentDidUpdate',
]);
/*
log.length = 0;
ReactDOM.unmountComponentAtNode(container);
expect(log).toEqual([
'OuterErrorBoundary componentWillUnmount',
'InnerUnmountBoundary componentWillUnmount',
'InnerUpdateBoundary componentWillUnmount',
]);
*/
</script>
</html>