From 19e105693d1f86eddc48f9a9fd22fc5f207e0bf3 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Mon, 11 Dec 2023 12:09:20 -0700 Subject: [PATCH] Add test to ensure manual retain and useReadQuery work correctly --- .../__tests__/createQueryPreloader.test.tsx | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx b/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx index f662f2d147b..d2798e744ee 100644 --- a/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx +++ b/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx @@ -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");