Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

stable-query-client.test.ts

216 lines5.9 KB

1import { RuleTester } from '@typescript-eslint/rule-tester'
2import { rule } from '../rules/stable-query-client/stable-query-client.rule'
3import { normalizeIndent } from './test-utils'
4​
5const ruleTester = new RuleTester()
6​
7ruleTester.run('stable-query-client', rule, {
8 valid: [
9 {
10 name: 'QueryClient is stable when wrapped in React.useState',
11 code: normalizeIndent`
12 import { QueryClient } from "@tanstack/react-query";
13​
14 function Component() {
15 const [queryClient] = React.useState(() => new QueryClient());
16 return;
17 }
18 `,
19 },
20 {
21 name: 'QueryClient is stable when wrapped in useState',
22 code: normalizeIndent`
23 import { QueryClient } from "@tanstack/react-query";
24​
25 function Component() {
26 const [queryClient] = useState(() => new QueryClient());
27 return;
28 }
29 `,
30 },
31 {
32 name: 'QueryClient is stable when wrapped in React.useMemo',
33 code: normalizeIndent`
34 import { QueryClient } from "@tanstack/react-query";
35​
36 function Component() {
37 const [queryClient] = React.useMemo(() => new QueryClient(), []);
38 return;
39 }
40 `,
41 },
42 {
43 name: 'QueryClient is stable when wrapped in useAnything',
44 code: normalizeIndent`
45 import { QueryClient } from "@tanstack/react-query";
46​
47 function Component() {
48 const [queryClient] = useAnything(() => new QueryClient());
49 return;
50 }
51 `,
52 },
53 {
54 name: 'QueryClient is imported from a non-tanstack package',
55 code: normalizeIndent`
56 import { QueryClient } from "other-library";
57​
58 function Component() {
59 const queryClient = new QueryClient();
60 return;
61 }
62 `,
63 },
64 {
65 name: 'QueryClient is not imported from @tanstack/react-query',
66 code: normalizeIndent`
67 import { QueryClient } from "@tanstack/solid-query";
68​
69 function Component() {
70 const queryClient = new QueryClient();
71 return;
72 }
73 `,
74 },
75 {
76 name: 'QueryClient is invoked outside of a function',
77 code: normalizeIndent`
78 import { QueryClient } from "@tanstack/solid-query";
79​
80 const queryClient = new QueryClient();
81​
82 function Component() {
83 return;
84 }
85 `,
86 },
87 {
88 name: 'QueryClient is invoked in a non-component function',
89 code: normalizeIndent`
90 import { QueryClient } from "@tanstack/solid-query";
91​
92 function someFn() {
93 const queryClient = new QueryClient();
94 return;
95 }
96 `,
97 },
98 {
99 name: 'QueryClient is not flagged when imported from a non-react-query package in an async component',
100 code: normalizeIndent`
101 import { QueryClient } from "@tanstack/solid-query";
102​
103 async function AsyncComponent() {
104 const queryClient = new QueryClient();
105 return;
106 }
107 `,
108 },
109 {
110 name: 'QueryClient is not flagged in an async react-query server component',
111 code: normalizeIndent`
112 import { QueryClient } from "@tanstack/react-query";
113​
114 async function ServerComponent() {
115 const queryClient = new QueryClient();
116 return;
117 }
118 `,
119 },
120 ],
121 invalid: [
122 {
123 name: 'QueryClient is not stable when it is not wrapped in React.useState in component',
124 code: normalizeIndent`
125 import { QueryClient } from "@tanstack/react-query";
126​
127 function Component() {
128 const queryClient = new QueryClient();
129 return;
130 }
131 `,
132 output: normalizeIndent`
133 import { QueryClient } from "@tanstack/react-query";
134​
135 function Component() {
136 const [queryClient] = React.useState(() => new QueryClient());
137 return;
138 }
139 `,
140 errors: [{ messageId: 'unstable' }],
141 },
142 {
143 name: 'QueryClient is not stable when it is not wrapped in React.useState in custom hook',
144 code: normalizeIndent`
145 import { QueryClient } from "@tanstack/react-query";
146​
147 function useHook() {
148 const queryClient = new QueryClient();
149 return;
150 }
151 `,
152 output: normalizeIndent`
153 import { QueryClient } from "@tanstack/react-query";
154​
155 function useHook() {
156 const [queryClient] = React.useState(() => new QueryClient());
157 return;
158 }
159 `,
160 errors: [{ messageId: 'unstable' }],
161 },
162 {
163 name: 'preserve QueryClient options',
164 code: normalizeIndent`
165 import { QueryClient } from "@tanstack/react-query";
166​
167 function Component() {
168 const queryClient = new QueryClient({ defaultOptions: { /* */ } });
169 return;
170 }
171 `,
172 output: normalizeIndent`
173 import { QueryClient } from "@tanstack/react-query";
174​
175 function Component() {
176 const [queryClient] = React.useState(() => new QueryClient({ defaultOptions: { /* */ } }));
177 return;
178 }
179 `,
180 errors: [{ messageId: 'unstable' }],
181 },
182 {
183 name: 'preserve QueryClient variable declarator name',
184 code: normalizeIndent`
185 import { QueryClient } from "@tanstack/react-query";
186​
187 function Component() {
188 const customName = new QueryClient();
189 return;
190 }
191 `,
192 output: normalizeIndent`
193 import { QueryClient } from "@tanstack/react-query";
194​
195 function Component() {
196 const [customName] = React.useState(() => new QueryClient());
197 return;
198 }
199 `,
200 errors: [{ messageId: 'unstable' }],
201 },
202 {
203 name: 'QueryClient with destructuring pattern reports error without autofix',
204 code: normalizeIndent`
205 import { QueryClient } from "@tanstack/react-query";
206​
207 function Component() {
208 const { defaultOptions } = new QueryClient();
209 return;
210 }
211 `,
212 output: null,
213 errors: [{ messageId: 'unstable' }],
214 },
215 ],
216})