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