From d49341d478ec562be90c62c93ff344519d656075 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Fri, 8 Mar 2024 10:13:12 -0700 Subject: [PATCH] Add failing test for changing variables without consuming queryRef --- .../__tests__/useBackgroundQuery.test.tsx | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/react/hooks/__tests__/useBackgroundQuery.test.tsx b/src/react/hooks/__tests__/useBackgroundQuery.test.tsx index f9f934cb392..54ad243d5ba 100644 --- a/src/react/hooks/__tests__/useBackgroundQuery.test.tsx +++ b/src/react/hooks/__tests__/useBackgroundQuery.test.tsx @@ -480,6 +480,51 @@ it("disposes of the queryRef when unmounting before it is used by useReadQuery", expect(client).not.toHaveSuspenseCacheEntryUsing(query); }); +it("disposes of old queryRefs when changing variables before the queryRef is used by useReadQuery", async () => { + const { query, mocks } = setupVariablesCase(); + const client = new ApolloClient({ + link: new MockLink(mocks), + cache: new InMemoryCache(), + }); + + const Profiler = createDefaultProfiler(); + + function App({ id }: { id: string }) { + useTrackRenders(); + useBackgroundQuery(query, { variables: { id } }); + + return null; + } + + const { rerender } = renderWithClient(, { + client, + wrapper: Profiler, + }); + + expect(client.getObservableQueries().size).toBe(1); + expect(client).toHaveSuspenseCacheEntryUsing(query, { + variables: { id: "1" }, + }); + + { + const { renderedComponents } = await Profiler.takeRender(); + + expect(renderedComponents).toStrictEqual([App]); + } + + rerender(); + + await wait(0); + + expect(client.getObservableQueries().size).toBe(1); + expect(client).toHaveSuspenseCacheEntryUsing(query, { + variables: { id: "2" }, + }); + expect(client).not.toHaveSuspenseCacheEntryUsing(query, { + variables: { id: "1" }, + }); +}); + it("does not prematurely dispose of the queryRef when using strict mode", async () => { const { query, mocks } = setupSimpleCase(); const client = new ApolloClient({