Skip to content

Commit

Permalink
Provide failing test for #9448
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 1, 2023
1 parent b9332e0 commit 6e44067
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/react/hooks/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data, Variables> = 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 }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
});

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");
Expand Down

0 comments on commit 6e44067

Please sign in to comment.