forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactIncrementalScheduling-test.internal.js
367 lines (322 loc) · 12.2 KB
/
ReactIncrementalScheduling-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
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
/**
* 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('ReactIncrementalScheduling', () => {
beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.warnAboutUnactedEffectsinDEV = false;
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
});
function span(prop) {
return {type: 'span', children: [], prop, hidden: false};
}
it('schedules and flushes deferred work', () => {
ReactNoop.render(<span prop="1" />);
expect(ReactNoop.getChildren()).toEqual([]);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop).toMatchRenderedOutput(<span prop="1" />);
});
it('searches for work on other roots once the current root completes', () => {
ReactNoop.renderToRootWithID(<span prop="a:1" />, 'a');
ReactNoop.renderToRootWithID(<span prop="b:1" />, 'b');
ReactNoop.renderToRootWithID(<span prop="c:1" />, 'c');
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop.getChildren('a')).toEqual([span('a:1')]);
expect(ReactNoop.getChildren('b')).toEqual([span('b:1')]);
expect(ReactNoop.getChildren('c')).toEqual([span('c:1')]);
});
it('schedules top-level updates in order of priority', () => {
// Initial render.
ReactNoop.render(<span prop={1} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop).toMatchRenderedOutput(<span prop={1} />);
ReactNoop.batchedUpdates(() => {
ReactNoop.render(<span prop={5} />);
ReactNoop.flushSync(() => {
ReactNoop.render(<span prop={2} />);
ReactNoop.render(<span prop={3} />);
ReactNoop.render(<span prop={4} />);
});
});
// The sync updates flush first.
expect(ReactNoop).toMatchRenderedOutput(<span prop={4} />);
// The terminal value should be the last update that was scheduled,
// regardless of priority. In this case, that's the last sync update.
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop).toMatchRenderedOutput(<span prop={4} />);
});
it('schedules top-level updates with same priority in order of insertion', () => {
// Initial render.
ReactNoop.render(<span prop={1} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop).toMatchRenderedOutput(<span prop={1} />);
ReactNoop.render(<span prop={2} />);
ReactNoop.render(<span prop={3} />);
ReactNoop.render(<span prop={4} />);
ReactNoop.render(<span prop={5} />);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop).toMatchRenderedOutput(<span prop={5} />);
});
it('works on deferred roots in the order they were scheduled', () => {
const {useEffect} = React;
function Text({text}) {
useEffect(
() => {
Scheduler.yieldValue(text);
},
[text],
);
return text;
}
ReactNoop.act(() => {
ReactNoop.renderToRootWithID(<Text text="a:1" />, 'a');
ReactNoop.renderToRootWithID(<Text text="b:1" />, 'b');
ReactNoop.renderToRootWithID(<Text text="c:1" />, 'c');
});
expect(Scheduler).toHaveYielded(['a:1', 'b:1', 'c:1']);
expect(ReactNoop.getChildrenAsJSX('a')).toEqual('a:1');
expect(ReactNoop.getChildrenAsJSX('b')).toEqual('b:1');
expect(ReactNoop.getChildrenAsJSX('c')).toEqual('c:1');
// Schedule deferred work in the reverse order
ReactNoop.batchedUpdates(() => {
ReactNoop.renderToRootWithID(<Text text="c:2" />, 'c');
ReactNoop.renderToRootWithID(<Text text="b:2" />, 'b');
});
// Ensure it starts in the order it was scheduled
expect(Scheduler).toFlushAndYieldThrough(['c:2']);
expect(ReactNoop.getChildrenAsJSX('a')).toEqual('a:1');
expect(ReactNoop.getChildrenAsJSX('b')).toEqual('b:1');
expect(ReactNoop.getChildrenAsJSX('c')).toEqual('c:2');
// Schedule last bit of work, it will get processed the last
ReactNoop.batchedUpdates(() => {
ReactNoop.renderToRootWithID(<Text text="a:2" />, 'a');
});
// Keep performing work in the order it was scheduled
expect(Scheduler).toFlushAndYieldThrough(['b:2']);
expect(ReactNoop.getChildrenAsJSX('a')).toEqual('a:1');
expect(ReactNoop.getChildrenAsJSX('b')).toEqual('b:2');
expect(ReactNoop.getChildrenAsJSX('c')).toEqual('c:2');
expect(Scheduler).toFlushAndYieldThrough(['a:2']);
expect(ReactNoop.getChildrenAsJSX('a')).toEqual('a:2');
expect(ReactNoop.getChildrenAsJSX('b')).toEqual('b:2');
expect(ReactNoop.getChildrenAsJSX('c')).toEqual('c:2');
});
it('schedules sync updates when inside componentDidMount/Update', () => {
let instance;
class Foo extends React.Component {
state = {tick: 0};
componentDidMount() {
Scheduler.yieldValue(
'componentDidMount (before setState): ' + this.state.tick,
);
this.setState({tick: 1});
// We're in a batch. Update hasn't flushed yet.
Scheduler.yieldValue(
'componentDidMount (after setState): ' + this.state.tick,
);
}
componentDidUpdate() {
Scheduler.yieldValue('componentDidUpdate: ' + this.state.tick);
if (this.state.tick === 2) {
Scheduler.yieldValue(
'componentDidUpdate (before setState): ' + this.state.tick,
);
this.setState({tick: 3});
Scheduler.yieldValue(
'componentDidUpdate (after setState): ' + this.state.tick,
);
// We're in a batch. Update hasn't flushed yet.
}
}
render() {
Scheduler.yieldValue('render: ' + this.state.tick);
instance = this;
return <span prop={this.state.tick} />;
}
}
ReactNoop.render(<Foo />);
// Render without committing
expect(Scheduler).toFlushAndYieldThrough(['render: 0']);
// Do one more unit of work to commit
expect(ReactNoop.flushNextYield()).toEqual([
'componentDidMount (before setState): 0',
'componentDidMount (after setState): 0',
// If the setState inside componentDidMount were deferred, there would be
// no more ops. Because it has Task priority, we get these ops, too:
'render: 1',
'componentDidUpdate: 1',
]);
instance.setState({tick: 2});
expect(Scheduler).toFlushAndYieldThrough(['render: 2']);
expect(ReactNoop.flushNextYield()).toEqual([
'componentDidUpdate: 2',
'componentDidUpdate (before setState): 2',
'componentDidUpdate (after setState): 2',
// If the setState inside componentDidUpdate were deferred, there would be
// no more ops. Because it has Task priority, we get these ops, too:
'render: 3',
'componentDidUpdate: 3',
]);
});
it('can opt-in to async scheduling inside componentDidMount/Update', () => {
let instance;
class Foo extends React.Component {
state = {tick: 0};
componentDidMount() {
ReactNoop.deferredUpdates(() => {
Scheduler.yieldValue(
'componentDidMount (before setState): ' + this.state.tick,
);
this.setState({tick: 1});
Scheduler.yieldValue(
'componentDidMount (after setState): ' + this.state.tick,
);
});
}
componentDidUpdate() {
ReactNoop.deferredUpdates(() => {
Scheduler.yieldValue('componentDidUpdate: ' + this.state.tick);
if (this.state.tick === 2) {
Scheduler.yieldValue(
'componentDidUpdate (before setState): ' + this.state.tick,
);
this.setState({tick: 3});
Scheduler.yieldValue(
'componentDidUpdate (after setState): ' + this.state.tick,
);
}
});
}
render() {
Scheduler.yieldValue('render: ' + this.state.tick);
instance = this;
return <span prop={this.state.tick} />;
}
}
ReactNoop.flushSync(() => {
ReactNoop.render(<Foo />);
});
// The cDM update should not have flushed yet because it has async priority.
expect(Scheduler).toHaveYielded([
'render: 0',
'componentDidMount (before setState): 0',
'componentDidMount (after setState): 0',
]);
expect(ReactNoop).toMatchRenderedOutput(<span prop={0} />);
// Now flush the cDM update.
expect(Scheduler).toFlushAndYield(['render: 1', 'componentDidUpdate: 1']);
expect(ReactNoop).toMatchRenderedOutput(<span prop={1} />);
// Increment the tick to 2. This will trigger an update inside cDU. Flush
// the first update without flushing the second one.
instance.setState({tick: 2});
expect(Scheduler).toFlushAndYieldThrough([
'render: 2',
'componentDidUpdate: 2',
'componentDidUpdate (before setState): 2',
'componentDidUpdate (after setState): 2',
]);
expect(ReactNoop).toMatchRenderedOutput(<span prop={2} />);
// Now flush the cDU update.
expect(Scheduler).toFlushAndYield(['render: 3', 'componentDidUpdate: 3']);
expect(ReactNoop).toMatchRenderedOutput(<span prop={3} />);
});
it('performs Task work even after time runs out', () => {
class Foo extends React.Component {
state = {step: 1};
componentDidMount() {
this.setState({step: 2}, () => {
this.setState({step: 3}, () => {
this.setState({step: 4}, () => {
this.setState({step: 5});
});
});
});
}
render() {
Scheduler.yieldValue('Foo');
return <span prop={this.state.step} />;
}
}
ReactNoop.render(<Foo />);
// This should be just enough to complete all the work, but not enough to
// commit it.
expect(Scheduler).toFlushAndYieldThrough(['Foo']);
expect(ReactNoop).toMatchRenderedOutput(null);
// Do one more unit of work.
ReactNoop.flushNextYield();
// The updates should all be flushed with Task priority
expect(ReactNoop).toMatchRenderedOutput(<span prop={5} />);
});
it('can opt-out of batching using unbatchedUpdates', () => {
ReactNoop.flushSync(() => {
ReactNoop.render(<span prop={0} />);
expect(ReactNoop.getChildren()).toEqual([]);
// Should not have flushed yet because we're still batching
// unbatchedUpdates reverses the effect of batchedUpdates, so sync
// updates are not batched
ReactNoop.unbatchedUpdates(() => {
ReactNoop.render(<span prop={1} />);
expect(ReactNoop).toMatchRenderedOutput(<span prop={1} />);
ReactNoop.render(<span prop={2} />);
expect(ReactNoop).toMatchRenderedOutput(<span prop={2} />);
});
ReactNoop.render(<span prop={3} />);
expect(ReactNoop).toMatchRenderedOutput(<span prop={2} />);
});
// Remaining update is now flushed
expect(ReactNoop).toMatchRenderedOutput(<span prop={3} />);
});
it('nested updates are always deferred, even inside unbatchedUpdates', () => {
let instance;
let ops = [];
class Foo extends React.Component {
state = {step: 0};
componentDidUpdate() {
ops.push('componentDidUpdate: ' + this.state.step);
if (this.state.step === 1) {
ReactNoop.unbatchedUpdates(() => {
// This is a nested state update, so it should not be
// flushed synchronously, even though we wrapped it
// in unbatchedUpdates.
this.setState({step: 2});
});
expect(ReactNoop).toMatchRenderedOutput(<span prop={1} />);
}
}
render() {
ops.push('render: ' + this.state.step);
instance = this;
return <span prop={this.state.step} />;
}
}
ReactNoop.render(<Foo />);
expect(Scheduler).toFlushWithoutYielding();
expect(ReactNoop).toMatchRenderedOutput(<span prop={0} />);
ReactNoop.flushSync(() => {
instance.setState({step: 1});
});
expect(ReactNoop).toMatchRenderedOutput(<span prop={2} />);
expect(ops).toEqual([
'render: 0',
'render: 1',
'componentDidUpdate: 1',
'render: 2',
'componentDidUpdate: 2',
]);
});
});