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

stable-query-client.test.ts

195 lines5.3 KB

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 parser: '@typescript-eslint/parser',
7 settings: {},
8})
9​
10ruleTester.run('stable-query-client', rule, {
11 valid: [
12 {
13 name: 'QueryClient is stable when wrapped in React.useState',
14 code: normalizeIndent`
15 import { QueryClient } from "@tanstack/react-query";
16​
17 function Component() {
18 const [queryClient] = React.useState(() => new QueryClient());
19 return;
20 }
21 `,
22 },
23 {
24 name: 'QueryClient is stable when wrapped in useState',
25 code: normalizeIndent`
26 import { QueryClient } from "@tanstack/react-query";
27​
28 function Component() {
29 const [queryClient] = useState(() => new QueryClient());
30 return;
31 }
32 `,
33 },
34 {
35 name: 'QueryClient is stable when wrapped in React.useMemo',
36 code: normalizeIndent`
37 import { QueryClient } from "@tanstack/react-query";
39 function Component() {
40 const [queryClient] = React.useMemo(() => new QueryClient(), []);
41 return;
42 }
43 `,
44 },
45 {
46 name: 'QueryClient is stable when wrapped in useAnything',
47 code: normalizeIndent`
48 import { QueryClient } from "@tanstack/react-query";
50 function Component() {
51 const [queryClient] = useAnything(() => new QueryClient());
52 return;
53 }
54 `,
55 },
56 {
57 name: 'QueryClient is imported from a non-tanstack package',
58 code: normalizeIndent`
59 import { QueryClient } from "other-library";
60​
61 function Component() {
62 const queryClient = new QueryClient();
63 return;
64 }
65 `,
66 },
67 {
68 name: 'QueryClient is not imported from @tanstack/react-query',
69 code: normalizeIndent`
70 import { QueryClient } from "@tanstack/solid-query";
71​
72 function Component() {
73 const queryClient = new QueryClient();
74 return;
75 }
76 `,
77 },
78 {
79 name: 'QueryClient is invoked outside of a function',
80 code: normalizeIndent`
81 import { QueryClient } from "@tanstack/solid-query";
82​
83 const queryClient = new QueryClient();
84​
85 function Component() {
86 return;
87 }
88 `,
89 },
90 {
91 name: 'QueryClient is invoked in a non-component function',
92 code: normalizeIndent`
93 import { QueryClient } from "@tanstack/solid-query";
94​
95 function someFn() {
96 const queryClient = new QueryClient();
97 return;
98 }
99 `,
100 },
101 {
102 name: 'QueryClient is invoked in an async (react server) component',
103 code: normalizeIndent`
104 import { QueryClient } from "@tanstack/solid-query";
105​
106 async function AsyncComponent() {
107 const queryClient = new QueryClient();
108 return;
109 }
110 `,
111 },
112 ],
113 invalid: [
114 {
115 name: 'QueryClient is not stable when it is not wrapped in React.useState in component',
116 code: normalizeIndent`
117 import { QueryClient } from "@tanstack/react-query";
118​
119 function Component() {
120 const queryClient = new QueryClient();
121 return;
122 }
123 `,
124 output: normalizeIndent`
125 import { QueryClient } from "@tanstack/react-query";
126​
127 function Component() {
128 const [queryClient] = React.useState(() => new QueryClient());
129 return;
130 }
131 `,
132 errors: [{ messageId: 'unstable' }],
133 },
134 {
135 name: 'QueryClient is not stable when it is not wrapped in React.useState in custom hook',
136 code: normalizeIndent`
137 import { QueryClient } from "@tanstack/react-query";
138​
139 function useHook() {
140 const queryClient = new QueryClient();
141 return;
142 }
143 `,
144 output: normalizeIndent`
145 import { QueryClient } from "@tanstack/react-query";
146​
147 function useHook() {
148 const [queryClient] = React.useState(() => new QueryClient());
149 return;
150 }
151 `,
152 errors: [{ messageId: 'unstable' }],
153 },
154 {
155 name: 'preserve QueryClient options',
156 code: normalizeIndent`
157 import { QueryClient } from "@tanstack/react-query";
158​
159 function Component() {
160 const queryClient = new QueryClient({ defaultOptions: { /* */ } });
161 return;
162 }
163 `,
164 output: normalizeIndent`
165 import { QueryClient } from "@tanstack/react-query";
166​
167 function Component() {
168 const [queryClient] = React.useState(() => new QueryClient({ defaultOptions: { /* */ } }));
169 return;
170 }
171 `,
172 errors: [{ messageId: 'unstable' }],
173 },
174 {
175 name: 'preserve QueryClient variable declarator name',
176 code: normalizeIndent`
177 import { QueryClient } from "@tanstack/react-query";
178​
179 function Component() {
180 const customName = new QueryClient();
181 return;
182 }
183 `,
184 output: normalizeIndent`
185 import { QueryClient } from "@tanstack/react-query";
186​
187 function Component() {
188 const [customName] = React.useState(() => new QueryClient());
189 return;
190 }
191 `,
192 errors: [{ messageId: 'unstable' }],
193 },
194 ],
195})