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

prefer-query-options.test.ts

1,019 lines32.4 KB

1import { RuleTester } from '@typescript-eslint/rule-tester'
2import { afterAll, describe, it } from 'vitest'
3import { rule } from '../rules/prefer-query-options/prefer-query-options.rule'
4import { normalizeIndent } from './test-utils'
5​
6RuleTester.afterAll = afterAll
7RuleTester.describe = describe
8RuleTester.it = it
9​
10const ruleTester = new RuleTester()
11​
12describe('prefer-query-options', () => {
13 describe('queryOptions / infiniteQueryOptions builders', () => {
14 ruleTester.run('prefer-query-options', rule, {
15 valid: [
16 {
17 name: 'queryOptions builder is allowed',
18 code: normalizeIndent`
19 import { queryOptions } from '@tanstack/react-query'
20​
21 const todosOptions = queryOptions({
22 queryKey: ['todos'],
23 queryFn: () => fetchTodos(),
24 })
25 `,
26 },
27 {
28 name: 'infiniteQueryOptions builder is allowed',
29 code: normalizeIndent`
30 import { infiniteQueryOptions } from '@tanstack/react-query'
31​
32 const todosOptions = infiniteQueryOptions({
33 queryKey: ['todos'],
34 queryFn: ({ pageParam }) => fetchTodos(pageParam),
35 initialPageParam: 0,
36 getNextPageParam: (lastPage) => lastPage.nextCursor,
37 })
38 `,
39 },
40 ],
41 invalid: [],
42 })
43 })
44​
45 describe('hooks consuming queryOptions result', () => {
46 ruleTester.run('prefer-query-options', rule, {
47 valid: [
48 {
49 name: 'useQuery with queryOptions result is allowed',
50 code: normalizeIndent`
51 import { useQuery, queryOptions } from '@tanstack/react-query'
52​
53 const todosOptions = queryOptions({
54 queryKey: ['todos'],
55 queryFn: () => fetchTodos(),
56 })
57​
58 function Component() {
59 const query = useQuery(todosOptions)
60 return null
61 }
62 `,
63 },
64 {
65 name: 'useQuery with queryOptions function call result is allowed',
66 code: normalizeIndent`
67 import { useQuery } from '@tanstack/react-query'
68​
69 function Component({ id }) {
70 const query = useQuery(todosOptions(id))
71 return null
72 }
73 `,
74 },
75 {
76 name: 'useQuery with imported queryOptions function call is allowed',
77 code: normalizeIndent`
78 import { useQuery } from '@tanstack/react-query'
79 import { getFooOptions } from './foo'
80​
81 function Component({ id }) {
82 const query = useQuery(getFooOptions(id))
83 return null
84 }
85 `,
86 },
87 {
88 name: 'useQuery spreading queryOptions result is allowed',
89 code: normalizeIndent`
90 import { useQuery, queryOptions } from '@tanstack/react-query'
91​
92 const todosOptions = queryOptions({
93 queryKey: ['todos'],
94 queryFn: () => fetchTodos(),
95 })
96​
97 function Component() {
98 const query = useQuery({ ...todosOptions, select: (data) => data.items })
99 return null
100 }
101 `,
102 },
103 {
104 name: 'useQuery spreading queryOptions function call result is allowed',
105 code: normalizeIndent`
106 import { useQuery } from '@tanstack/react-query'
107​
108 function Component({ id }) {
109 const query = useQuery({ ...todosOptions(id), select: (data) => data.items })
110 return null
111 }
112 `,
113 },
114 {
115 name: 'useQueries with all entries from queryOptions is allowed',
116 code: normalizeIndent`
117 import { useQueries } from '@tanstack/react-query'
118​
119 function Component() {
120 const queries = useQueries({
121 queries: [todosOptions, usersOptions],
122 })
123 return null
124 }
125 `,
126 },
127 ],
128 invalid: [],
129 })
130 })
131​
132 describe('queryClient methods referencing queryKey from options', () => {
133 ruleTester.run('prefer-query-options', rule, {
134 valid: [
135 {
136 name: 'queryClient.getQueryData with options.queryKey is allowed',
137 code: normalizeIndent`
138 import { useQueryClient, queryOptions } from '@tanstack/react-query'
139​
140 const todosOptions = queryOptions({
141 queryKey: ['todos'],
142 queryFn: () => fetchTodos(),
143 })
144​
145 function Component() {
146 const queryClient = useQueryClient()
147 const data = queryClient.getQueryData(todosOptions.queryKey)
148 return null
149 }
150 `,
151 },
152 {
153 name: 'queryClient.setQueryData with options.queryKey is allowed',
154 code: normalizeIndent`
155 import { useQueryClient, queryOptions } from '@tanstack/react-query'
156​
157 const todosOptions = queryOptions({
158 queryKey: ['todos'],
159 queryFn: () => fetchTodos(),
160 })
161​
162 function Component() {
163 const queryClient = useQueryClient()
164 queryClient.setQueryData(todosOptions.queryKey, [])
165 return null
166 }
167 `,
168 },
169 {
170 name: 'queryClient.invalidateQueries with options.queryKey is allowed',
171 code: normalizeIndent`
172 import { useQueryClient, queryOptions } from '@tanstack/react-query'
173​
174 const todosOptions = queryOptions({
175 queryKey: ['todos'],
176 queryFn: () => fetchTodos(),
177 })
178​
179 function Component() {
180 const queryClient = useQueryClient()
181 queryClient.invalidateQueries({ queryKey: todosOptions.queryKey })
182 return null
183 }
184 `,
185 },
186 {
187 name: 'queryClient.invalidateQueries with options.queryKey and extra filter props is allowed',
188 code: normalizeIndent`
189 import { useQueryClient, queryOptions } from '@tanstack/react-query'
190​
191 const todosOptions = queryOptions({
192 queryKey: ['todos'],
193 queryFn: () => fetchTodos(),
194 })
195​
196 function Component() {
197 const queryClient = useQueryClient()
198 queryClient.invalidateQueries({ queryKey: todosOptions.queryKey, exact: true })
199 return null
200 }
201 `,
202 },
203 {
204 name: 'queryClient.getQueryData with variable queryKey is allowed',
205 code: normalizeIndent`
206 import { useQueryClient } from '@tanstack/react-query'
207​
208 function Component({ queryKey }) {
209 const queryClient = useQueryClient()
210 const data = queryClient.getQueryData(queryKey)
211 return null
212 }
213 `,
214 },
215 {
216 name: 'shadowed queryClient parameter is ignored',
217 code: normalizeIndent`
218 import { useQueryClient } from '@tanstack/react-query'
219​
220 function Component() {
221 const queryClient = useQueryClient()
222​
223 function run(queryClient) {
224 queryClient.fetchQuery({
225 queryKey: ['todos'],
226 queryFn: () => fetchTodos(),
227 })
228 }
229​
230 return null
231 }
232 `,
233 },
234 {
235 name: 'non-queryClient fetchQuery call is ignored',
236 code: normalizeIndent`
237 import { useQuery } from '@tanstack/react-query'
238​
239 const analytics = {
240 fetchQuery(options) {
241 return options
242 },
243 }
244​
245 function Component() {
246 useQuery(todosOptions)
247​
248 analytics.fetchQuery({
249 queryKey: ['todos'],
250 queryFn: () => fetchTodos(),
251 })
252​
253 return null
254 }
255 `,
256 },
257 ],
258 invalid: [],
259 })
260 })
261​
262 describe('non-tanstack imports', () => {
263 ruleTester.run('prefer-query-options', rule, {
264 valid: [
265 {
266 name: 'non-tanstack useQuery is ignored',
267 code: normalizeIndent`
268 import { useQuery } from 'other-library'
269​
270 function Component() {
271 const query = useQuery({
272 queryKey: ['todos'],
273 queryFn: () => fetchTodos(),
274 })
275 return null
276 }
277 `,
278 },
279 ],
280 invalid: [],
281 })
282 })
283​
284 describe('inline lone queryKey or queryFn in hooks', () => {
285 ruleTester.run('prefer-query-options', rule, {
286 valid: [],
287 invalid: [
288 {
289 name: 'useQuery with only inline queryKey (no queryFn)',
290 code: normalizeIndent`
291 import { useQuery } from '@tanstack/react-query'
292​
293 function Component() {
294 const query = useQuery({ queryKey: ['todos'] })
295 return null
296 }
297 `,
298 errors: [{ messageId: 'preferQueryOptions' }],
299 },
300 {
301 name: 'useQuery with only inline queryFn (no queryKey)',
302 code: normalizeIndent`
303 import { useQuery } from '@tanstack/react-query'
304​
305 function Component() {
306 const query = useQuery({ queryFn: () => fetchTodos() })
307 return null
308 }
309 `,
310 errors: [{ messageId: 'preferQueryOptions' }],
311 },
312 ],
313 })
314 })
315​
316 describe('spread with inline queryKey or queryFn override in hooks', () => {
317 ruleTester.run('prefer-query-options', rule, {
318 valid: [],
319 invalid: [
320 {
321 name: 'useQuery spreading options but overriding queryKey inline',
322 code: normalizeIndent`
323 import { useQuery } from '@tanstack/react-query'
324​
325 function Component() {
326 const query = useQuery({ ...options, queryKey: ['override'] })
327 return null
328 }
329 `,
330 errors: [{ messageId: 'preferQueryOptions' }],
331 },
332 {
333 name: 'useQuery spreading options but overriding queryFn inline',
334 code: normalizeIndent`
335 import { useQuery } from '@tanstack/react-query'
336​
337 function Component() {
338 const query = useQuery({ ...options, queryFn: () => fetchOverride() })
339 return null
340 }
341 `,
342 errors: [{ messageId: 'preferQueryOptions' }],
343 },
344 ],
345 })
346 })
347​
348 describe('inline queryKey + queryFn in hooks', () => {
349 ruleTester.run('prefer-query-options', rule, {
350 valid: [],
351 invalid: [
352 {
353 name: 'useQuery with inline queryKey + queryFn',
354 code: normalizeIndent`
355 import { useQuery } from '@tanstack/react-query'
356​
357 function Component() {
358 const query = useQuery({
359 queryKey: ['todos'],
360 queryFn: () => fetchTodos(),
361 })
362 return null
363 }
364 `,
365 errors: [{ messageId: 'preferQueryOptions' }],
366 },
367 {
368 name: 'aliased useQuery with inline queryKey + queryFn',
369 code: normalizeIndent`
370 import { useQuery as useTanstackQuery } from '@tanstack/react-query'
371​
372 function Component() {
373 const query = useTanstackQuery({
374 queryKey: ['todos'],
375 queryFn: () => fetchTodos(),
376 })
377 return null
378 }
379 `,
380 errors: [{ messageId: 'preferQueryOptions' }],
381 },
382 {
383 name: 'useInfiniteQuery with inline queryKey + queryFn',
384 code: normalizeIndent`
385 import { useInfiniteQuery } from '@tanstack/react-query'
386​
387 function Component() {
388 const query = useInfiniteQuery({
389 queryKey: ['todos'],
390 queryFn: ({ pageParam }) => fetchTodos(pageParam),
391 initialPageParam: 0,
392 getNextPageParam: (lastPage) => lastPage.nextCursor,
393 })
394 return null
395 }
396 `,
397 errors: [{ messageId: 'preferQueryOptions' }],
398 },
399 {
400 name: 'useSuspenseQuery with inline queryKey + queryFn',
401 code: normalizeIndent`
402 import { useSuspenseQuery } from '@tanstack/react-query'
403​
404 function Component() {
405 const query = useSuspenseQuery({
406 queryKey: ['todos'],
407 queryFn: () => fetchTodos(),
408 })
409 return null
410 }
411 `,
412 errors: [{ messageId: 'preferQueryOptions' }],
413 },
414 {
415 name: 'useSuspenseInfiniteQuery with inline queryKey + queryFn',
416 code: normalizeIndent`
417 import { useSuspenseInfiniteQuery } from '@tanstack/react-query'
418​
419 function Component() {
420 const query = useSuspenseInfiniteQuery({
421 queryKey: ['todos'],
422 queryFn: ({ pageParam }) => fetchTodos(pageParam),
423 initialPageParam: 0,
424 getNextPageParam: (lastPage) => lastPage.nextCursor,
425 })
426 return null
427 }
428 `,
429 errors: [{ messageId: 'preferQueryOptions' }],
430 },
431 {
432 name: 'useQueries with inline queryKey + queryFn in queries array',
433 code: normalizeIndent`
434 import { useQueries } from '@tanstack/react-query'
435​
436 function Component() {
437 const queries = useQueries({
438 queries: [
439 {
440 queryKey: ['todos'],
441 queryFn: () => fetchTodos(),
442 },
443 ],
444 })
445 return null
446 }
447 `,
448 errors: [{ messageId: 'preferQueryOptions' }],
449 },
450 {
451 name: 'useQueries with multiple inline entries reports multiple errors',
452 code: normalizeIndent`
453 import { useQueries } from '@tanstack/react-query'
454​
455 function Component() {
456 const queries = useQueries({
457 queries: [
458 {
459 queryKey: ['todos'],
460 queryFn: () => fetchTodos(),
461 },
462 {
463 queryKey: ['users'],
464 queryFn: () => fetchUsers(),
465 },
466 ],
467 })
468 return null
469 }
470 `,
471 errors: [
472 { messageId: 'preferQueryOptions' },
473 { messageId: 'preferQueryOptions' },
474 ],
475 },
476 {
477 name: 'useQueries with mapped inline query objects',
478 code: normalizeIndent`
479 import { useQueries } from '@tanstack/react-query'
480​
481 function Component({ ids }) {
482 const queries = useQueries({
483 queries: ids.map((id) => ({
484 queryKey: ['todos', id],
485 queryFn: () => fetchTodo(id),
486 })),
487 })
488 return null
489 }
490 `,
491 errors: [{ messageId: 'preferQueryOptions' }],
492 },
493 {
494 name: 'useSuspenseQueries with inline queryKey + queryFn in queries array',
495 code: normalizeIndent`
496 import { useSuspenseQueries } from '@tanstack/react-query'
497​
498 function Component() {
499 const queries = useSuspenseQueries({
500 queries: [
501 {
502 queryKey: ['todos'],
503 queryFn: () => fetchTodos(),
504 },
505 ],
506 })
507 return null
508 }
509 `,
510 errors: [{ messageId: 'preferQueryOptions' }],
511 },
512 {
513 name: 'useSuspenseQueries with mapped inline query objects',
514 code: normalizeIndent`
515 import { useSuspenseQueries } from '@tanstack/react-query'
516​
517 function Component({ ids }) {
518 const queries = useSuspenseQueries({
519 queries: ids.map((id) => ({
520 queryKey: ['todos', id],
521 queryFn: () => fetchTodo(id),
522 })),
523 })
524 return null
525 }
526 `,
527 errors: [{ messageId: 'preferQueryOptions' }],
528 },
529 {
530 name: 'usePrefetchQuery with inline queryKey + queryFn',
531 code: normalizeIndent`
532 import { usePrefetchQuery } from '@tanstack/react-query'
533​
534 function Component() {
535 usePrefetchQuery({
536 queryKey: ['todos'],
537 queryFn: () => fetchTodos(),
538 })
539 return null
540 }
541 `,
542 errors: [{ messageId: 'preferQueryOptions' }],
543 },
544 {
545 name: 'usePrefetchInfiniteQuery with inline queryKey + queryFn',
546 code: normalizeIndent`
547 import { usePrefetchInfiniteQuery } from '@tanstack/react-query'
548​
549 function Component() {
550 usePrefetchInfiniteQuery({
551 queryKey: ['todos'],
552 queryFn: ({ pageParam }) => fetchTodos(pageParam),
553 initialPageParam: 0,
554 getNextPageParam: (lastPage) => lastPage.nextCursor,
555 })
556 return null
557 }
558 `,
559 errors: [{ messageId: 'preferQueryOptions' }],
560 },
561 {
562 name: 'inline queryKey + queryFn inside a custom hook',
563 code: normalizeIndent`
564 import { useQuery } from '@tanstack/react-query'
565​
566 function useTodos() {
567 return useQuery({
568 queryKey: ['todos'],
569 queryFn: () => fetchTodos(),
570 })
571 }
572 `,
573 errors: [{ messageId: 'preferQueryOptions' }],
574 },
575 ],
576 })
577 })
578​
579 describe('queryClient with alternate variable names', () => {
580 ruleTester.run('prefer-query-options', rule, {
581 valid: [],
582 invalid: [
583 {
584 name: 'client.fetchQuery with inline queryKey + queryFn',
585 code: normalizeIndent`
586 import { useQueryClient } from '@tanstack/react-query'
587​
588 function Component() {
589 const client = useQueryClient()
590 client.fetchQuery({
591 queryKey: ['todos'],
592 queryFn: () => fetchTodos(),
593 })
594 return null
595 }
596 `,
597 errors: [{ messageId: 'preferQueryOptions' }],
598 },
599 {
600 name: 'aliased useQueryClient tracks query client variables',
601 code: normalizeIndent`
602 import { useQueryClient as getClient } from '@tanstack/react-query'
603​
604 function Component() {
605 const client = getClient()
606 client.fetchQuery({
607 queryKey: ['todos'],
608 queryFn: () => fetchTodos(),
609 })
610 return null
611 }
612 `,
613 errors: [{ messageId: 'preferQueryOptions' }],
614 },
615 {
616 name: 'aliased QueryClient tracks query client instances',
617 code: normalizeIndent`
618 import { QueryClient as Client } from '@tanstack/react-query'
619​
620 const queryClient = new Client()
621​
622 queryClient.fetchQuery({
623 queryKey: ['todos'],
624 queryFn: () => fetchTodos(),
625 })
626 `,
627 errors: [{ messageId: 'preferQueryOptions' }],
628 },
629 {
630 name: 'qc.getQueryData with inline queryKey',
631 code: normalizeIndent`
632 import { useQueryClient } from '@tanstack/react-query'
633​
634 function Component() {
635 const qc = useQueryClient()
636 const data = qc.getQueryData(['todos'])
637 return null
638 }
639 `,
640 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
641 },
642 {
643 name: 'client.invalidateQueries with inline queryKey in filters',
644 code: normalizeIndent`
645 import { useQueryClient } from '@tanstack/react-query'
646​
647 function Component() {
648 const client = useQueryClient()
649 client.invalidateQueries({ queryKey: ['todos'] })
650 return null
651 }
652 `,
653 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
654 },
655 ],
656 })
657 })
658​
659 describe('inline queryKey + queryFn in queryClient methods', () => {
660 ruleTester.run('prefer-query-options', rule, {
661 valid: [],
662 invalid: [
663 {
664 name: 'queryClient.fetchQuery with inline queryKey + queryFn',
665 code: normalizeIndent`
666 import { useQueryClient } from '@tanstack/react-query'
667​
668 function Component() {
669 const queryClient = useQueryClient()
670 queryClient.fetchQuery({
671 queryKey: ['todos'],
672 queryFn: () => fetchTodos(),
673 })
674 return null
675 }
676 `,
677 errors: [{ messageId: 'preferQueryOptions' }],
678 },
679 {
680 name: 'queryClient.prefetchQuery with inline queryKey + queryFn',
681 code: normalizeIndent`
682 import { useQueryClient } from '@tanstack/react-query'
683​
684 function Component() {
685 const queryClient = useQueryClient()
686 queryClient.prefetchQuery({
687 queryKey: ['todos'],
688 queryFn: () => fetchTodos(),
689 })
690 return null
691 }
692 `,
693 errors: [{ messageId: 'preferQueryOptions' }],
694 },
695 {
696 name: 'queryClient.fetchInfiniteQuery with inline queryKey + queryFn',
697 code: normalizeIndent`
698 import { useQueryClient } from '@tanstack/react-query'
699​
700 function Component() {
701 const queryClient = useQueryClient()
702 queryClient.fetchInfiniteQuery({
703 queryKey: ['todos'],
704 queryFn: ({ pageParam }) => fetchTodos(pageParam),
705 initialPageParam: 0,
706 getNextPageParam: (lastPage) => lastPage.nextCursor,
707 })
708 return null
709 }
710 `,
711 errors: [{ messageId: 'preferQueryOptions' }],
712 },
713 {
714 name: 'queryClient.prefetchInfiniteQuery with inline queryKey + queryFn',
715 code: normalizeIndent`
716 import { useQueryClient } from '@tanstack/react-query'
717​
718 function Component() {
719 const queryClient = useQueryClient()
720 queryClient.prefetchInfiniteQuery({
721 queryKey: ['todos'],
722 queryFn: ({ pageParam }) => fetchTodos(pageParam),
723 initialPageParam: 0,
724 getNextPageParam: (lastPage) => lastPage.nextCursor,
725 })
726 return null
727 }
728 `,
729 errors: [{ messageId: 'preferQueryOptions' }],
730 },
731 {
732 name: 'queryClient.ensureQueryData with inline queryKey + queryFn',
733 code: normalizeIndent`
734 import { useQueryClient } from '@tanstack/react-query'
735​
736 function Component() {
737 const queryClient = useQueryClient()
738 queryClient.ensureQueryData({
739 queryKey: ['todos'],
740 queryFn: () => fetchTodos(),
741 })
742 return null
743 }
744 `,
745 errors: [{ messageId: 'preferQueryOptions' }],
746 },
747 {
748 name: 'queryClient.ensureInfiniteQueryData with inline queryKey + queryFn',
749 code: normalizeIndent`
750 import { useQueryClient } from '@tanstack/react-query'
751​
752 function Component() {
753 const queryClient = useQueryClient()
754 queryClient.ensureInfiniteQueryData({
755 queryKey: ['todos'],
756 queryFn: ({ pageParam }) => fetchTodos(pageParam),
757 initialPageParam: 0,
758 getNextPageParam: (lastPage) => lastPage.nextCursor,
759 })
760 return null
761 }
762 `,
763 errors: [{ messageId: 'preferQueryOptions' }],
764 },
765 ],
766 })
767 })
768​
769 describe('inline queryKey as direct parameter', () => {
770 ruleTester.run('prefer-query-options', rule, {
771 valid: [],
772 invalid: [
773 {
774 name: 'queryClient.getQueryData with inline queryKey',
775 code: normalizeIndent`
776 import { useQueryClient } from '@tanstack/react-query'
777​
778 function Component() {
779 const queryClient = useQueryClient()
780 const data = queryClient.getQueryData(['todos'])
781 return null
782 }
783 `,
784 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
785 },
786 {
787 name: 'queryClient.getQueryData with inline queryKey as const',
788 code: normalizeIndent`
789 import { useQueryClient } from '@tanstack/react-query'
790​
791 function Component() {
792 const queryClient = useQueryClient()
793 const data = queryClient.getQueryData(['todos'] as const)
794 return null
795 }
796 `,
797 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
798 },
799 {
800 name: 'queryClient.getQueryData with inline queryKey satisfies',
801 code: normalizeIndent`
802 import { useQueryClient } from '@tanstack/react-query'
803​
804 function Component() {
805 const queryClient = useQueryClient()
806 const data = queryClient.getQueryData((['todos']) satisfies readonly string[])
807 return null
808 }
809 `,
810 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
811 },
812 {
813 name: 'queryClient.setQueryData with inline queryKey',
814 code: normalizeIndent`
815 import { useQueryClient } from '@tanstack/react-query'
816​
817 function Component() {
818 const queryClient = useQueryClient()
819 queryClient.setQueryData(['todos'], [])
820 return null
821 }
822 `,
823 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
824 },
825 {
826 name: 'queryClient.getQueryState with inline queryKey',
827 code: normalizeIndent`
828 import { useQueryClient } from '@tanstack/react-query'
829​
830 function Component() {
831 const queryClient = useQueryClient()
832 const state = queryClient.getQueryState(['todos'])
833 return null
834 }
835 `,
836 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
837 },
838 {
839 name: 'queryClient.setQueryDefaults with inline queryKey',
840 code: normalizeIndent`
841 import { useQueryClient } from '@tanstack/react-query'
842​
843 function Component() {
844 const queryClient = useQueryClient()
845 queryClient.setQueryDefaults(['todos'], { staleTime: 1000 })
846 return null
847 }
848 `,
849 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
850 },
851 {
852 name: 'queryClient.getQueryDefaults with inline queryKey',
853 code: normalizeIndent`
854 import { useQueryClient } from '@tanstack/react-query'
855​
856 function Component() {
857 const queryClient = useQueryClient()
858 const defaults = queryClient.getQueryDefaults(['todos'])
859 return null
860 }
861 `,
862 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
863 },
864 ],
865 })
866 })
867​
868 describe('inline queryKey in filter objects', () => {
869 ruleTester.run('prefer-query-options', rule, {
870 valid: [],
871 invalid: [
872 {
873 name: 'queryClient.invalidateQueries with inline queryKey in filters',
874 code: normalizeIndent`
875 import { useQueryClient } from '@tanstack/react-query'
876​
877 function Component() {
878 const queryClient = useQueryClient()
879 queryClient.invalidateQueries({ queryKey: ['todos'] })
880 return null
881 }
882 `,
883 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
884 },
885 {
886 name: 'queryClient.invalidateQueries with inline queryKey as const in filters',
887 code: normalizeIndent`
888 import { useQueryClient } from '@tanstack/react-query'
889​
890 function Component() {
891 const queryClient = useQueryClient()
892 queryClient.invalidateQueries({ queryKey: ['todos'] as const })
893 return null
894 }
895 `,
896 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
897 },
898 {
899 name: 'queryClient.invalidateQueries with inline queryKey satisfies in filters',
900 code: normalizeIndent`
901 import { useQueryClient } from '@tanstack/react-query'
902​
903 function Component() {
904 const queryClient = useQueryClient()
905 queryClient.invalidateQueries({
906 queryKey: (['todos']) satisfies readonly string[],
907 })
908 return null
909 }
910 `,
911 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
912 },
913 {
914 name: 'queryClient.cancelQueries with inline queryKey in filters',
915 code: normalizeIndent`
916 import { useQueryClient } from '@tanstack/react-query'
917​
918 function Component() {
919 const queryClient = useQueryClient()
920 queryClient.cancelQueries({ queryKey: ['todos'] })
921 return null
922 }
923 `,
924 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
925 },
926 {
927 name: 'queryClient.refetchQueries with inline queryKey in filters',
928 code: normalizeIndent`
929 import { useQueryClient } from '@tanstack/react-query'
930​
931 function Component() {
932 const queryClient = useQueryClient()
933 queryClient.refetchQueries({ queryKey: ['todos'] })
934 return null
935 }
936 `,
937 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
938 },
939 {
940 name: 'queryClient.removeQueries with inline queryKey in filters',
941 code: normalizeIndent`
942 import { useQueryClient } from '@tanstack/react-query'
943​
944 function Component() {
945 const queryClient = useQueryClient()
946 queryClient.removeQueries({ queryKey: ['todos'] })
947 return null
948 }
949 `,
950 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
951 },
952 {
953 name: 'queryClient.resetQueries with inline queryKey in filters',
954 code: normalizeIndent`
955 import { useQueryClient } from '@tanstack/react-query'
956​
957 function Component() {
958 const queryClient = useQueryClient()
959 queryClient.resetQueries({ queryKey: ['todos'] })
960 return null
961 }
962 `,
963 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
964 },
965 {
966 name: 'queryClient.isFetching with inline queryKey in filters',
967 code: normalizeIndent`
968 import { useQueryClient } from '@tanstack/react-query'
969​
970 function Component() {
971 const queryClient = useQueryClient()
972 const count = queryClient.isFetching({ queryKey: ['todos'] })
973 return null
974 }
975 `,
976 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
977 },
978 {
979 name: 'queryClient.getQueriesData with inline queryKey in filters',
980 code: normalizeIndent`
981 import { useQueryClient } from '@tanstack/react-query'
982​
983 function Component() {
984 const queryClient = useQueryClient()
985 const data = queryClient.getQueriesData({ queryKey: ['todos'] })
986 return null
987 }
988 `,
989 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
990 },
991 {
992 name: 'queryClient.setQueriesData with inline queryKey in filters',
993 code: normalizeIndent`
994 import { useQueryClient } from '@tanstack/react-query'
995​
996 function Component() {
997 const queryClient = useQueryClient()
998 queryClient.setQueriesData({ queryKey: ['todos'] }, [])
999 return null
1001 `,
1002 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
1003 },
1005 name: 'useIsFetching with inline queryKey in filters',
1006 code: normalizeIndent`
1007 import { useIsFetching } from '@tanstack/react-query'
1008​
1009 function Component() {
1010 const count = useIsFetching({ queryKey: ['todos'] })
1011 return null
1013 `,
1014 errors: [{ messageId: 'preferQueryOptionsQueryKey' }],
1015 },
1016 ],
1017 })
1018 })