/
ReactInterleavedUpdates-test.js
191 lines (166 loc) · 5.3 KB
/
ReactInterleavedUpdates-test.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
let React;
let ReactNoop;
let Scheduler;
let startTransition;
let useState;
let useEffect;
let act;
describe('ReactInterleavedUpdates', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('jest-react').act;
startTransition = React.startTransition;
useState = React.useState;
useEffect = React.useEffect;
});
function Text({text}) {
Scheduler.unstable_yieldValue(text);
return text;
}
test('update during an interleaved event is not processed during the current render', async () => {
const updaters = [];
function Child() {
const [state, setState] = useState(0);
useEffect(() => {
updaters.push(setState);
}, []);
return <Text text={state} />;
}
function updateChildren(value) {
for (let i = 0; i < updaters.length; i++) {
const setState = updaters[i];
setState(value);
}
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(
<>
<Child />
<Child />
<Child />
</>,
);
});
expect(Scheduler).toHaveYielded([0, 0, 0]);
expect(root).toMatchRenderedOutput('000');
await act(async () => {
if (gate(flags => flags.enableSyncDefaultUpdates)) {
React.startTransition(() => {
updateChildren(1);
});
} else {
updateChildren(1);
}
// Partially render the children. Only the first one.
expect(Scheduler).toFlushAndYieldThrough([1]);
// In an interleaved event, schedule an update on each of the children.
// Including the two that haven't rendered yet.
if (gate(flags => flags.enableSyncDefaultUpdates)) {
React.startTransition(() => {
updateChildren(2);
});
} else {
updateChildren(2);
}
// We should continue rendering without including the interleaved updates.
expect(Scheduler).toFlushUntilNextPaint([1, 1]);
expect(root).toMatchRenderedOutput('111');
});
// The interleaved updates flush in a separate render.
expect(Scheduler).toHaveYielded([2, 2, 2]);
expect(root).toMatchRenderedOutput('222');
});
// @gate !enableSyncDefaultUpdates
test('low priority update during an interleaved event is not processed during the current render', async () => {
// Same as previous test, but the interleaved update is lower priority than
// the in-progress render.
const updaters = [];
function Child() {
const [state, setState] = useState(0);
useEffect(() => {
updaters.push(setState);
}, []);
return <Text text={state} />;
}
function updateChildren(value) {
for (let i = 0; i < updaters.length; i++) {
const setState = updaters[i];
setState(value);
}
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(
<>
<Child />
<Child />
<Child />
</>,
);
});
expect(Scheduler).toHaveYielded([0, 0, 0]);
expect(root).toMatchRenderedOutput('000');
await act(async () => {
updateChildren(1);
// Partially render the children. Only the first one.
expect(Scheduler).toFlushAndYieldThrough([1]);
// In an interleaved event, schedule an update on each of the children.
// Including the two that haven't rendered yet.
startTransition(() => {
updateChildren(2);
});
// We should continue rendering without including the interleaved updates.
expect(Scheduler).toFlushUntilNextPaint([1, 1]);
expect(root).toMatchRenderedOutput('111');
});
// The interleaved updates flush in a separate render.
expect(Scheduler).toHaveYielded([2, 2, 2]);
expect(root).toMatchRenderedOutput('222');
});
test('regression for #24350: does not add to main update queue until interleaved update queue has been cleared', async () => {
let setStep;
function App() {
const [step, _setState] = useState(0);
setStep = _setState;
return (
<>
<Text text={'A' + step} />
<Text text={'B' + step} />
<Text text={'C' + step} />
</>
);
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['A0', 'B0', 'C0']);
expect(root).toMatchRenderedOutput('A0B0C0');
await act(async () => {
// Start the render phase.
startTransition(() => {
setStep(1);
});
expect(Scheduler).toFlushAndYieldThrough(['A1', 'B1']);
// Schedule an interleaved update. This gets placed on a special queue.
startTransition(() => {
setStep(2);
});
// Finish rendering the first update.
expect(Scheduler).toFlushUntilNextPaint(['C1']);
// Schedule another update. (In the regression case, this was treated
// as a normal, non-interleaved update and it was inserted into the queue
// before the interleaved one was processed.)
startTransition(() => {
setStep(3);
});
});
// The last update should win.
expect(Scheduler).toHaveYielded(['A3', 'B3', 'C3']);
expect(root).toMatchRenderedOutput('A3B3C3');
});
});