Skip to content

Commit

Permalink
Test that defaultOptions do not interfere with skipping/unskipping.
Browse files Browse the repository at this point in the history
This test started out as a regression test for PR #9665, but it revealed
a few other problems that I will fix in the next commit, potentially
solving issue #9655 as well.
  • Loading branch information
benjamn committed May 3, 2022
1 parent 77119b0 commit 6695b02
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,111 @@ describe('useQuery Hook', () => {
"cache-first",
]);
});

it("defaultOptions do not confuse useQuery when unskipping a query (issue #9635)", async () => {
const query: TypedDocumentNode<{
counter: number;
}> = gql`
query GetCounter {
counter
}
`;

let count = 0;
const client = new ApolloClient({
cache: new InMemoryCache(),
link: new ApolloLink(request => new Observable(observer => {
if (request.operationName === "GetCounter") {
observer.next({
data: {
counter: ++count,
},
});
setTimeout(() => {
observer.complete();
}, 10);
} else {
observer.error(new Error(`Unknown query: ${
request.operationName || request.query
}`));
}
})),
});

const defaultFetchPolicy = "network-only";

const { result, waitForNextUpdate } = renderHook(
() => {
const [skip, setSkip] = useState(true);
return {
setSkip,
query: useQuery(query, {
skip,
defaultOptions: {
fetchPolicy: defaultFetchPolicy,
},
}),
};
},
{
wrapper: ({ children }) => (
<ApolloProvider client={client}>
{children}
</ApolloProvider>
),
},
);

expect(result.current.query.loading).toBe(false);
expect(result.current.query.networkStatus).toBe(NetworkStatus.ready);
expect(result.current.query.data).toBeUndefined();

await expect(waitForNextUpdate({
timeout: 20,
})).rejects.toThrow('Timed out');

act(() => {
result.current.setSkip(false);
});
expect(result.current.query.loading).toBe(true);
expect(result.current.query.networkStatus).toBe(NetworkStatus.loading);
expect(result.current.query.data).toBeUndefined();
await waitForNextUpdate();
expect(result.current.query.loading).toBe(false);
expect(result.current.query.networkStatus).toBe(NetworkStatus.ready);
expect(result.current.query.data).toEqual({ counter: 1 });

const { options } = result.current.query.observable;
expect(options.fetchPolicy).toBe(defaultFetchPolicy);

await expect(waitForNextUpdate({
timeout: 20,
})).rejects.toThrow('Timed out');

act(() => {
result.current.setSkip(true);
});
expect(result.current.query.loading).toBe(false);
expect(result.current.query.networkStatus).toBe(NetworkStatus.ready);
expect(result.current.query.data).toBeUndefined();

await expect(waitForNextUpdate({
timeout: 20,
})).rejects.toThrow('Timed out');

act(() => {
result.current.setSkip(false);
});
expect(result.current.query.loading).toBe(true);
expect(result.current.query.networkStatus).toBe(NetworkStatus.loading);
expect(result.current.query.data).toEqual({ counter: 1 });
await waitForNextUpdate();
expect(result.current.query.loading).toBe(false);
expect(result.current.query.networkStatus).toBe(NetworkStatus.ready);
expect(result.current.query.data).toEqual({ counter: 2 });

expect(options.fetchPolicy).toBe(defaultFetchPolicy);
});
});

it("can provide options.client without ApolloProvider", async () => {
Expand Down

0 comments on commit 6695b02

Please sign in to comment.