Skip to content

Commit

Permalink
rewrite test act helpers based on react/facebook#15591
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sunil Pai committed May 10, 2019
1 parent 17516b7 commit ae3b98f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 72 deletions.
16 changes: 8 additions & 8 deletions src/__tests__/profiling-test.js
Expand Up @@ -129,7 +129,7 @@ describe('profiling', () => {
const rendererID = utils.getRendererID();
const rootID = store.roots[0];

await utils.actSuspense(() =>
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
Expand All @@ -145,7 +145,7 @@ describe('profiling', () => {

exportImportHelper(rendererID, rootID);

await utils.actSuspense(() =>
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
Expand Down Expand Up @@ -218,7 +218,7 @@ describe('profiling', () => {
const rootID = store.roots[0];

for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
await utils.actSuspense(() => {
await utils.actAsync(() => {
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
Expand All @@ -237,7 +237,7 @@ describe('profiling', () => {
exportImportHelper(rendererID, rootID);

for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
await utils.actSuspense(() => {
await utils.actAsync(() => {
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('profiling', () => {
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}`);
Expand All @@ -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}`);
Expand Down Expand Up @@ -413,7 +413,7 @@ describe('profiling', () => {
const rendererID = utils.getRendererID();
const rootID = store.roots[0];

await utils.actSuspense(() =>
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
Expand All @@ -429,7 +429,7 @@ describe('profiling', () => {

exportImportHelper(rendererID, rootID);

await utils.actSuspense(() =>
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
Expand Down
66 changes: 30 additions & 36 deletions src/__tests__/profilingCharts-test.js
Expand Up @@ -97,18 +97,16 @@ describe('profiling charts', () => {
for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
suspenseResolved = false;

await utils.actSuspense(
() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
)
);

expect(suspenseResolved).toBe(true);
Expand Down Expand Up @@ -189,18 +187,16 @@ describe('profiling charts', () => {
for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
suspenseResolved = false;

await utils.actSuspense(
() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
)
);

expect(suspenseResolved).toBe(true);
Expand Down Expand Up @@ -272,18 +268,16 @@ describe('profiling charts', () => {
for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
suspenseResolved = false;

await utils.actSuspense(
() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
)
);

expect(suspenseResolved).toBe(true);
Expand Down
22 changes: 10 additions & 12 deletions src/__tests__/profilingCommitTreeBuilder-test.js
Expand Up @@ -67,18 +67,16 @@ describe('commit tree', () => {
for (let commitIndex = 0; commitIndex < 4; commitIndex++) {
suspenseResolved = false;

await utils.actSuspense(
() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
commitIndex={commitIndex}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
)
);

expect(suspenseResolved).toBe(true);
Expand Down
22 changes: 6 additions & 16 deletions src/__tests__/utils.js
Expand Up @@ -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<void> {
export async function actAsync(cb: () => *) : Promise<void> {
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();
});
}
}
Expand Down

0 comments on commit ae3b98f

Please sign in to comment.