Open History to see the last commit
1import { RuleTester } from '@typescript-eslint/rule-tester'
2import {
3 reactHookNames,
4 rule,
5 useQueryHookNames,
6} from '../rules/no-unstable-deps/no-unstable-deps.rule'
7β
8const ruleTester = new RuleTester()
9β
10interface TestCase {
11 reactHookImport: string
12 reactHookInvocation: string
13 reactHookAlias: string
14}
15const baseTestCases = {
16 valid: ({ reactHookImport, reactHookInvocation, reactHookAlias }: TestCase) =>
17 [
18 {
19 name: `should pass when destructured mutate is passed to ${reactHookAlias} as dependency`,
20 code: `
21 ${reactHookImport}
22 import { useMutation } from "@tanstack/react-query";
23β
24 function Component() {
25 const { mutate } = useMutation({ mutationFn: (value: string) => value });
26 const callback = ${reactHookInvocation}(() => { mutate('hello') }, [mutate]);
27 return;
28 }
29 `,
30 },
31 ]
32 .concat(
33 useQueryHookNames.map((queryHook) => ({
34 name: `should pass result of ${queryHook} is passed to ${reactHookInvocation} as dependency`,
35 code: `
36 ${reactHookImport}
37 import { ${queryHook} } from "@tanstack/react-query";
38β
39 function Component() {
40 const { refetch } = ${queryHook}({ queryFn: (value: string) => value });
41 const callback = ${reactHookInvocation}(() => { query.refetch() }, [refetch]);
42 return;
43 }
44 `,
45 })),
46 )
47 .concat([
48 {
49 name: `should pass when useQueries with combine is passed to ${reactHookAlias} as dependency`,
50 code: `
51 ${reactHookImport}
52 import { useQueries } from "@tanstack/react-query";
53β
54 function Component() {
55 const queries = useQueries({
56 queries: [
57 { queryKey: ['test'], queryFn: () => 'test' }
58 ],
59 combine: (results) => ({ data: results[0]?.data })
60 });
61 const callback = ${reactHookInvocation}(() => { queries.data }, [queries]);
62 return;
63 }
64 `,
65 },
66 {
67 name: `should pass when useSuspenseQueries with combine is passed to ${reactHookAlias} as dependency`,
68 code: `
69 ${reactHookImport}
70 import { useSuspenseQueries } from "@tanstack/react-query";
71β
72 function Component() {
73 const queries = useSuspenseQueries({
74 queries: [
75 { queryKey: ['test'], queryFn: () => 'test' }
76 ],
77 combine: (results) => ({ data: results[0]?.data })
78 });
79 const callback = ${reactHookInvocation}(() => { queries.data }, [queries]);
80 return;
81 }
82 `,
83 },
84 ])
85 .concat([
86 {
87 name: `should pass when useQueries is array-destructured and element properties are used with ${reactHookAlias}`,
88 code: `
89 ${reactHookImport}
90 import { useQueries } from "@tanstack/react-query";
91β
92 function Component() {
93 const [{ data }] = useQueries({
94 queries: [
95 { queryKey: ['test'], queryFn: () => 'test' }
96 ]
97 });
98 const callback = ${reactHookInvocation}(() => { data }, [data]);
99 return;
100 }
101 `,
102 },
103 ])
104 .concat([
105 {
106 name: `should pass when useQuery is imported from non-TanStack source and used with ${reactHookAlias}`,
107 code: `
108 ${reactHookImport}
109 import { useQuery } from "./router";
110β
111 function Component() {
112 const query = useQuery();
113 const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]);
114 return;
115 }
116 `,
117 },
118 {
119 name: `should pass when useMutation is imported from non-TanStack source and used with ${reactHookAlias}`,
120 code: `
121 ${reactHookImport}
122 import { useMutation } from "./api";
123β
124 function Component() {
125 const mutation = useMutation();
126 const callback = ${reactHookInvocation}(() => { mutation.mutate() }, [mutation]);
127 return;
128 }
129 `,
130 },
131 {
132 name: `should pass when functions or variables with Object.prototype names are used with ${reactHookAlias}`,
133 code: `
134 ${reactHookImport}
135 import { useQuery } from "@tanstack/react-query";
136β
137 function Component() {
138 const toString = () => 'str';
139 const valueOf = 42;
140 const callback = ${reactHookInvocation}(() => { toString() }, [toString, valueOf]);
141 return;
142 }
143 `,
144 },
145 {
146 name: `should pass when custom functions named after Object.prototype methods are invoked`,
147 code: `
148 ${reactHookImport}
149 import { useQuery } from "@tanstack/react-query";
150β
151 function toString() {
152 return 'test';
153 }
154β
155 function Component() {
156 const res = toString();
157 const callback = ${reactHookInvocation}(() => { res }, [res]);
158 return;
159 }
160 `,
161 },
162 {
163 name: `should pass when a local function named after Object.prototype method is called alongside destructured query result with ${reactHookAlias}`,
164 code: `
165 ${reactHookImport}
166 import { useQuery } from "@tanstack/react-query";
167β
168 function toString() {
169 return 'formatted';
170 }
171β
172 function Component() {
173 const { data } = useQuery({ queryKey: ['test'], queryFn: () => 'test' });
174 const formatted = toString();
175 const callback = ${reactHookInvocation}(() => { formatted }, [data, formatted]);
176 return;
177 }
178 `,
179 },
180 ]),
181 invalid: ({
182 reactHookImport,
183 reactHookInvocation,
184 reactHookAlias,
185 }: TestCase) =>
186 [
187 {
188 name: `result of useMutation is passed to ${reactHookInvocation} as dependency `,
189 code: `
190 ${reactHookImport}
191 import { useMutation } from "@tanstack/react-query";
192β
193 function Component() {
194 const mutation = useMutation({ mutationFn: (value: string) => value });
195 const callback = ${reactHookInvocation}(() => { mutation.mutate('hello') }, [mutation]);
196 return;
197 }
198 `,
199 errors: [
200 {
201 messageId: 'noUnstableDeps',
202 data: { reactHook: reactHookAlias, queryHook: 'useMutation' },
203 },
204 ],
205 },
206 ]
207 .concat(
208 useQueryHookNames.map((queryHook) => ({
209 name: `result of ${queryHook} is passed to ${reactHookInvocation} as dependency`,
210 code: `
211 ${reactHookImport}
212 import { ${queryHook} } from "@tanstack/react-query";
213β
214 function Component() {
215 const query = ${queryHook}({ queryFn: (value: string) => value });
216 const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]);
217 return;
218 }
219 `,
220 errors: [
221 {
222 messageId: 'noUnstableDeps',
223 data: { reactHook: reactHookAlias, queryHook },
224 },
225 ],
226 })),
227 )
228 .concat([
229 {
230 name: `result of useQueries without combine is passed to ${reactHookInvocation} as dependency`,
231 code: `
232 ${reactHookImport}
233 import { useQueries } from "@tanstack/react-query";
234β
235 function Component() {
236 const queries = useQueries({
237 queries: [
238 { queryKey: ['test'], queryFn: () => 'test' }
239 ]
240 });
241 const callback = ${reactHookInvocation}(() => { queries[0]?.data }, [queries]);
242 return;
243 }
244 `,
245 errors: [
246 {
247 messageId: 'noUnstableDeps',
248 data: { reactHook: reactHookAlias, queryHook: 'useQueries' },
249 },
250 ],
251 },
252 {
253 name: `result of custom useMutation wrapper is passed to ${reactHookInvocation} as dependency`,
254 code: `
255 ${reactHookImport}
256 import { useMutation } from "@tanstack/react-query";
257β
258 const useMyMutation = () => useMutation({ mutationFn: (value: string) => value });
259β
260 function Component() {
261 const mutation = useMyMutation();
262 const callback = ${reactHookInvocation}(() => { mutation.mutate('hello') }, [mutation]);
263 return;
264 }
265 `,
266 errors: [
267 {
268 messageId: 'noUnstableDeps',
269 data: { reactHook: reactHookAlias, queryHook: 'useMutation' },
270 },
271 ],
272 },
273 {
274 name: `result of custom useQuery wrapper is passed to ${reactHookInvocation} as dependency`,
275 code: `
276 ${reactHookImport}
277 import { useQuery } from "@tanstack/react-query";
278β
279 function useMyQuery() {
280 return useQuery({ queryFn: (value: string) => value });
281 }
282β
283 function Component() {
284 const query = useMyQuery();
285 const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]);
286 return;
287 }
288 `,
289 errors: [
290 {
291 messageId: 'noUnstableDeps',
292 data: { reactHook: reactHookAlias, queryHook: 'useQuery' },
293 },
294 ],
295 },
296 {
297 name: `result of later custom useMutation wrapper is passed to ${reactHookInvocation} as dependency`,
298 code: `
299 ${reactHookImport}
300 import { useMutation } from "@tanstack/react-query";
301β
302 function Component() {
303 const mutation = useMyMutation();
304 const callback = ${reactHookInvocation}(() => { mutation.mutate('hello') }, [mutation]);
305 return;
306 }
307β
308 function useMyMutation() {
309 return useMutation({ mutationFn: (value: string) => value });
310 }
311 `,
312 errors: [
313 {
314 messageId: 'noUnstableDeps',
315 data: { reactHook: reactHookAlias, queryHook: 'useMutation' },
316 },
317 ],
318 },
319 {
320 name: `result of later custom useQuery wrapper is passed to ${reactHookInvocation} as dependency`,
321 code: `
322 ${reactHookImport}
323 import { useQuery } from "@tanstack/react-query";
324β
325 function Component() {
326 const query = useMyQuery();
327 const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]);
328 return;
329 }
330β
331 function useMyQuery() {
332 return useQuery({ queryFn: (value: string) => value });
333 }
334 `,
335 errors: [
336 {
337 messageId: 'noUnstableDeps',
338 data: { reactHook: reactHookAlias, queryHook: 'useQuery' },
339 },
340 ],
341 },
342 ])
343 .concat([
344 {
345 name: `array-destructured useQueries element is passed to ${reactHookInvocation} as dependency`,
346 code: `
347 ${reactHookImport}
348 import { useQueries } from "@tanstack/react-query";
349β
350 function Component() {
351 const [userQuery, postsQuery] = useQueries({
352 queries: [
353 { queryKey: ['user'], queryFn: () => 'user' },
354 { queryKey: ['posts'], queryFn: () => 'posts' }
355 ]
356 });
357 const callback = ${reactHookInvocation}(() => { userQuery.data }, [userQuery]);
358 return;
359 }
360 `,
361 errors: [
362 {
363 messageId: 'noUnstableDeps',
364 data: { reactHook: reactHookAlias, queryHook: 'useQueries' },
365 },
366 ],
367 },
368 {
369 name: `array-destructured useSuspenseQueries element is passed to ${reactHookInvocation} as dependency`,
370 code: `
371 ${reactHookImport}
372 import { useSuspenseQueries } from "@tanstack/react-query";
373β
374 function Component() {
375 const [query] = useSuspenseQueries({
376 queries: [
377 { queryKey: ['test'], queryFn: () => 'test' }
378 ]
379 });
380 const callback = ${reactHookInvocation}(() => { query.data }, [query]);
381 return;
382 }
383 `,
384 errors: [
385 {
386 messageId: 'noUnstableDeps',
387 data: {
388 reactHook: reactHookAlias,
389 queryHook: 'useSuspenseQueries',
390 },
391 },
392 ],
393 },
394 {
395 name: `rest element of array-destructured useQueries is passed to ${reactHookInvocation} as dependency`,
396 code: `
397 ${reactHookImport}
398 import { useQueries } from "@tanstack/react-query";
399β
400 function Component() {
401 const [firstQuery, ...restQueries] = useQueries({
402 queries: [
403 { queryKey: ['a'], queryFn: () => 'a' },
404 { queryKey: ['b'], queryFn: () => 'b' }
405 ]
406 });
407 const callback = ${reactHookInvocation}(() => {}, [restQueries]);
408 return;
409 }
410 `,
411 errors: [
412 {
413 messageId: 'noUnstableDeps',
414 data: { reactHook: reactHookAlias, queryHook: 'useQueries' },
415 },
416 ],
417 },
418 ]),
419}
420β
421const testCases = (reactHookName: string) => [
422 {
423 reactHookImport: 'import * as React from "react";',
424 reactHookInvocation: `React.${reactHookName}`,
425 reactHookAlias: reactHookName,
426 },
427 {
428 reactHookImport: `import { ${reactHookName} } from "react";`,
429 reactHookInvocation: reactHookName,
430 reactHookAlias: reactHookName,
431 },
432 {
433 reactHookImport: `import { ${reactHookName} as useAlias } from "react";`,
434 reactHookInvocation: 'useAlias',
435 reactHookAlias: 'useAlias',
436 },
437]
438β
439reactHookNames.forEach((reactHookName) => {
440 testCases(reactHookName).forEach(
441 ({ reactHookInvocation, reactHookAlias, reactHookImport }) => {
442 ruleTester.run('no-unstable-deps', rule, {
443 valid: baseTestCases.valid({
444 reactHookImport,
445 reactHookInvocation,
446 reactHookAlias,
447 }),
448 invalid: baseTestCases.invalid({
449 reactHookImport,
450 reactHookInvocation,
451 reactHookAlias,
452 }),
453 })
454 },
455 )
456})