32
Random = require('random-seed');
33
});
34
35
+ jest.setTimeout(20000);
36
+
37
function createFuzzer() {
38
const {useState, useContext, useLayoutEffect} = React;
39
59
};
60
const timeoutID = setTimeout(() => {
61
pendingTasks.delete(task);
60
- Scheduler.log(task.label);
62
setStep(i + 1);
63
}, remountAfter);
64
pendingTasks.add(task);
88
};
89
const timeoutID = setTimeout(() => {
90
pendingTasks.delete(task);
90
- Scheduler.log(task.label);
91
setStep([i + 1, suspendFor]);
92
}, beginAfter);
93
pendingTasks.add(task);
138
return resolvedText;
139
}
140
141
- async function resolveAllTasks() {
142
- Scheduler.unstable_flushAllWithoutAsserting();
143
- let elapsedTime = 0;
144
- while (pendingTasks && pendingTasks.size > 0) {
145
- if ((elapsedTime += 1000) > 1000000) {
146
- throw new Error('Something did not resolve properly.');
147
- }
148
- await act(() => {
149
- ReactNoop.batchedUpdates(() => {
150
- jest.advanceTimersByTime(1000);
151
- });
152
- });
153
- Scheduler.unstable_flushAllWithoutAsserting();
154
- }
155
- }
156
-
141
async function testResolvedOutput(unwrappedChildren) {
142
const children = (
143
<Suspense fallback="Loading...">{unwrappedChildren}</Suspense>
144
);
145
146
+ // Render the app multiple times: once without suspending (as if all the
147
+ // data was already preloaded), and then again with suspensey data.
148
resetCache();
149
const expectedRoot = ReactNoop.createRoot();
164
- expectedRoot.render(
165
- <ShouldSuspendContext.Provider value={false}>
166
- {children}
167
- </ShouldSuspendContext.Provider>,
168
- );
169
- await resolveAllTasks();
150
+ await act(() => {
151
+ expectedRoot.render(
152
+ <ShouldSuspendContext.Provider value={false}>
153
+ {children}
154
+ </ShouldSuspendContext.Provider>,
155
+ );
156
+ });
157
+
158
const expectedOutput = expectedRoot.getChildrenAsJSX();
159
160
resetCache();
173
- ReactNoop.renderLegacySyncRoot(children);
174
- await resolveAllTasks();
175
- const legacyOutput = ReactNoop.getChildrenAsJSX();
176
- expect(legacyOutput).toEqual(expectedOutput);
177
- ReactNoop.renderLegacySyncRoot(null);
161
+
162
+ const concurrentRootThatSuspends = ReactNoop.createRoot();
163
+ await act(() => {
164
+ concurrentRootThatSuspends.render(children);
165
+ });
166
+
167
+ resetCache();
168
+
169
+ // Do it again in legacy mode.
170
+ const legacyRootThatSuspends = ReactNoop.createLegacyRoot();
171
+ await act(() => {
172
+ legacyRootThatSuspends.render(children);
173
+ });
174
+
175
+ // Now compare the final output. It should be the same.
176
+ expect(concurrentRootThatSuspends.getChildrenAsJSX()).toEqual(
177
+ expectedOutput,
178
+ );
179
+ expect(legacyRootThatSuspends.getChildrenAsJSX()).toEqual(expectedOutput);
180
+
181
+ // TODO: There are Scheduler logs in this test file but they were only
182
+ // added for debugging purposes; we don't make any assertions on them.
183
+ // Should probably just delete.
184
+ Scheduler.unstable_clearLog();
185
}
186
187
function pickRandomWeighted(rand, options) {