Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DO NOT MERGE - rewrite test act helpers based on react/#15591 #266

Merged
merged 6 commits into from May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 59 additions & 57 deletions src/__tests__/inspectedElementContext-test.js
Expand Up @@ -8,6 +8,7 @@ import type Store from 'src/devtools/store';
describe('InspectedElementContext', () => {
let React;
let ReactDOM;
let act;
let TestRenderer: ReactTestRenderer;
let bridge: Bridge;
let store: Store;
Expand All @@ -19,6 +20,18 @@ describe('InspectedElementContext', () => {
let StoreContext;
let TreeContextController;

// a version of actAsync that *doesn't* recursively flush timers
async function actAsync(cb: () => *): Promise<void> {
// $FlowFixMe Flow doesn't know about "await act()" yet
await act(async () => {
await cb();
});
// $FlowFixMe Flow doesn't know about "await act()" yet
await act(async () => {
jest.runOnlyPendingTimers();
});
}

beforeEach(() => {
utils = require('./utils');
utils.beforeEachProfiling();
Expand All @@ -29,6 +42,7 @@ describe('InspectedElementContext', () => {

React = require('react');
ReactDOM = require('react-dom');
act = require('react-dom/test-utils').act;
TestRenderer = utils.requireTestRenderer();

BridgeContext = require('src/devtools/views/context').BridgeContext;
Expand Down Expand Up @@ -84,19 +98,17 @@ describe('InspectedElementContext', () => {
return null;
}

await utils.actAsync(
() =>
TestRenderer.create(
<Contexts
defaultSelectedElementID={example.id}
defaultSelectedElementIndex={0}
>
<React.Suspense fallback={null}>
<Suspender target={example} />
</React.Suspense>
</Contexts>
),
3
await actAsync(() =>
TestRenderer.create(
<Contexts
defaultSelectedElementID={example.id}
defaultSelectedElementIndex={0}
>
<React.Suspense fallback={null}>
<Suspender target={example} />
</React.Suspense>
</Contexts>
)
);
expect(didFinish).toBe(true);

Expand All @@ -120,40 +132,36 @@ describe('InspectedElementContext', () => {
return null;
}

await utils.actAsync(
() =>
TestRenderer.create(
<Contexts
defaultSelectedElementID={example.id}
defaultSelectedElementIndex={0}
>
<React.Suspense fallback={null}>
<Suspender target={example} />
</React.Suspense>
</Contexts>
),
3
await actAsync(() =>
TestRenderer.create(
<Contexts
defaultSelectedElementID={example.id}
defaultSelectedElementIndex={0}
>
<React.Suspense fallback={null}>
<Suspender target={example} />
</React.Suspense>
</Contexts>
)
);
expect(inspectedElement).toMatchSnapshot('2: initial render');

await utils.actAsync(() =>
await actAsync(() =>
ReactDOM.render(<Example foo={2} bar="def" />, container)
);

inspectedElement = null;
await utils.actAsync(
() =>
TestRenderer.create(
<Contexts
defaultSelectedElementID={example.id}
defaultSelectedElementIndex={0}
>
<React.Suspense fallback={null}>
<Suspender target={example} />
</React.Suspense>
</Contexts>
),
1
await actAsync(() =>
TestRenderer.create(
<Contexts
defaultSelectedElementID={example.id}
defaultSelectedElementIndex={0}
>
<React.Suspense fallback={null}>
<Suspender target={example} />
</React.Suspense>
</Contexts>
)
);
expect(inspectedElement).toMatchSnapshot('2: updated state');

Expand Down Expand Up @@ -194,7 +202,7 @@ describe('InspectedElementContext', () => {
targetRenderCount = 0;

let renderer;
await utils.actAsync(
await actAsync(
() =>
(renderer = TestRenderer.create(
<Contexts
Expand All @@ -205,8 +213,7 @@ describe('InspectedElementContext', () => {
<Suspender target={id} />
</React.Suspense>
</Contexts>
)),
3
))
);
expect(targetRenderCount).toBe(1);
expect(inspectedElement).toMatchSnapshot('2: initial render');
Expand All @@ -215,26 +222,21 @@ describe('InspectedElementContext', () => {

targetRenderCount = 0;
inspectedElement = null;
await utils.actAsync(
() =>
renderer.update(
<Contexts
defaultSelectedElementID={id}
defaultSelectedElementIndex={1}
>
<React.Suspense fallback={null}>
<Suspender target={id} />
</React.Suspense>
</Contexts>
),
1
await actAsync(() =>
renderer.update(
<Contexts defaultSelectedElementID={id} defaultSelectedElementIndex={1}>
<React.Suspense fallback={null}>
<Suspender target={id} />
</React.Suspense>
</Contexts>
)
);
expect(targetRenderCount).toBe(0);
expect(inspectedElement).toEqual(initialInspectedElement);

targetRenderCount = 0;

await utils.actAsync(() =>
await actAsync(() =>
ReactDOM.render(
<Wrapper>
<Target foo={2} bar="def" />
Expand Down
72 changes: 32 additions & 40 deletions src/__tests__/ownersListContext-test.js
Expand Up @@ -84,30 +84,26 @@ describe('OwnersListContext', () => {
return null;
}

await utils.actAsync(
() =>
TestRenderer.create(
<Contexts defaultOwnerID={parent.id}>
<React.Suspense fallback={null}>
<Suspender owner={parent} />
</React.Suspense>
</Contexts>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<Contexts defaultOwnerID={parent.id}>
<React.Suspense fallback={null}>
<Suspender owner={parent} />
</React.Suspense>
</Contexts>
)
);
expect(didFinish).toBe(true);

didFinish = false;
await utils.actAsync(
() =>
TestRenderer.create(
<Contexts defaultOwnerID={firstChild.id}>
<React.Suspense fallback={null}>
<Suspender owner={firstChild} />
</React.Suspense>
</Contexts>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<Contexts defaultOwnerID={firstChild.id}>
<React.Suspense fallback={null}>
<Suspender owner={firstChild} />
</React.Suspense>
</Contexts>
)
);
expect(didFinish).toBe(true);

Expand Down Expand Up @@ -148,16 +144,14 @@ describe('OwnersListContext', () => {
return null;
}

await utils.actAsync(
() =>
TestRenderer.create(
<Contexts defaultOwnerID={firstChild.id}>
<React.Suspense fallback={null}>
<Suspender owner={firstChild} />
</React.Suspense>
</Contexts>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<Contexts defaultOwnerID={firstChild.id}>
<React.Suspense fallback={null}>
<Suspender owner={firstChild} />
</React.Suspense>
</Contexts>
)
);
expect(didFinish).toBe(true);

Expand Down Expand Up @@ -190,16 +184,14 @@ describe('OwnersListContext', () => {
return null;
}

await utils.actAsync(
() =>
TestRenderer.create(
<Contexts defaultOwnerID={grandparent.id}>
<React.Suspense fallback={null}>
<Suspender owner={grandparent} />
</React.Suspense>
</Contexts>
),
3
await utils.actAsync(() =>
TestRenderer.create(
<Contexts defaultOwnerID={grandparent.id}>
<React.Suspense fallback={null}>
<Suspender owner={grandparent} />
</React.Suspense>
</Contexts>
)
);
expect(didFinish).toBe(true);

Expand Down
3 changes: 0 additions & 3 deletions src/__tests__/profilingCharts-test.js
Expand Up @@ -88,7 +88,6 @@ describe('profiling charts', () => {

for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
renderFinished = false;

utils.act(() => {
TestRenderer.create(
<Validator commitIndex={commitIndex} rootID={rootID} />
Expand Down Expand Up @@ -162,7 +161,6 @@ describe('profiling charts', () => {

for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
renderFinished = false;

utils.act(() => {
TestRenderer.create(
<Validator commitIndex={commitIndex} rootID={rootID} />
Expand Down Expand Up @@ -227,7 +225,6 @@ describe('profiling charts', () => {

for (let commitIndex = 0; commitIndex < 2; commitIndex++) {
renderFinished = false;

utils.act(() => {
TestRenderer.create(
<Validator commitIndex={commitIndex} rootID={rootID} />
Expand Down
24 changes: 7 additions & 17 deletions src/__tests__/utils.js
Expand Up @@ -14,32 +14,22 @@ export function act(callback: Function): void {
});

// Flush Bridge operations
jest.runAllTimers();
TestUtils.act(() => {
jest.runAllTimers();
});
}

export async function actAsync(
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.runOnlyPendingTimers();
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.runOnlyPendingTimers();
Scheduler.flushAll();
jest.runAllTimers();
});
}
}
Expand Down