From f339a407577d3d5f13cdaf9425608206e378e0cb Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 7 Nov 2023 19:03:57 -0700 Subject: [PATCH] Get test for cache-and-network updated to work correctly --- .../__tests__/useInteractiveQuery.test.tsx | 118 ++++++++++++++---- 1 file changed, 91 insertions(+), 27 deletions(-) diff --git a/src/react/hooks/__tests__/useInteractiveQuery.test.tsx b/src/react/hooks/__tests__/useInteractiveQuery.test.tsx index 89ba4be2be1..44a32a541ee 100644 --- a/src/react/hooks/__tests__/useInteractiveQuery.test.tsx +++ b/src/react/hooks/__tests__/useInteractiveQuery.test.tsx @@ -1144,45 +1144,109 @@ it("can disable canonical results when the cache's canonizeResults setting is tr await expect(ProfiledApp).not.toRerender(); }); -// TODO(FIXME): test fails, should return cache data first if it exists -it.skip("returns initial cache data followed by network data when the fetch policy is `cache-and-network`", async () => { - const query = gql` - { +it("returns initial cache data followed by network data when the fetch policy is `cache-and-network`", async () => { + const user = userEvent.setup(); + const query: TypedDocumentNode<{ hello: string }, never> = gql` + query { hello } `; const cache = new InMemoryCache(); - const link = mockSingleLink({ - request: { query }, - result: { data: { hello: "from link" } }, - delay: 20, - }); + const link = new MockLink([ + { + request: { query }, + result: { data: { hello: "from link" } }, + delay: 20, + }, + ]); - const client = new ApolloClient({ - link, - cache, - }); + const client = new ApolloClient({ link, cache }); cache.writeQuery({ query, data: { hello: "from cache" } }); - const { result } = renderHook( - () => useInteractiveQuery(query, { fetchPolicy: "cache-and-network" }), - { - wrapper: ({ children }) => ( - {children} - ), - } - ); + function SuspenseFallback() { + ProfiledApp.updateSnapshot((snapshot) => ({ + ...snapshot, + suspenseCount: snapshot.suspenseCount + 1, + })); + + return

Loading

; + } + + function Parent() { + const [queryRef, loadQuery] = useInteractiveQuery(query, { + fetchPolicy: "cache-and-network", + }); + + return ( + <> + + }> + {queryRef && } + + + ); + } + + function Child({ + queryRef, + }: { + queryRef: QueryReference<{ hello: string }>; + }) { + const result = useReadQuery(queryRef); - const [queryRef] = result.current; + ProfiledApp.updateSnapshot((snapshot) => ({ ...snapshot, result })); - const _result = await queryRef[QUERY_REFERENCE_SYMBOL].promise; + return null; + } - expect(_result).toEqual({ - data: { hello: "from link" }, - loading: false, - networkStatus: 7, + const ProfiledApp = profile<{ + result: UseReadQueryResult | null; + suspenseCount: number; + }>({ + Component: () => ( + + + + ), + initialSnapshot: { + result: null, + suspenseCount: 0, + }, }); + + render(); + + { + const { snapshot } = await ProfiledApp.takeRender(); + expect(snapshot.result).toEqual(null); + } + + await act(() => user.click(screen.getByText("Load query"))); + + { + const { snapshot } = await ProfiledApp.takeRender(); + + expect(snapshot.suspenseCount).toBe(0); + expect(snapshot.result).toEqual({ + data: { hello: "from cache" }, + networkStatus: NetworkStatus.loading, + error: undefined, + }); + } + + { + const { snapshot } = await ProfiledApp.takeRender(); + + expect(snapshot.suspenseCount).toBe(0); + expect(snapshot.result).toEqual({ + data: { hello: "from link" }, + networkStatus: NetworkStatus.ready, + error: undefined, + }); + } + + await expect(ProfiledApp).not.toRerender(); }); it("all data is present in the cache, no network request is made", async () => {