@samitouri / QOS-React-2 / commits / ae3b98f5ad

rewrite test act helpers based on react/#15591

This simplifies your test helpers to loop until all timers are flushed (including the ones that get queued after updates), and works in concurrent mode. I also renamed actSuspense to actAsync to be clearer.

Sunil Pai committed May 10, 2019 at 12:13 UTC ae3b98f5ade8ec6564780f2ef9bae70ff5fea756
4 files changed +54 -72
src/__tests__/profiling-test.js
+8 -8
@@ -129,7 +129,7 @@ describe('profiling', () => {
129 const rendererID = utils.getRendererID();
130 const rootID = store.roots[0];
131
132 - await utils.actSuspense(() =>
132 + await utils.actAsync(() =>
133 TestRenderer.create(
134 <React.Suspense fallback={null}>
135 <Suspender
@@ -145,7 +145,7 @@ describe('profiling', () => {
145
146 exportImportHelper(rendererID, rootID);
147
148 - await utils.actSuspense(() =>
148 + await utils.actAsync(() =>
149 TestRenderer.create(
150 <React.Suspense fallback={null}>
151 <Suspender
@@ -218,7 +218,7 @@ describe('profiling', () => {
218 const rootID = store.roots[0];
219
220 for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
221 - await utils.actSuspense(() => {
221 + await utils.actAsync(() => {
222 TestRenderer.create(
223 <React.Suspense fallback={null}>
224 <Suspender
@@ -237,7 +237,7 @@ describe('profiling', () => {
237 exportImportHelper(rendererID, rootID);
238
239 for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
240 - await utils.actSuspense(() => {
240 + await utils.actAsync(() => {
241 TestRenderer.create(
242 <React.Suspense fallback={null}>
243 <Suspender
@@ -311,7 +311,7 @@ describe('profiling', () => {
311 const rootID = store.roots[0];
312
313 for (let index = 0; index < store.numElements; index++) {
314 - await utils.actSuspense(() => {
314 + await utils.actAsync(() => {
315 const fiberID = store.getElementIDAtIndex(index);
316 if (fiberID == null) {
317 throw Error(`Unexpected null ID for element at index ${index}`);
@@ -334,7 +334,7 @@ describe('profiling', () => {
334 exportImportHelper(rendererID, rootID);
335
336 for (let index = 0; index < store.numElements; index++) {
337 - await utils.actSuspense(() => {
337 + await utils.actAsync(() => {
338 const fiberID = store.getElementIDAtIndex(index);
339 if (fiberID == null) {
340 throw Error(`Unexpected null ID for element at index ${index}`);
@@ -413,7 +413,7 @@ describe('profiling', () => {
413 const rendererID = utils.getRendererID();
414 const rootID = store.roots[0];
415
416 - await utils.actSuspense(() =>
416 + await utils.actAsync(() =>
417 TestRenderer.create(
418 <React.Suspense fallback={null}>
419 <Suspender
@@ -429,7 +429,7 @@ describe('profiling', () => {
429
430 exportImportHelper(rendererID, rootID);
431
432 - await utils.actSuspense(() =>
432 + await utils.actAsync(() =>
433 TestRenderer.create(
434 <React.Suspense fallback={null}>
435 <Suspender
src/__tests__/profilingCharts-test.js
+30 -36
@@ -97,18 +97,16 @@ describe('profiling charts', () => {
97 for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
98 suspenseResolved = false;
99
100 - await utils.actSuspense(
101 - () =>
102 - TestRenderer.create(
103 - <React.Suspense fallback={null}>
104 - <Suspender
105 - commitIndex={commitIndex}
106 - rendererID={rendererID}
107 - rootID={rootID}
108 - />
109 - </React.Suspense>
110 - ),
111 - 3
100 + await utils.actAsync(() =>
101 + TestRenderer.create(
102 + <React.Suspense fallback={null}>
103 + <Suspender
104 + commitIndex={commitIndex}
105 + rendererID={rendererID}
106 + rootID={rootID}
107 + />
108 + </React.Suspense>
109 + )
110 );
111
112 expect(suspenseResolved).toBe(true);
@@ -189,18 +187,16 @@ describe('profiling charts', () => {
187 for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
188 suspenseResolved = false;
189
192 - await utils.actSuspense(
193 - () =>
194 - TestRenderer.create(
195 - <React.Suspense fallback={null}>
196 - <Suspender
197 - commitIndex={commitIndex}
198 - rendererID={rendererID}
199 - rootID={rootID}
200 - />
201 - </React.Suspense>
202 - ),
203 - 3
190 + await utils.actAsync(() =>
191 + TestRenderer.create(
192 + <React.Suspense fallback={null}>
193 + <Suspender
194 + commitIndex={commitIndex}
195 + rendererID={rendererID}
196 + rootID={rootID}
197 + />
198 + </React.Suspense>
199 + )
200 );
201
202 expect(suspenseResolved).toBe(true);
@@ -272,18 +268,16 @@ describe('profiling charts', () => {
268 for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
269 suspenseResolved = false;
270
275 - await utils.actSuspense(
276 - () =>
277 - TestRenderer.create(
278 - <React.Suspense fallback={null}>
279 - <Suspender
280 - commitIndex={commitIndex}
281 - rendererID={rendererID}
282 - rootID={rootID}
283 - />
284 - </React.Suspense>
285 - ),
286 - 3
271 + await utils.actAsync(() =>
272 + TestRenderer.create(
273 + <React.Suspense fallback={null}>
274 + <Suspender
275 + commitIndex={commitIndex}
276 + rendererID={rendererID}
277 + rootID={rootID}
278 + />
279 + </React.Suspense>
280 + )
281 );
282
283 expect(suspenseResolved).toBe(true);
src/__tests__/profilingCommitTreeBuilder-test.js
+10 -12
@@ -67,18 +67,16 @@ describe('commit tree', () => {
67 for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
68 suspenseResolved = false;
69
70 - await utils.actSuspense(
71 - () =>
72 - TestRenderer.create(
73 - <React.Suspense fallback={null}>
74 - <Suspender
75 - commitIndex={commitIndex}
76 - rendererID={rendererID}
77 - rootID={rootID}
78 - />
79 - </React.Suspense>
80 - ),
81 - 3
70 + await utils.actAsync(() =>
71 + TestRenderer.create(
72 + <React.Suspense fallback={null}>
73 + <Suspender
74 + commitIndex={commitIndex}
75 + rendererID={rendererID}
76 + rootID={rootID}
77 + />
78 + </React.Suspense>
79 + )
80 );
81
82 expect(suspenseResolved).toBe(true);
src/__tests__/utils.js
+6 -16
@@ -9,32 +9,22 @@ export function act(callback: Function): void {
9 });
10
11 // Flush Bridge operations
12 - jest.runAllTimers();
12 + TestUtils.act(() => {
13 + jest.runAllTimers();
14 + });
15 }
16
15 -export async function actSuspense(
16 - callback: Function,
17 - numTimesToFlush: number = 1
18 -): Promise<void> {
17 +export async function actAsync(cb: () => *) : Promise<void> {
18 const TestUtils = require('react-dom/test-utils');
20 - const Scheduler = require('scheduler');
19
20 // $FlowFixMe Flow doens't know about "await act()" yet
21 await TestUtils.act(async () => {
24 - callback();
25 -
26 - // Resolve pending suspense promises
27 - jest.runAllTimers();
22 + await cb();
23 });
29 -
30 - // Run cascading microtasks and flush scheduled React work.
31 - // Components that suspend multiple times will need to do this once per suspend operation.
32 - // HACK Ideally the mock scheduler would provide an API to ask if there was outstanding work.
33 - while (--numTimesToFlush >= 0) {
24 + while (jest.getTimerCount() > 0) {
25 // $FlowFixMe Flow doens't know about "await act()" yet
26 await TestUtils.act(async () => {
27 jest.runAllTimers();
37 - Scheduler.flushAll();
28 });
29 }
30 }