Open History to see the last commit
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 invoked in an async (react server) 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 invalid: [
111 {
112 name: 'QueryClient is not stable when it is not wrapped in React.useState in component',
113 code: normalizeIndent`
114 import { QueryClient } from "@tanstack/react-query";
115β
116 function Component() {
117 const queryClient = new QueryClient();
118 return;
119 }
120 `,
121 output: normalizeIndent`
122 import { QueryClient } from "@tanstack/react-query";
123β
124 function Component() {
125 const [queryClient] = React.useState(() => new QueryClient());
126 return;
127 }
128 `,
129 errors: [{ messageId: 'unstable' }],
130 },
131 {
132 name: 'QueryClient is not stable when it is not wrapped in React.useState in custom hook',
133 code: normalizeIndent`
134 import { QueryClient } from "@tanstack/react-query";
135β
136 function useHook() {
137 const queryClient = new QueryClient();
138 return;
139 }
140 `,
141 output: normalizeIndent`
142 import { QueryClient } from "@tanstack/react-query";
143β
144 function useHook() {
145 const [queryClient] = React.useState(() => new QueryClient());
146 return;
147 }
148 `,
149 errors: [{ messageId: 'unstable' }],
150 },
151 {
152 name: 'preserve QueryClient options',
153 code: normalizeIndent`
154 import { QueryClient } from "@tanstack/react-query";
155β
156 function Component() {
157 const queryClient = new QueryClient({ defaultOptions: { /* */ } });
158 return;
159 }
160 `,
161 output: normalizeIndent`
162 import { QueryClient } from "@tanstack/react-query";
163β
164 function Component() {
165 const [queryClient] = React.useState(() => new QueryClient({ defaultOptions: { /* */ } }));
166 return;
167 }
168 `,
169 errors: [{ messageId: 'unstable' }],
170 },
171 {
172 name: 'preserve QueryClient variable declarator name',
173 code: normalizeIndent`
174 import { QueryClient } from "@tanstack/react-query";
175β
176 function Component() {
177 const customName = new QueryClient();
178 return;
179 }
180 `,
181 output: normalizeIndent`
182 import { QueryClient } from "@tanstack/react-query";
183β
184 function Component() {
185 const [customName] = React.useState(() => new QueryClient());
186 return;
187 }
188 `,
189 errors: [{ messageId: 'unstable' }],
190 },
191 ],
192})