-
Notifications
You must be signed in to change notification settings - Fork 45.5k
/
ReactSuspenseCallback-test.internal.js
305 lines (268 loc) · 8.3 KB
/
ReactSuspenseCallback-test.internal.js
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
/**
* 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
* @jest-environment node
*/
'use strict';
let React;
let ReactFeatureFlags;
let ReactNoop;
let Scheduler;
describe('ReactSuspense', () => {
beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableSuspenseCallback = true;
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
});
function text(t) {
return {text: t, hidden: false};
}
function createThenable() {
let completed = false;
let resolve;
const promise = new Promise(res => {
resolve = () => {
completed = true;
res();
};
});
const PromiseComp = () => {
if (!completed) {
throw promise;
}
return 'Done';
};
return {promise, resolve, PromiseComp};
}
it('check type', () => {
const {PromiseComp} = createThenable();
const elementBadType = (
<React.Suspense suspenseCallback={1} fallback={'Waiting'}>
<PromiseComp />
</React.Suspense>
);
ReactNoop.render(elementBadType);
expect(() => Scheduler.unstable_flushAll()).toErrorDev([
'Warning: Unexpected type for suspenseCallback.',
]);
const elementMissingCallback = (
<React.Suspense fallback={'Waiting'}>
<PromiseComp />
</React.Suspense>
);
ReactNoop.render(elementMissingCallback);
expect(() => Scheduler.unstable_flushAll()).toErrorDev([]);
});
it('1 then 0 suspense callback', async () => {
const {promise, resolve, PromiseComp} = createThenable();
let ops = [];
const suspenseCallback = thenables => {
ops.push(thenables);
};
const element = (
<React.Suspense suspenseCallback={suspenseCallback} fallback={'Waiting'}>
<PromiseComp />
</React.Suspense>
);
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Waiting')]);
expect(ops).toEqual([new Set([promise])]);
ops = [];
await resolve();
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Done')]);
expect(ops).toEqual([]);
});
it('2 then 1 then 0 suspense callback', async () => {
const {
promise: promise1,
resolve: resolve1,
PromiseComp: PromiseComp1,
} = createThenable();
const {
promise: promise2,
resolve: resolve2,
PromiseComp: PromiseComp2,
} = createThenable();
let ops = [];
const suspenseCallback1 = thenables => {
ops.push(thenables);
};
const element = (
<React.Suspense
suspenseCallback={suspenseCallback1}
fallback={'Waiting Tier 1'}>
<PromiseComp1 />
<PromiseComp2 />
</React.Suspense>
);
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Waiting Tier 1')]);
expect(ops).toEqual([new Set([promise1, promise2])]);
ops = [];
await resolve1();
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Waiting Tier 1')]);
expect(ops).toEqual([new Set([promise2])]);
ops = [];
await resolve2();
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Done'), text('Done')]);
expect(ops).toEqual([]);
});
it('nested suspense promises are reported only for their tier', () => {
const {promise, PromiseComp} = createThenable();
const ops1 = [];
const suspenseCallback1 = thenables => {
ops1.push(thenables);
};
const ops2 = [];
const suspenseCallback2 = thenables => {
ops2.push(thenables);
};
const element = (
<React.Suspense
suspenseCallback={suspenseCallback1}
fallback={'Waiting Tier 1'}>
<React.Suspense
suspenseCallback={suspenseCallback2}
fallback={'Waiting Tier 2'}>
<PromiseComp />
</React.Suspense>
</React.Suspense>
);
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Waiting Tier 2')]);
expect(ops1).toEqual([]);
expect(ops2).toEqual([new Set([promise])]);
});
it('competing suspense promises', async () => {
const {
promise: promise1,
resolve: resolve1,
PromiseComp: PromiseComp1,
} = createThenable();
const {
promise: promise2,
resolve: resolve2,
PromiseComp: PromiseComp2,
} = createThenable();
let ops1 = [];
const suspenseCallback1 = thenables => {
ops1.push(thenables);
};
let ops2 = [];
const suspenseCallback2 = thenables => {
ops2.push(thenables);
};
const element = (
<React.Suspense
suspenseCallback={suspenseCallback1}
fallback={'Waiting Tier 1'}>
<React.Suspense
suspenseCallback={suspenseCallback2}
fallback={'Waiting Tier 2'}>
<PromiseComp2 />
</React.Suspense>
<PromiseComp1 />
</React.Suspense>
);
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Waiting Tier 1')]);
expect(ops1).toEqual([new Set([promise1])]);
expect(ops2).toEqual([]);
ops1 = [];
ops2 = [];
await resolve1();
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([
text('Waiting Tier 2'),
text('Done'),
]);
expect(ops1).toEqual([]);
expect(ops2).toEqual([new Set([promise2])]);
ops1 = [];
ops2 = [];
await resolve2();
ReactNoop.render(element);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren()).toEqual([text('Done'), text('Done')]);
expect(ops1).toEqual([]);
expect(ops2).toEqual([]);
});
if (__DEV__) {
it('regression test for #16215 that relies on implementation details', async () => {
// Regression test for https://github.com/facebook/react/pull/16215.
// The bug only happens if there's an error earlier in the commit phase.
// The first error is the one that gets thrown, so to observe the later
// error, I've mocked the ReactErrorUtils module.
//
// If this test starts failing because the implementation details change,
// you can probably just delete it. It's not worth the hassle.
jest.resetModules();
const errors = [];
let hasCaughtError = false;
jest.mock('shared/ReactErrorUtils', () => ({
invokeGuardedCallback(name, fn, context, ...args) {
try {
return fn.call(context, ...args);
} catch (error) {
hasCaughtError = true;
errors.push(error);
}
},
hasCaughtError() {
return hasCaughtError;
},
clearCaughtError() {
hasCaughtError = false;
return errors[errors.length - 1];
},
}));
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableSuspenseCallback = true;
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
const {useEffect} = React;
const {PromiseComp} = createThenable();
function App() {
useEffect(() => {
Scheduler.unstable_yieldValue('Passive Effect');
});
return (
<React.Suspense
suspenseCallback={() => {
throw Error('Oops!');
}}
fallback="Loading...">
<PromiseComp />
</React.Suspense>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
expect(Scheduler).toFlushAndThrow('Oops!');
});
// Should have only received a single error. Before the bug fix, there was
// also a second error related to the Suspense update queue.
expect(errors.length).toBe(1);
expect(errors[0].message).toEqual('Oops!');
});
}
});