Skip to content

Commit

Permalink
inline actAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 29, 2024
1 parent ddcf14c commit cf5ebd7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/react/hooks/__tests__/useSuspenseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import React, { Fragment, StrictMode, Suspense, useTransition } from "react";
import {
act,
screen,
renderAsync,
waitFor,
RenderHookOptions,
renderHookAsync,
renderHook,
actAsync,
renderAsync,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { ErrorBoundary, FallbackProps } from "react-error-boundary";
Expand Down Expand Up @@ -59,6 +58,7 @@ import {
PaginatedCaseVariables,
setupPaginatedCase,
spyOnConsole,
actAsync,
} from "../../../testing/internal";

import {
Expand All @@ -71,6 +71,8 @@ import {

const IS_REACT_19 = React.version.startsWith("19");

afterEach(() => wait(10));

type RenderSuspenseHookOptions<Props, TSerializedCache = {}> = Omit<
RenderHookOptions<Props>,
"wrapper"
Expand Down Expand Up @@ -3687,7 +3689,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch().catch(() => {});
void result.current.refetch().catch(() => {});
});

await waitFor(() => expect(renders.errorCount).toBe(1));
Expand Down Expand Up @@ -4280,7 +4282,7 @@ describe("useSuspenseQuery", () => {

// TODO check: using actAsync instead of unawaited act changes observed render counts here.
await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});
await waitFor(() => {
expect(result.current).toMatchObject({
Expand Down Expand Up @@ -4444,7 +4446,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch({ id: "2" });
void result.current.refetch({ id: "2" });
});

await waitFor(() => {
Expand Down Expand Up @@ -4517,7 +4519,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand All @@ -4529,7 +4531,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down Expand Up @@ -4604,7 +4606,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch().catch(() => {});
void result.current.refetch().catch(() => {});
});

await waitFor(() => {
Expand Down Expand Up @@ -4669,7 +4671,7 @@ describe("useSuspenseQuery", () => {
});
});

await act(async () => {
await actAsync(async () => {
await result.current.refetch();
});

Expand Down Expand Up @@ -4738,7 +4740,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down Expand Up @@ -4814,7 +4816,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down Expand Up @@ -4969,7 +4971,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down Expand Up @@ -5011,7 +5013,7 @@ describe("useSuspenseQuery", () => {
});
});

act(() => {
await actAsync(() => {
result.current.fetchMore({
variables: { offset: 2 },
updateQuery: (prev, { fetchMoreResult }) => ({
Expand Down Expand Up @@ -5067,7 +5069,7 @@ describe("useSuspenseQuery", () => {
});
});

act(() => {
await actAsync(() => {
result.current.fetchMore({ variables: { offset: 2 } });
});

Expand Down Expand Up @@ -5152,8 +5154,8 @@ describe("useSuspenseQuery", () => {

expect(mergeParams).toEqual([[undefined, [2, 3, 5, 7, 11]]]);

act(() => {
result.current.refetch({ min: 12, max: 30 });
await actAsync(() => {
void result.current.refetch({ min: 12, max: 30 });
});

await waitFor(() => {
Expand Down Expand Up @@ -5229,8 +5231,8 @@ describe("useSuspenseQuery", () => {

expect(mergeParams).toEqual([[undefined, [2, 3, 5, 7, 11]]]);

act(() => {
result.current.refetch({ min: 12, max: 30 });
await actAsync(() => {
void result.current.refetch({ min: 12, max: 30 });
});

await waitFor(() => {
Expand Down Expand Up @@ -5305,8 +5307,8 @@ describe("useSuspenseQuery", () => {

expect(mergeParams).toEqual([[undefined, [2, 3, 5, 7, 11]]]);

act(() => {
result.current.refetch({ min: 12, max: 30 });
await actAsync(() => {
void result.current.refetch({ min: 12, max: 30 });
});

await waitFor(() => {
Expand Down Expand Up @@ -6109,7 +6111,7 @@ describe("useSuspenseQuery", () => {
await rerenderAsync({ errorPolicy: "all" });

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down Expand Up @@ -6182,7 +6184,7 @@ describe("useSuspenseQuery", () => {
await rerenderAsync({ context: { phase: "rerender" } });

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down Expand Up @@ -6356,7 +6358,7 @@ describe("useSuspenseQuery", () => {
expect(mergeParams).toEqual([[undefined, [2, 3, 5, 7, 11]]]);

await actAsync(async () => {
await result.current.refetch({ min: 12, max: 30 });
void result.current.refetch({ min: 12, max: 30 });
});

await waitFor(() => {
Expand All @@ -6378,7 +6380,7 @@ describe("useSuspenseQuery", () => {
await rerenderAsync({ refetchWritePolicy: "overwrite" });

await actAsync(async () => {
await result.current.refetch({ min: 30, max: 50 });
void result.current.refetch({ min: 30, max: 50 });
});

await waitFor(() => {
Expand Down Expand Up @@ -6596,7 +6598,7 @@ describe("useSuspenseQuery", () => {
const cacheKey = cache.identify({ __typename: "Character", id: "1" })!;

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down Expand Up @@ -6749,7 +6751,7 @@ describe("useSuspenseQuery", () => {
await rerenderAsync({ errorPolicy: "all", variables: { id: "1" } });

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

const expectedError = new ApolloError({
Expand Down Expand Up @@ -6826,7 +6828,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand All @@ -6838,7 +6840,7 @@ describe("useSuspenseQuery", () => {
});

await actAsync(async () => {
await result.current.refetch();
void result.current.refetch();
});

await waitFor(() => {
Expand Down
1 change: 1 addition & 0 deletions src/testing/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export {
createMockWrapper,
createClientWrapper,
} from "./renderHelpers.js";
export { actAsync } from "./rtl/actAsync.js";
12 changes: 12 additions & 0 deletions src/testing/internal/rtl/actAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from "react";

// This is a helper required for React 19 testing.
// There are currently multiple directions this could play out in RTL and none of
// them has been released yet, so we are inling this helper for now.
// See https://github.com/testing-library/react-testing-library/pull/1214
// and https://github.com/testing-library/react-testing-library/pull/1365
export function actAsync(scope: () => void | Promise<void>): Promise<void> {
return React.act(async () => {
await scope();
});
}

0 comments on commit cf5ebd7

Please sign in to comment.