Skip to content

Commit

Permalink
Add test to ensure manual retain and useReadQuery work correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 11, 2023
1 parent e755d82 commit 19e1056
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/react/query-preloader/__tests__/createQueryPreloader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,30 @@ test("useReadQuery auto-retains the queryRef and disposes of it when unmounted",
expect(client).not.toHaveSuspenseCacheEntryUsing(query);
});

test.todo(
"queryRef is not disposed when useReadQuery unmounts when manually retained"
);
test("unmounting useReadQuery does not auto dispose of the queryRef when manually retained", async () => {
const { query, mocks } = useSimpleCase();

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

const queryRef = preloadQuery(query);
const dispose = queryRef.retain();

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

await act(() => queryRef.toPromise());

const internalQueryRef = unwrapQueryRef(queryRef);

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

unmount();
await wait(0);

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

dispose();
});

test("useReadQuery warns when called with a disposed queryRef", async () => {
using _consoleSpy = spyOnConsole("warn");
Expand Down

0 comments on commit 19e1056

Please sign in to comment.