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.
This commit is contained in:
Sunil Pai
2019-05-10 12:13:14 +01:00
parent 17516b76ae
commit ae3b98f5ad
4 changed files with 54 additions and 72 deletions
+8 -8
View File
@@ -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
@@ -145,7 +145,7 @@ describe('profiling', () => {
exportImportHelper(rendererID, rootID);
await utils.actSuspense(() =>
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
@@ -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
@@ -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
@@ -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}`);
@@ -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(
<React.Suspense fallback={null}>
<Suspender
@@ -429,7 +429,7 @@ describe('profiling', () => {
exportImportHelper(rendererID, rootID);
await utils.actSuspense(() =>
await utils.actAsync(() =>
TestRenderer.create(
<React.Suspense fallback={null}>
<Suspender
+30 -36
View File
@@ -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);
@@ -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);
@@ -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);
@@ -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);
+6 -16
View File
@@ -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();
});
}
}