From ae3b98f5ade8ec6564780f2ef9bae70ff5fea756 Mon Sep 17 00:00:00 2001 From: Sunil Pai Date: Fri, 10 May 2019 12:13:14 +0100 Subject: [PATCH] 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. --- src/__tests__/profiling-test.js | 16 ++--- src/__tests__/profilingCharts-test.js | 66 +++++++++---------- .../profilingCommitTreeBuilder-test.js | 22 +++---- src/__tests__/utils.js | 22 ++----- 4 files changed, 54 insertions(+), 72 deletions(-) diff --git a/src/__tests__/profiling-test.js b/src/__tests__/profiling-test.js index 8631c9c08a64..26d2e2e67856 100644 --- a/src/__tests__/profiling-test.js +++ b/src/__tests__/profiling-test.js @@ -129,7 +129,7 @@ describe('profiling', () => { const rendererID = utils.getRendererID(); const rootID = store.roots[0]; - await utils.actSuspense(() => + await utils.actAsync(() => TestRenderer.create( { exportImportHelper(rendererID, rootID); - await utils.actSuspense(() => + await utils.actAsync(() => TestRenderer.create( { const rootID = store.roots[0]; for (let commitIndex = 0; commitIndex < 4; commitIndex++) { - await utils.actSuspense(() => { + await utils.actAsync(() => { TestRenderer.create( { exportImportHelper(rendererID, rootID); for (let commitIndex = 0; commitIndex < 4; commitIndex++) { - await utils.actSuspense(() => { + await utils.actAsync(() => { TestRenderer.create( { const rootID = store.roots[0]; for (let index = 0; index < store.numElements; index++) { - await utils.actSuspense(() => { + await utils.actAsync(() => { const fiberID = store.getElementIDAtIndex(index); if (fiberID == null) { throw Error(`Unexpected null ID for element at index ${index}`); @@ -334,7 +334,7 @@ describe('profiling', () => { exportImportHelper(rendererID, rootID); for (let index = 0; index < store.numElements; index++) { - await utils.actSuspense(() => { + await utils.actAsync(() => { const fiberID = store.getElementIDAtIndex(index); if (fiberID == null) { throw Error(`Unexpected null ID for element at index ${index}`); @@ -413,7 +413,7 @@ describe('profiling', () => { const rendererID = utils.getRendererID(); const rootID = store.roots[0]; - await utils.actSuspense(() => + await utils.actAsync(() => TestRenderer.create( { exportImportHelper(rendererID, rootID); - await utils.actSuspense(() => + await utils.actAsync(() => TestRenderer.create( { for (let commitIndex = 0; commitIndex < 2; commitIndex++) { suspenseResolved = false; - await utils.actSuspense( - () => - TestRenderer.create( - - - - ), - 3 + await utils.actAsync(() => + TestRenderer.create( + + + + ) ); expect(suspenseResolved).toBe(true); @@ -189,18 +187,16 @@ describe('profiling charts', () => { for (let commitIndex = 0; commitIndex < 2; commitIndex++) { suspenseResolved = false; - await utils.actSuspense( - () => - TestRenderer.create( - - - - ), - 3 + await utils.actAsync(() => + TestRenderer.create( + + + + ) ); expect(suspenseResolved).toBe(true); @@ -272,18 +268,16 @@ describe('profiling charts', () => { for (let commitIndex = 0; commitIndex < 2; commitIndex++) { suspenseResolved = false; - await utils.actSuspense( - () => - TestRenderer.create( - - - - ), - 3 + await utils.actAsync(() => + TestRenderer.create( + + + + ) ); expect(suspenseResolved).toBe(true); diff --git a/src/__tests__/profilingCommitTreeBuilder-test.js b/src/__tests__/profilingCommitTreeBuilder-test.js index 1d037e778f53..f44073099dfc 100644 --- a/src/__tests__/profilingCommitTreeBuilder-test.js +++ b/src/__tests__/profilingCommitTreeBuilder-test.js @@ -67,18 +67,16 @@ describe('commit tree', () => { for (let commitIndex = 0; commitIndex < 4; commitIndex++) { suspenseResolved = false; - await utils.actSuspense( - () => - TestRenderer.create( - - - - ), - 3 + await utils.actAsync(() => + TestRenderer.create( + + + + ) ); expect(suspenseResolved).toBe(true); diff --git a/src/__tests__/utils.js b/src/__tests__/utils.js index 89c7ffd6b130..e9aeb3f05d59 100644 --- a/src/__tests__/utils.js +++ b/src/__tests__/utils.js @@ -9,32 +9,22 @@ export function act(callback: Function): void { }); // Flush Bridge operations - jest.runAllTimers(); + TestUtils.act(() => { + jest.runAllTimers(); + }); } -export async function actSuspense( - callback: Function, - numTimesToFlush: number = 1 -): Promise { +export async function actAsync(cb: () => *) : Promise { const TestUtils = require('react-dom/test-utils'); - const Scheduler = require('scheduler'); // $FlowFixMe Flow doens't know about "await act()" yet await TestUtils.act(async () => { - callback(); - - // Resolve pending suspense promises - jest.runAllTimers(); + await cb(); }); - - // Run cascading microtasks and flush scheduled React work. - // Components that suspend multiple times will need to do this once per suspend operation. - // HACK Ideally the mock scheduler would provide an API to ask if there was outstanding work. - while (--numTimesToFlush >= 0) { + while (jest.getTimerCount() > 0) { // $FlowFixMe Flow doens't know about "await act()" yet await TestUtils.act(async () => { jest.runAllTimers(); - Scheduler.flushAll(); }); } }