Skip to content

Commit e9d4910

Browse files
committed
Ensure new pollInterval is applied in useLazyQuery when rerendered with a new value
1 parent 189d166 commit e9d4910

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import {
2+
disableActEnvironment,
3+
renderHookToSnapshotStream,
4+
} from "@testing-library/react-render-stream";
5+
import React from "react";
6+
7+
import { gql, NetworkStatus } from "@apollo/client";
8+
import { useLazyQuery } from "@apollo/client/react";
9+
import { MockedProvider } from "@apollo/client/testing/react";
10+
11+
test("updates poll interval when rerendering with different pollInterval", async () => {
12+
const query = gql`
13+
query {
14+
hello
15+
}
16+
`;
17+
18+
let count = 0;
19+
20+
const wrapper = ({ children }: any) => (
21+
<MockedProvider
22+
mocks={[
23+
{
24+
request: { query },
25+
result: () => ({ data: { hello: `world ${++count}` } }),
26+
delay: 10,
27+
maxUsageCount: Number.POSITIVE_INFINITY,
28+
},
29+
]}
30+
>
31+
{children}
32+
</MockedProvider>
33+
);
34+
35+
using _disabledAct = disableActEnvironment();
36+
const renderStream = await renderHookToSnapshotStream(
37+
({ pollInterval }) => useLazyQuery(query, { pollInterval }),
38+
{ initialProps: { pollInterval: 50 }, wrapper }
39+
);
40+
41+
const { takeSnapshot, getCurrentSnapshot, rerender } = renderStream;
42+
43+
{
44+
const [, result] = await takeSnapshot();
45+
46+
expect(result).toStrictEqualTyped({
47+
data: undefined,
48+
dataState: "empty",
49+
called: false,
50+
loading: false,
51+
networkStatus: NetworkStatus.ready,
52+
previousData: undefined,
53+
variables: {},
54+
});
55+
}
56+
57+
const [execute] = getCurrentSnapshot();
58+
59+
await expect(execute()).resolves.toStrictEqualTyped({
60+
data: { hello: "world 1" },
61+
});
62+
63+
{
64+
const [, result] = await takeSnapshot();
65+
66+
expect(result).toStrictEqualTyped({
67+
data: undefined,
68+
dataState: "empty",
69+
called: true,
70+
loading: true,
71+
networkStatus: NetworkStatus.loading,
72+
previousData: undefined,
73+
variables: {},
74+
});
75+
}
76+
77+
{
78+
const [, result] = await takeSnapshot();
79+
80+
expect(result).toStrictEqualTyped({
81+
data: { hello: "world 1" },
82+
dataState: "complete",
83+
called: true,
84+
loading: false,
85+
networkStatus: NetworkStatus.ready,
86+
previousData: undefined,
87+
variables: {},
88+
});
89+
}
90+
91+
{
92+
const [, result] = await takeSnapshot({ timeout: 60 });
93+
94+
expect(result).toStrictEqualTyped({
95+
data: { hello: "world 1" },
96+
dataState: "complete",
97+
called: true,
98+
loading: true,
99+
networkStatus: NetworkStatus.poll,
100+
previousData: undefined,
101+
variables: {},
102+
});
103+
}
104+
105+
{
106+
const [, result] = await takeSnapshot();
107+
108+
expect(result).toStrictEqualTyped({
109+
data: { hello: "world 2" },
110+
dataState: "complete",
111+
called: true,
112+
loading: false,
113+
networkStatus: NetworkStatus.ready,
114+
previousData: { hello: "world 1" },
115+
variables: {},
116+
});
117+
}
118+
119+
await rerender({ pollInterval: 100 });
120+
121+
await expect(renderStream).toRerenderWithSimilarSnapshot();
122+
await expect(renderStream).not.toRerender({ timeout: 60 });
123+
124+
{
125+
const [, result] = await takeSnapshot();
126+
127+
expect(result).toStrictEqualTyped({
128+
data: { hello: "world 2" },
129+
dataState: "complete",
130+
called: true,
131+
loading: true,
132+
networkStatus: NetworkStatus.poll,
133+
previousData: { hello: "world 1" },
134+
variables: {},
135+
});
136+
}
137+
138+
{
139+
const [, result] = await takeSnapshot();
140+
141+
expect(result).toStrictEqualTyped({
142+
data: { hello: "world 3" },
143+
dataState: "complete",
144+
called: true,
145+
loading: false,
146+
networkStatus: NetworkStatus.ready,
147+
previousData: { hello: "world 2" },
148+
variables: {},
149+
});
150+
}
151+
152+
await rerender({ pollInterval: 0 });
153+
await expect(renderStream).toRerenderWithSimilarSnapshot();
154+
155+
await expect(renderStream).not.toRerender({ timeout: 150 });
156+
});

src/react/hooks/useLazyQuery.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ export const useLazyQuery: useLazyQuery.Signature = function useLazyQuery<
567567
notifyOnNetworkStatusChange: stableOptions?.notifyOnNetworkStatusChange,
568568
nextFetchPolicy: options?.nextFetchPolicy,
569569
skipPollAttempt: options?.skipPollAttempt,
570+
pollInterval: options?.pollInterval,
570571
};
571572

572573
// Wait to apply the changed fetch policy until after the execute
@@ -589,6 +590,7 @@ export const useLazyQuery: useLazyQuery.Signature = function useLazyQuery<
589590
// so `stableOptions` isn't updated when using inline functions.
590591
options?.nextFetchPolicy,
591592
options?.skipPollAttempt,
593+
options?.pollInterval,
592594
]);
593595

594596
const execute: useLazyQuery.ExecFunction<TData, TVariables> =

0 commit comments

Comments
 (0)