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 .concat([
68 {
69 name: `should pass when useQuery is imported from non-TanStack source and used with ${reactHookAlias}`,
70 code: `
71 ${reactHookImport}
72 import { useQuery } from "./router";
73β
74 function Component() {
75 const query = useQuery();
76 const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]);
77 return;
78 }
79 `,
80 },
81 {
82 name: `should pass when useMutation is imported from non-TanStack source and used with ${reactHookAlias}`,
83 code: `
84 ${reactHookImport}
85 import { useMutation } from "./api";
86β
87 function Component() {
88 const mutation = useMutation();
89 const callback = ${reactHookInvocation}(() => { mutation.mutate() }, [mutation]);
90 return;
91 }
92 `,
93 },
94 ]),
95 invalid: ({
96 reactHookImport,
97 reactHookInvocation,
98 reactHookAlias,
99 }: TestCase) =>
100 [
101 {
102 name: `result of useMutation is passed to ${reactHookInvocation} as dependency `,
103 code: `
104 ${reactHookImport}
105 import { useMutation } from "@tanstack/react-query";
106β
107 function Component() {
108 const mutation = useMutation({ mutationFn: (value: string) => value });
109 const callback = ${reactHookInvocation}(() => { mutation.mutate('hello') }, [mutation]);
110 return;
111 }
112 `,
113 errors: [
114 {
115 messageId: 'noUnstableDeps',
116 data: { reactHook: reactHookAlias, queryHook: 'useMutation' },
117 },
118 ],
119 },
120 ]
121 .concat(
122 useQueryHookNames.map((queryHook) => ({
123 name: `result of ${queryHook} is passed to ${reactHookInvocation} as dependency`,
124 code: `
125 ${reactHookImport}
126 import { ${queryHook} } from "@tanstack/react-query";
127β
128 function Component() {
129 const query = ${queryHook}({ queryFn: (value: string) => value });
130 const callback = ${reactHookInvocation}(() => { query.refetch() }, [query]);
131 return;
132 }
133 `,
134 errors: [
135 {
136 messageId: 'noUnstableDeps',
137 data: { reactHook: reactHookAlias, queryHook },
138 },
139 ],
140 })),
141 )
142 .concat([
143 {
144 name: `result of useQueries without combine is passed to ${reactHookInvocation} as dependency`,
145 code: `
146 ${reactHookImport}
147 import { useQueries } from "@tanstack/react-query";
148β
149 function Component() {
150 const queries = useQueries({
151 queries: [
152 { queryKey: ['test'], queryFn: () => 'test' }
153 ]
154 });
155 const callback = ${reactHookInvocation}(() => { queries[0]?.data }, [queries]);
156 return;
157 }
158 `,
159 errors: [
160 {
161 messageId: 'noUnstableDeps',
162 data: { reactHook: reactHookAlias, queryHook: 'useQueries' },
163 },
164 ],
165 },
166 ]),
167}
168β
169const testCases = (reactHookName: string) => [
170 {
171 reactHookImport: 'import * as React from "React";',
172 reactHookInvocation: `React.${reactHookName}`,
173 reactHookAlias: reactHookName,
174 },
175 {
176 reactHookImport: `import { ${reactHookName} } from "React";`,
177 reactHookInvocation: reactHookName,
178 reactHookAlias: reactHookName,
179 },
180 {
181 reactHookImport: `import { ${reactHookName} as useAlias } from "React";`,
182 reactHookInvocation: 'useAlias',
183 reactHookAlias: 'useAlias',
184 },
185]
186β
187reactHookNames.forEach((reactHookName) => {
188 testCases(reactHookName).forEach(
189 ({ reactHookInvocation, reactHookAlias, reactHookImport }) => {
190 ruleTester.run('no-unstable-deps', rule, {
191 valid: baseTestCases.valid({
192 reactHookImport,
193 reactHookInvocation,
194 reactHookAlias,
195 }),
196 invalid: baseTestCases.invalid({
197 reactHookImport,
198 reactHookInvocation,
199 reactHookAlias,
200 }),
201 })
202 },
203 )
204})