Skip to content

Commit 5ae1215

Browse files
authored
Use fake timers to test polling (apollographql#12323)
1 parent 72c5a9b commit 5ae1215

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/react/hooks/__tests__/useQuery.test.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,7 @@ describe("useQuery Hook", () => {
26842684
// https://github.com/apollographql/apollo-client/issues/9431
26852685
// https://github.com/apollographql/apollo-client/issues/11750
26862686
it("stops polling when component unmounts with cache-and-network fetch policy", async () => {
2687+
jest.useFakeTimers();
26872688
const query: TypedDocumentNode<{ hello: string }> = gql`
26882689
query {
26892690
hello
@@ -2694,17 +2695,17 @@ describe("useQuery Hook", () => {
26942695
{
26952696
request: { query },
26962697
result: { data: { hello: "world 1" } },
2697-
delay: 3,
2698+
delay: 20,
26982699
},
26992700
{
27002701
request: { query },
27012702
result: { data: { hello: "world 2" } },
2702-
delay: 3,
2703+
delay: 20,
27032704
},
27042705
{
27052706
request: { query },
27062707
result: { data: { hello: "world 3" } },
2707-
delay: 3,
2708+
delay: 20,
27082709
},
27092710
];
27102711

@@ -2725,7 +2726,7 @@ describe("useQuery Hook", () => {
27252726
const { takeSnapshot, unmount } = await renderHookToSnapshotStream(
27262727
() =>
27272728
useQuery(query, {
2728-
pollInterval: 10,
2729+
pollInterval: 100,
27292730
fetchPolicy: "cache-and-network",
27302731
}),
27312732
{
@@ -2736,9 +2737,10 @@ describe("useQuery Hook", () => {
27362737
);
27372738

27382739
{
2739-
const result = await takeSnapshot();
2740+
const promise = takeSnapshot();
2741+
await jest.advanceTimersByTimeAsync(0);
27402742

2741-
expect(result).toEqualQueryResult({
2743+
await expect(promise).resolves.toEqualQueryResult({
27422744
data: undefined,
27432745
called: true,
27442746
loading: true,
@@ -2748,10 +2750,13 @@ describe("useQuery Hook", () => {
27482750
});
27492751
}
27502752

2753+
jest.advanceTimersByTime(20);
2754+
27512755
{
2752-
const result = await takeSnapshot();
2756+
const promise = takeSnapshot();
2757+
await jest.advanceTimersByTimeAsync(0);
27532758

2754-
expect(result).toEqualQueryResult({
2759+
await expect(promise).resolves.toEqualQueryResult({
27552760
data: { hello: "world 1" },
27562761
called: true,
27572762
loading: false,
@@ -2762,12 +2767,13 @@ describe("useQuery Hook", () => {
27622767
expect(requestSpy).toHaveBeenCalledTimes(1);
27632768
}
27642769

2765-
await wait(10);
2770+
jest.advanceTimersByTime(100);
27662771

27672772
{
2768-
const result = await takeSnapshot();
2773+
const promise = takeSnapshot();
2774+
await jest.advanceTimersByTimeAsync(0);
27692775

2770-
expect(result).toEqualQueryResult({
2776+
await expect(promise).resolves.toEqualQueryResult({
27712777
data: { hello: "world 2" },
27722778
called: true,
27732779
loading: false,
@@ -2780,11 +2786,12 @@ describe("useQuery Hook", () => {
27802786

27812787
unmount();
27822788

2783-
await expect(takeSnapshot).not.toRerender({ timeout: 50 });
2789+
jest.advanceTimersByTime(200);
27842790

2785-
// TODO rarely seeing 3 here (also old `useQuery` implementation)
27862791
expect(requestSpy).toHaveBeenCalledTimes(2);
27872792
expect(onErrorFn).toHaveBeenCalledTimes(0);
2793+
2794+
jest.useRealTimers();
27882795
});
27892796

27902797
it("should stop polling when component is unmounted in Strict Mode", async () => {

0 commit comments

Comments
 (0)