Skip to content

Commit

Permalink
Add test to ensure useReadQuery auto retains the query ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 11, 2023
1 parent 51df7c2 commit e755d82
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/react/query-preloader/__tests__/createQueryPreloader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
useVariablesCase,
} from "../../../testing/internal";
import { ApolloProvider } from "../../context";
import { render, renderHook } from "@testing-library/react";
import { act, render, renderHook } from "@testing-library/react";
import { UseReadQueryResult, useReadQuery } from "../../hooks";
import { GraphQLError } from "graphql";
import { ErrorBoundary } from "react-error-boundary";
Expand Down Expand Up @@ -229,9 +229,38 @@ test("Honors configured auto dispose timer on the client", async () => {
jest.useRealTimers();
});

test.todo(
"useReadQuery auto-retains the queryRef and disposes of it when unmounted"
);
test("useReadQuery auto-retains the queryRef and disposes of it when unmounted", async () => {
jest.useFakeTimers();
const { query, mocks } = useSimpleCase();

const client = createDefaultClient(mocks);
const preloadQuery = createQueryPreloader(client);

const queryRef = preloadQuery(query);

const { unmount } = renderHook(() => useReadQuery(queryRef));

// We don't start the dispose timer until the promise is initially resolved
// so we need to wait for it
jest.advanceTimersByTime(20);
await act(() => queryRef.toPromise());
jest.advanceTimersByTime(30_000);

const internalQueryRef = unwrapQueryRef(queryRef);

expect(internalQueryRef.disposed).toBe(false);

jest.useRealTimers();

unmount();

await wait(0);

expect(internalQueryRef.disposed).toBe(true);
expect(client.getObservableQueries().size).toBe(0);
expect(client).not.toHaveSuspenseCacheEntryUsing(query);
});

test.todo(
"queryRef is not disposed when useReadQuery unmounts when manually retained"
);
Expand Down

0 comments on commit e755d82

Please sign in to comment.