diff --git a/src/react/hooks/__tests__/useLazyQuery.test.tsx b/src/react/hooks/__tests__/useLazyQuery.test.tsx index 150ce125fca..2c7da2b080d 100644 --- a/src/react/hooks/__tests__/useLazyQuery.test.tsx +++ b/src/react/hooks/__tests__/useLazyQuery.test.tsx @@ -1483,6 +1483,78 @@ describe("useLazyQuery Hook", () => { expect(fetchCount).toBe(1); }); + // https://github.com/apollographql/apollo-client/issues/9448 + it("does not issue multiple network calls when calling execute again without variables", async () => { + interface Data { + user: { id: string; name: string }; + } + + interface Variables { + id?: string; + } + + const query: TypedDocumentNode = gql` + query UserQuery($id: ID) { + user(id: $id) { + id + name + } + } + `; + + let fetchCount = 0; + + const link = new ApolloLink((operation) => { + fetchCount++; + return new Observable((observer) => { + setTimeout(() => { + observer.next({ + data: { + user: { id: operation.variables.id || null, name: "John Doe" }, + }, + }); + observer.complete(); + }, 20); + }); + }); + + const client = new ApolloClient({ + link, + cache: new InMemoryCache(), + defaultOptions: { watchQuery: { fetchPolicy: "no-cache" } }, + }); + + const { result } = renderHook(() => useLazyQuery(query), { + wrapper: ({ children }) => ( + {children} + ), + }); + + const [execute] = result.current; + + await act(() => execute({ variables: { id: "2" } })); + + expect(fetchCount).toBe(1); + + await waitFor(() => { + expect(result.current[1].data).toEqual({ + user: { id: "2", name: "John Doe" }, + }); + }); + + expect(fetchCount).toBe(1); + + await act(() => execute()); + + await waitFor(() => { + expect(result.current[1].data).toEqual({ + user: { id: null, name: "John Doe" }, + }); + }); + + expect(fetchCount).toBe(2); + }); + describe("network errors", () => { async function check(errorPolicy: ErrorPolicy) { const networkError = new Error("from the network");