@tanstack/angular-query-experimental 5.103.1 → 5.103.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/dist/create-base-query.d.mts +169 -0
  2. package/dist/create-base-query.d.mts.map +1 -0
  3. package/dist/create-base-query.mjs +91 -0
  4. package/dist/create-base-query.mjs.map +1 -0
  5. package/dist/devtools/index.d.mts +3 -0
  6. package/dist/devtools/index.mjs +2 -0
  7. package/dist/devtools/production/index.d.mts +4 -0
  8. package/dist/devtools/production/index.mjs +2 -0
  9. package/dist/devtools/stub.d.mts +6 -0
  10. package/dist/devtools/stub.d.mts.map +1 -0
  11. package/dist/devtools/stub.mjs +9 -0
  12. package/dist/devtools/stub.mjs.map +1 -0
  13. package/dist/devtools/types.d.mts +107 -0
  14. package/dist/devtools/types.d.mts.map +1 -0
  15. package/dist/devtools/types.mjs +1 -0
  16. package/{devtools/with-devtools.d.ts → dist/devtools/with-devtools.d.mts} +6 -2
  17. package/dist/devtools/with-devtools.d.mts.map +1 -0
  18. package/dist/devtools/with-devtools.mjs +110 -0
  19. package/dist/devtools/with-devtools.mjs.map +1 -0
  20. package/dist/devtools-panel/index.d.mts +3 -0
  21. package/dist/devtools-panel/index.mjs +2 -0
  22. package/{devtools-panel/inject-devtools-panel.d.ts → dist/devtools-panel/inject-devtools-panel.d.mts} +6 -2
  23. package/dist/devtools-panel/inject-devtools-panel.d.mts.map +1 -0
  24. package/dist/devtools-panel/inject-devtools-panel.mjs +67 -0
  25. package/dist/devtools-panel/inject-devtools-panel.mjs.map +1 -0
  26. package/dist/devtools-panel/production/index.d.mts +4 -0
  27. package/dist/devtools-panel/production/index.mjs +2 -0
  28. package/dist/devtools-panel/stub.d.mts +6 -0
  29. package/dist/devtools-panel/stub.d.mts.map +1 -0
  30. package/dist/devtools-panel/stub.mjs +7 -0
  31. package/dist/devtools-panel/stub.mjs.map +1 -0
  32. package/dist/devtools-panel/types.d.mts +52 -0
  33. package/dist/devtools-panel/types.d.mts.map +1 -0
  34. package/dist/devtools-panel/types.mjs +1 -0
  35. package/dist/index.d.mts +16 -0
  36. package/{index.mjs → dist/index.mjs} +8 -21
  37. package/dist/index.mjs.map +1 -0
  38. package/{infinite-query-options.d.ts → dist/infinite-query-options.d.mts} +35 -31
  39. package/dist/infinite-query-options.d.mts.map +1 -0
  40. package/dist/infinite-query-options.mjs +8 -0
  41. package/dist/infinite-query-options.mjs.map +1 -0
  42. package/{inject-infinite-query.d.ts → dist/inject-infinite-query.d.mts} +18 -14
  43. package/dist/inject-infinite-query.d.mts.map +1 -0
  44. package/dist/inject-infinite-query.mjs +13 -0
  45. package/dist/inject-infinite-query.mjs.map +1 -0
  46. package/{inject-is-fetching.d.ts → dist/inject-is-fetching.d.mts} +14 -10
  47. package/dist/inject-is-fetching.d.mts.map +1 -0
  48. package/dist/inject-is-fetching.mjs +69 -0
  49. package/dist/inject-is-fetching.mjs.map +1 -0
  50. package/{inject-is-mutating.d.ts → dist/inject-is-mutating.d.mts} +14 -10
  51. package/dist/inject-is-mutating.d.mts.map +1 -0
  52. package/dist/inject-is-mutating.mjs +52 -0
  53. package/dist/inject-is-mutating.mjs.map +1 -0
  54. package/{inject-is-restoring.d.ts → dist/inject-is-restoring.d.mts} +13 -10
  55. package/dist/inject-is-restoring.d.mts.map +1 -0
  56. package/dist/inject-is-restoring.mjs +35 -0
  57. package/dist/inject-is-restoring.mjs.map +1 -0
  58. package/{inject-mutation-state.d.ts → dist/inject-mutation-state.d.mts} +16 -13
  59. package/dist/inject-mutation-state.d.mts.map +1 -0
  60. package/dist/inject-mutation-state.mjs +103 -0
  61. package/dist/inject-mutation-state.mjs.map +1 -0
  62. package/{inject-mutation.d.ts → dist/inject-mutation.d.mts} +15 -11
  63. package/dist/inject-mutation.d.mts.map +1 -0
  64. package/dist/inject-mutation.mjs +226 -0
  65. package/dist/inject-mutation.mjs.map +1 -0
  66. package/dist/inject-queries-experimental/index.d.mts +2 -0
  67. package/dist/inject-queries-experimental/index.mjs +2 -0
  68. package/{inject-queries.d.ts → dist/inject-queries.d.mts} +36 -55
  69. package/dist/inject-queries.d.mts.map +1 -0
  70. package/dist/inject-queries.mjs +203 -0
  71. package/dist/inject-queries.mjs.map +1 -0
  72. package/{inject-query-client.d.ts → dist/inject-query-client.d.mts} +8 -4
  73. package/dist/inject-query-client.d.mts.map +1 -0
  74. package/dist/inject-query-client.mjs +23 -0
  75. package/dist/inject-query-client.mjs.map +1 -0
  76. package/{inject-query.d.ts → dist/inject-query.d.mts} +18 -14
  77. package/dist/inject-query.d.mts.map +1 -0
  78. package/dist/inject-query.mjs +12 -0
  79. package/dist/inject-query.mjs.map +1 -0
  80. package/{mutation-options.d.ts → dist/mutation-options.d.mts} +8 -4
  81. package/dist/mutation-options.d.mts.map +1 -0
  82. package/dist/mutation-options.mjs +8 -0
  83. package/dist/mutation-options.mjs.map +1 -0
  84. package/dist/pending-tasks-compat.d.mts +10 -0
  85. package/dist/pending-tasks-compat.d.mts.map +1 -0
  86. package/dist/pending-tasks-compat.mjs +13 -0
  87. package/dist/pending-tasks-compat.mjs.map +1 -0
  88. package/{providers.d.ts → dist/providers.d.mts} +16 -13
  89. package/dist/providers.d.mts.map +1 -0
  90. package/dist/providers.mjs +124 -0
  91. package/dist/providers.mjs.map +1 -0
  92. package/{query-options.d.ts → dist/query-options.d.mts} +41 -37
  93. package/dist/query-options.d.mts.map +1 -0
  94. package/dist/query-options.mjs +8 -0
  95. package/dist/query-options.mjs.map +1 -0
  96. package/dist/signal-proxy.d.mts +13 -0
  97. package/dist/signal-proxy.d.mts.map +1 -0
  98. package/dist/signal-proxy.mjs +35 -0
  99. package/dist/signal-proxy.mjs.map +1 -0
  100. package/{types.d.ts → dist/types.d.mts} +36 -37
  101. package/dist/types.d.mts.map +1 -0
  102. package/dist/types.mjs +6 -0
  103. package/dist/types.mjs.map +1 -0
  104. package/package.json +48 -20
  105. package/src/__tests__/infinite-query-options.test-d.ts +314 -0
  106. package/src/__tests__/infinite-query-options.test.ts +19 -0
  107. package/src/__tests__/inject-devtools-panel.test.ts +196 -0
  108. package/src/__tests__/inject-infinite-query.test-d.ts +66 -0
  109. package/src/__tests__/inject-infinite-query.test.ts +227 -0
  110. package/src/__tests__/inject-is-fetching.test.ts +108 -0
  111. package/src/__tests__/inject-is-mutating.test.ts +113 -0
  112. package/src/__tests__/inject-is-restoring.test.ts +93 -0
  113. package/src/__tests__/inject-mutation-state.test-d.ts +22 -0
  114. package/src/__tests__/inject-mutation-state.test.ts +198 -0
  115. package/src/__tests__/inject-mutation.test-d.ts +158 -0
  116. package/src/__tests__/inject-mutation.test.ts +1161 -0
  117. package/src/__tests__/inject-queries.test-d.ts +196 -0
  118. package/src/__tests__/inject-queries.test.ts +240 -0
  119. package/src/__tests__/inject-query.test-d.ts +357 -0
  120. package/src/__tests__/inject-query.test.ts +700 -0
  121. package/src/__tests__/mutation-options.test-d.ts +234 -0
  122. package/src/__tests__/mutation-options.test.ts +344 -0
  123. package/src/__tests__/pending-tasks.test.ts +644 -0
  124. package/src/__tests__/provide-query-client.test.ts +38 -0
  125. package/src/__tests__/provide-tanstack-query.test.ts +38 -0
  126. package/src/__tests__/query-options.test-d.ts +262 -0
  127. package/src/__tests__/query-options.test.ts +14 -0
  128. package/src/__tests__/signal-proxy.test.ts +27 -0
  129. package/src/__tests__/test-utils.ts +77 -0
  130. package/src/__tests__/with-devtools.test.ts +674 -0
  131. package/src/create-base-query.ts +175 -0
  132. package/src/devtools/index.ts +8 -0
  133. package/src/devtools/production/index.ts +1 -0
  134. package/src/devtools/stub.ts +7 -0
  135. package/src/devtools/types.ts +116 -0
  136. package/src/devtools/with-devtools.ts +182 -0
  137. package/src/devtools-panel/index.ts +8 -0
  138. package/src/devtools-panel/inject-devtools-panel.ts +110 -0
  139. package/src/devtools-panel/production/index.ts +1 -0
  140. package/src/devtools-panel/stub.ts +7 -0
  141. package/src/devtools-panel/types.ts +57 -0
  142. package/src/index.ts +60 -0
  143. package/src/infinite-query-options.ts +354 -0
  144. package/src/inject-infinite-query.ts +296 -0
  145. package/src/inject-is-fetching.ts +97 -0
  146. package/src/inject-is-mutating.ts +80 -0
  147. package/src/inject-is-restoring.ts +53 -0
  148. package/src/inject-mutation-state.ts +167 -0
  149. package/src/inject-mutation.ts +328 -0
  150. package/src/inject-queries-experimental/index.ts +1 -0
  151. package/src/inject-queries.ts +479 -0
  152. package/src/inject-query-client.ts +22 -0
  153. package/src/inject-query.ts +208 -0
  154. package/src/mutation-options.ts +121 -0
  155. package/src/pending-tasks-compat.ts +27 -0
  156. package/src/providers.ts +170 -0
  157. package/src/query-options.ts +292 -0
  158. package/src/signal-proxy.ts +46 -0
  159. package/src/types.ts +429 -0
  160. package/create-base-query.d.ts +0 -164
  161. package/create-base-query.mjs +0 -99
  162. package/create-base-query.mjs.map +0 -1
  163. package/devtools/index.d.ts +0 -2
  164. package/devtools/index.mjs +0 -5
  165. package/devtools/index.mjs.map +0 -1
  166. package/devtools/production/index.d.ts +0 -1
  167. package/devtools/stub.d.ts +0 -2
  168. package/devtools/stub.mjs +0 -8
  169. package/devtools/stub.mjs.map +0 -1
  170. package/devtools/types.d.ts +0 -100
  171. package/devtools/with-devtools.mjs +0 -106
  172. package/devtools/with-devtools.mjs.map +0 -1
  173. package/devtools-panel/index.d.ts +0 -2
  174. package/devtools-panel/index.mjs +0 -5
  175. package/devtools-panel/index.mjs.map +0 -1
  176. package/devtools-panel/inject-devtools-panel.mjs +0 -72
  177. package/devtools-panel/inject-devtools-panel.mjs.map +0 -1
  178. package/devtools-panel/production/index.d.ts +0 -1
  179. package/devtools-panel/stub.d.ts +0 -2
  180. package/devtools-panel/stub.mjs +0 -8
  181. package/devtools-panel/stub.mjs.map +0 -1
  182. package/devtools-panel/types.d.ts +0 -48
  183. package/index.d.ts +0 -25
  184. package/index.mjs.map +0 -1
  185. package/infinite-query-options.mjs +0 -7
  186. package/infinite-query-options.mjs.map +0 -1
  187. package/inject-infinite-query.mjs +0 -18
  188. package/inject-infinite-query.mjs.map +0 -1
  189. package/inject-is-fetching.mjs +0 -31
  190. package/inject-is-fetching.mjs.map +0 -1
  191. package/inject-is-mutating.mjs +0 -31
  192. package/inject-is-mutating.mjs.map +0 -1
  193. package/inject-is-restoring.mjs +0 -21
  194. package/inject-is-restoring.mjs.map +0 -1
  195. package/inject-mutation-state.mjs +0 -51
  196. package/inject-mutation-state.mjs.map +0 -1
  197. package/inject-mutation.mjs +0 -94
  198. package/inject-mutation.mjs.map +0 -1
  199. package/inject-queries-experimental/index.d.ts +0 -1
  200. package/inject-queries-experimental/index.mjs +0 -5
  201. package/inject-queries-experimental/index.mjs.map +0 -1
  202. package/inject-queries.mjs +0 -82
  203. package/inject-queries.mjs.map +0 -1
  204. package/inject-query-client.mjs +0 -9
  205. package/inject-query-client.mjs.map +0 -1
  206. package/inject-query.mjs +0 -14
  207. package/inject-query.mjs.map +0 -1
  208. package/mutation-options.mjs +0 -7
  209. package/mutation-options.mjs.map +0 -1
  210. package/pending-tasks-compat.d.ts +0 -7
  211. package/pending-tasks-compat.mjs +0 -19
  212. package/pending-tasks-compat.mjs.map +0 -1
  213. package/providers.mjs +0 -32
  214. package/providers.mjs.map +0 -1
  215. package/query-options.mjs +0 -7
  216. package/query-options.mjs.map +0 -1
  217. package/signal-proxy.d.ts +0 -11
  218. package/signal-proxy.mjs +0 -29
  219. package/signal-proxy.mjs.map +0 -1
@@ -0,0 +1,314 @@
1
+ import { assertType, describe, expectTypeOf, it } from 'vitest'
2
+ import { queryKey } from '@tanstack/query-test-utils'
3
+ import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
4
+ import { infiniteQueryOptions } from '../infinite-query-options'
5
+ import { injectInfiniteQuery } from '../inject-infinite-query'
6
+ import { injectQuery } from '../inject-query'
7
+ import type {
8
+ DataTag,
9
+ InfiniteData,
10
+ InitialDataFunction,
11
+ } from '@tanstack/query-core'
12
+
13
+ // Regression test for exported infiniteQueryOptions inference under declaration emit.
14
+ // TypeScript should be able to name the return type without expanding the
15
+ // internal data tag symbols into the consumer's .d.ts output.
16
+ export const exportedInfiniteQueryOptions = infiniteQueryOptions({
17
+ queryKey: ['invalid'],
18
+ getNextPageParam: () => 1,
19
+ initialPageParam: 1,
20
+ })
21
+
22
+ describe('infiniteQueryOptions', () => {
23
+ it('should not allow excess properties', () => {
24
+ const key = queryKey()
25
+ assertType(
26
+ infiniteQueryOptions({
27
+ queryKey: key,
28
+ queryFn: () => Promise.resolve('data'),
29
+ getNextPageParam: () => 1,
30
+ initialPageParam: 1,
31
+ // @ts-expect-error this is a good error, because stallTime does not exist!
32
+ stallTime: 1000,
33
+ }),
34
+ )
35
+ })
36
+ it('should infer types for callbacks', () => {
37
+ const key = queryKey()
38
+ infiniteQueryOptions({
39
+ queryKey: key,
40
+ queryFn: () => Promise.resolve('data'),
41
+ staleTime: 1000,
42
+ getNextPageParam: () => 1,
43
+ initialPageParam: 1,
44
+ select: (data) => {
45
+ expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
46
+ },
47
+ })
48
+ })
49
+ it('should work when passed to useInfiniteQuery', () => {
50
+ const key = queryKey()
51
+ const options = infiniteQueryOptions({
52
+ queryKey: key,
53
+ queryFn: () => Promise.resolve('string'),
54
+ getNextPageParam: () => 1,
55
+ initialPageParam: 1,
56
+ })
57
+
58
+ const { data } = injectInfiniteQuery(() => options)
59
+
60
+ // known issue: type of pageParams is unknown when returned from useInfiniteQuery
61
+ expectTypeOf(data()).toEqualTypeOf<
62
+ InfiniteData<string, unknown> | undefined
63
+ >()
64
+ })
65
+
66
+ it('should work when passed to fetchInfiniteQuery', async () => {
67
+ const key = queryKey()
68
+ const options = infiniteQueryOptions({
69
+ queryKey: key,
70
+ queryFn: () => Promise.resolve('string'),
71
+ getNextPageParam: () => 1,
72
+ initialPageParam: 1,
73
+ })
74
+
75
+ // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
76
+ const data = await new QueryClient().fetchInfiniteQuery(options)
77
+
78
+ expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
79
+ })
80
+
81
+ it('should work when passed to infiniteQuery', async () => {
82
+ const options = infiniteQueryOptions({
83
+ queryKey: ['key'],
84
+ queryFn: () => Promise.resolve('string'),
85
+ getNextPageParam: () => 1,
86
+ initialPageParam: 1,
87
+ })
88
+
89
+ const data = await new QueryClient().infiniteQuery(options)
90
+
91
+ expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
92
+ })
93
+
94
+ it('should work when passed to infiniteQuery with select', async () => {
95
+ const options = infiniteQueryOptions({
96
+ queryKey: ['key'],
97
+ queryFn: () => Promise.resolve('string'),
98
+ getNextPageParam: () => 1,
99
+ initialPageParam: 1,
100
+ select: (data) => data.pages,
101
+ })
102
+
103
+ const data = await new QueryClient().infiniteQuery(options)
104
+
105
+ expectTypeOf(data).toEqualTypeOf<Array<string>>()
106
+ })
107
+
108
+ it('should work when passed to infiniteQuery with enabled: false', async () => {
109
+ const options = infiniteQueryOptions({
110
+ queryKey: ['key'],
111
+ queryFn: () => Promise.resolve('string'),
112
+ getNextPageParam: () => 1,
113
+ initialPageParam: 1,
114
+ enabled: false,
115
+ })
116
+
117
+ const data = await new QueryClient().infiniteQuery(options)
118
+
119
+ expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
120
+ })
121
+
122
+ it('should work when passed to infiniteQuery with skipToken', async () => {
123
+ const options = infiniteQueryOptions({
124
+ queryKey: ['key'],
125
+ queryFn: skipToken,
126
+ getNextPageParam: () => 1,
127
+ initialPageParam: 1,
128
+ })
129
+
130
+ const data = await new QueryClient().infiniteQuery(options)
131
+
132
+ expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
133
+ })
134
+
135
+ it('should tag the queryKey with the result type of the QueryFn', () => {
136
+ const key = queryKey()
137
+ const { queryKey: tagged } = infiniteQueryOptions({
138
+ queryKey: key,
139
+ queryFn: () => Promise.resolve('string'),
140
+ getNextPageParam: () => 1,
141
+ initialPageParam: 1,
142
+ })
143
+
144
+ expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
145
+ })
146
+ it('should tag the queryKey even if no promise is returned', () => {
147
+ const key = queryKey()
148
+ const { queryKey: tagged } = infiniteQueryOptions({
149
+ queryKey: key,
150
+ queryFn: () => 'string',
151
+ getNextPageParam: () => 1,
152
+ initialPageParam: 1,
153
+ })
154
+
155
+ expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
156
+ })
157
+ it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
158
+ const key = queryKey()
159
+ const { queryKey: tagged } = infiniteQueryOptions({
160
+ queryKey: key,
161
+ queryFn: () => Promise.resolve('string'),
162
+ select: (data) => data.pages,
163
+ getNextPageParam: () => 1,
164
+ initialPageParam: 1,
165
+ })
166
+
167
+ expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
168
+ })
169
+ it('should return the proper type when passed to getQueryData', () => {
170
+ const key = queryKey()
171
+ const { queryKey: tagged } = infiniteQueryOptions({
172
+ queryKey: key,
173
+ queryFn: () => Promise.resolve('string'),
174
+ getNextPageParam: () => 1,
175
+ initialPageParam: 1,
176
+ })
177
+
178
+ const queryClient = new QueryClient()
179
+ const data = queryClient.getQueryData(tagged)
180
+
181
+ expectTypeOf(data).toEqualTypeOf<
182
+ InfiniteData<string, unknown> | undefined
183
+ >()
184
+ })
185
+ it('should properly type when passed to setQueryData', () => {
186
+ const key = queryKey()
187
+ const { queryKey: tagged } = infiniteQueryOptions({
188
+ queryKey: key,
189
+ queryFn: () => Promise.resolve('string'),
190
+ getNextPageParam: () => 1,
191
+ initialPageParam: 1,
192
+ })
193
+
194
+ const queryClient = new QueryClient()
195
+ const data = queryClient.setQueryData(tagged, (prev) => {
196
+ expectTypeOf(prev).toEqualTypeOf<
197
+ InfiniteData<string, unknown> | undefined
198
+ >()
199
+ return prev
200
+ })
201
+
202
+ expectTypeOf(data).toEqualTypeOf<
203
+ InfiniteData<string, unknown> | undefined
204
+ >()
205
+ })
206
+
207
+ it('should not be allowed to be passed to non-infinite query functions', () => {
208
+ const key = queryKey()
209
+ const queryClient = new QueryClient()
210
+ const options = infiniteQueryOptions({
211
+ queryKey: key,
212
+ queryFn: () => Promise.resolve('string'),
213
+ getNextPageParam: () => 1,
214
+ initialPageParam: 1,
215
+ })
216
+ assertType(
217
+ // @ts-expect-error cannot pass infinite options to non-infinite query functions
218
+ injectQuery(() => options),
219
+ )
220
+ assertType(
221
+ // @ts-expect-error cannot pass infinite options to non-infinite query functions
222
+ queryClient.query(options),
223
+ )
224
+
225
+ // deprecated methods below to be removed next major version
226
+ assertType(
227
+ // @ts-expect-error cannot pass infinite options to non-infinite query functions
228
+ // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
229
+ queryClient.ensureQueryData(options),
230
+ )
231
+ assertType(
232
+ // @ts-expect-error cannot pass infinite options to non-infinite query functions
233
+ // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
234
+ queryClient.fetchQuery(options),
235
+ )
236
+ assertType(
237
+ // @ts-expect-error cannot pass infinite options to non-infinite query functions
238
+ // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
239
+ queryClient.prefetchQuery(options),
240
+ )
241
+ })
242
+
243
+ it('should allow optional initialData function', () => {
244
+ const key = queryKey()
245
+ const initialData: { example: boolean } | undefined = { example: true }
246
+ const queryOptions = infiniteQueryOptions({
247
+ queryKey: key,
248
+ queryFn: () => initialData,
249
+ initialData: initialData
250
+ ? () => ({ pages: [initialData], pageParams: [] })
251
+ : undefined,
252
+ getNextPageParam: () => 1,
253
+ initialPageParam: 1,
254
+ })
255
+ expectTypeOf(queryOptions.initialData).toExtend<
256
+ | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
257
+ | InfiniteData<{ example: boolean }, number>
258
+ | undefined
259
+ >()
260
+ })
261
+
262
+ it('should allow optional initialData object', () => {
263
+ const key = queryKey()
264
+ const initialData: { example: boolean } | undefined = { example: true }
265
+ const queryOptions = infiniteQueryOptions({
266
+ queryKey: key,
267
+ queryFn: () => initialData,
268
+ initialData: initialData
269
+ ? { pages: [initialData], pageParams: [] }
270
+ : undefined,
271
+ getNextPageParam: () => 1,
272
+ initialPageParam: 1,
273
+ })
274
+ expectTypeOf(queryOptions.initialData).toExtend<
275
+ | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
276
+ | InfiniteData<{ example: boolean }, number>
277
+ | undefined
278
+ >()
279
+ })
280
+
281
+ it('should return a custom query key type', () => {
282
+ type MyQueryKey = [Array<string>, { type: 'foo' }]
283
+
284
+ const options = infiniteQueryOptions({
285
+ queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
286
+ queryFn: () => Promise.resolve(1),
287
+ getNextPageParam: () => 1,
288
+ initialPageParam: 1,
289
+ })
290
+
291
+ expectTypeOf(options.queryKey).toEqualTypeOf<
292
+ DataTag<MyQueryKey, InfiniteData<number>, Error>
293
+ >()
294
+ })
295
+
296
+ it('should return a custom query key type with datatag', () => {
297
+ type MyQueryKey = DataTag<
298
+ [Array<string>, { type: 'foo' }],
299
+ number,
300
+ Error & { myMessage: string }
301
+ >
302
+
303
+ const options = infiniteQueryOptions({
304
+ queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
305
+ queryFn: () => Promise.resolve(1),
306
+ getNextPageParam: () => 1,
307
+ initialPageParam: 1,
308
+ })
309
+
310
+ expectTypeOf(options.queryKey).toEqualTypeOf<
311
+ DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
312
+ >()
313
+ })
314
+ })
@@ -0,0 +1,19 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { queryKey } from '@tanstack/query-test-utils'
3
+
4
+ import { infiniteQueryOptions } from '../infinite-query-options'
5
+ import type { CreateInfiniteQueryOptions } from '../types'
6
+
7
+ describe('infiniteQueryOptions', () => {
8
+ it('should return the object received as a parameter without any modification.', () => {
9
+ const key = queryKey()
10
+ const object: CreateInfiniteQueryOptions = {
11
+ queryKey: key,
12
+ queryFn: () => Promise.resolve(5),
13
+ getNextPageParam: () => null,
14
+ initialPageParam: null,
15
+ }
16
+
17
+ expect(infiniteQueryOptions(object)).toBe(object)
18
+ })
19
+ })
@@ -0,0 +1,196 @@
1
+ import {
2
+ ElementRef,
3
+ provideZonelessChangeDetection,
4
+ signal,
5
+ } from '@angular/core'
6
+ import { TestBed } from '@angular/core/testing'
7
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
8
+ import { QueryClient } from '@tanstack/query-core'
9
+ import { provideTanStackQuery } from '../providers'
10
+ import { injectDevtoolsPanel } from '../devtools-panel'
11
+
12
+ const mockDevtoolsPanelInstance = {
13
+ mount: vi.fn(),
14
+ unmount: vi.fn(),
15
+ setClient: vi.fn(),
16
+ setErrorTypes: vi.fn(),
17
+ setOnClose: vi.fn(),
18
+ }
19
+
20
+ const mocks = vi.hoisted(() => {
21
+ function MockTanstackQueryDevtoolsPanel() {
22
+ return mockDevtoolsPanelInstance
23
+ }
24
+
25
+ return {
26
+ mockTanstackQueryDevtoolsPanel: vi.fn(MockTanstackQueryDevtoolsPanel),
27
+ }
28
+ })
29
+
30
+ vi.mock('@tanstack/query-devtools', () => ({
31
+ TanstackQueryDevtoolsPanel: mocks.mockTanstackQueryDevtoolsPanel,
32
+ }))
33
+
34
+ describe('injectDevtoolsPanel', () => {
35
+ let queryClient: QueryClient
36
+ let mockElementRef: ElementRef
37
+
38
+ const waitForDevtoolsToBeCreated = async () => {
39
+ await vi.waitFor(() => {
40
+ expect(mocks.mockTanstackQueryDevtoolsPanel).toHaveBeenCalledTimes(1)
41
+ })
42
+ }
43
+
44
+ beforeEach(() => {
45
+ vi.clearAllMocks()
46
+ queryClient = new QueryClient()
47
+ mockElementRef = new ElementRef(document.createElement('div'))
48
+ TestBed.configureTestingModule({
49
+ providers: [
50
+ provideZonelessChangeDetection(),
51
+ provideTanStackQuery(queryClient),
52
+ { provide: ElementRef, useValue: signal(mockElementRef) },
53
+ ],
54
+ })
55
+ })
56
+
57
+ afterEach(() => {
58
+ vi.restoreAllMocks()
59
+ })
60
+
61
+ it('should return a DevtoolsPanelRef', () => {
62
+ const result = TestBed.runInInjectionContext(() => {
63
+ return injectDevtoolsPanel(() => ({
64
+ hostElement: TestBed.inject(ElementRef),
65
+ }))
66
+ })
67
+
68
+ expect(result).toEqual({
69
+ destroy: expect.any(Function),
70
+ })
71
+ })
72
+
73
+ it('should initialize TanstackQueryDevtoolsPanel', async () => {
74
+ TestBed.runInInjectionContext(() => {
75
+ injectDevtoolsPanel(() => ({
76
+ hostElement: TestBed.inject(ElementRef),
77
+ }))
78
+ })
79
+
80
+ TestBed.tick()
81
+
82
+ await waitForDevtoolsToBeCreated()
83
+
84
+ expect(mockDevtoolsPanelInstance.mount).toHaveBeenCalledTimes(1)
85
+ })
86
+
87
+ it('should destroy TanstackQueryDevtoolsPanel', async () => {
88
+ const result = TestBed.runInInjectionContext(() => {
89
+ return injectDevtoolsPanel(() => ({
90
+ hostElement: TestBed.inject(ElementRef),
91
+ }))
92
+ })
93
+
94
+ TestBed.tick()
95
+
96
+ await waitForDevtoolsToBeCreated()
97
+
98
+ result.destroy()
99
+
100
+ expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
101
+ })
102
+
103
+ it('should destroy TanstackQueryDevtoolsPanel when hostElement is removed', async () => {
104
+ const hostElement = signal<ElementRef>(mockElementRef)
105
+
106
+ TestBed.runInInjectionContext(() => {
107
+ return injectDevtoolsPanel(() => ({
108
+ hostElement: hostElement(),
109
+ }))
110
+ })
111
+
112
+ TestBed.tick()
113
+
114
+ await waitForDevtoolsToBeCreated()
115
+
116
+ expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(0)
117
+
118
+ hostElement.set(null as unknown as ElementRef)
119
+
120
+ TestBed.tick()
121
+
122
+ expect(mockDevtoolsPanelInstance.unmount).toHaveBeenCalledTimes(1)
123
+ })
124
+
125
+ it('should update client', async () => {
126
+ const client = signal(new QueryClient())
127
+
128
+ TestBed.runInInjectionContext(() => {
129
+ return injectDevtoolsPanel(() => ({
130
+ hostElement: TestBed.inject(ElementRef),
131
+ client: client(),
132
+ }))
133
+ })
134
+
135
+ TestBed.tick()
136
+
137
+ await waitForDevtoolsToBeCreated()
138
+
139
+ expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(0)
140
+
141
+ client.set(new QueryClient())
142
+
143
+ TestBed.tick()
144
+
145
+ expect(mockDevtoolsPanelInstance.setClient).toHaveBeenCalledTimes(1)
146
+ })
147
+
148
+ it('should update error types', async () => {
149
+ const errorTypes = signal([])
150
+
151
+ TestBed.runInInjectionContext(() => {
152
+ return injectDevtoolsPanel(() => ({
153
+ hostElement: TestBed.inject(ElementRef),
154
+ errorTypes: errorTypes(),
155
+ }))
156
+ })
157
+
158
+ TestBed.tick()
159
+
160
+ await waitForDevtoolsToBeCreated()
161
+
162
+ expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(0)
163
+
164
+ errorTypes.set([])
165
+
166
+ TestBed.tick()
167
+
168
+ expect(mockDevtoolsPanelInstance.setErrorTypes).toHaveBeenCalledTimes(1)
169
+ })
170
+
171
+ it('should update onclose', async () => {
172
+ const functionA = () => {}
173
+ const functionB = () => {}
174
+
175
+ const onClose = signal(functionA)
176
+
177
+ TestBed.runInInjectionContext(() => {
178
+ return injectDevtoolsPanel(() => ({
179
+ hostElement: TestBed.inject(ElementRef),
180
+ onClose: onClose(),
181
+ }))
182
+ })
183
+
184
+ TestBed.tick()
185
+
186
+ await waitForDevtoolsToBeCreated()
187
+
188
+ expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(0)
189
+
190
+ onClose.set(functionB)
191
+
192
+ TestBed.tick()
193
+
194
+ expect(mockDevtoolsPanelInstance.setOnClose).toHaveBeenCalledTimes(1)
195
+ })
196
+ })
@@ -0,0 +1,66 @@
1
+ import { TestBed } from '@angular/core/testing'
2
+ import { afterEach, beforeEach, describe, expectTypeOf, it, vi } from 'vitest'
3
+ import { provideZonelessChangeDetection } from '@angular/core'
4
+ import { queryKey, sleep } from '@tanstack/query-test-utils'
5
+ import {
6
+ QueryClient,
7
+ injectInfiniteQuery,
8
+ provideTanStackQuery,
9
+ skipToken,
10
+ } from '..'
11
+ import type { InfiniteData } from '@tanstack/query-core'
12
+
13
+ describe('injectInfiniteQuery', () => {
14
+ let queryClient: QueryClient
15
+
16
+ beforeEach(() => {
17
+ queryClient = new QueryClient()
18
+ vi.useFakeTimers()
19
+ TestBed.configureTestingModule({
20
+ providers: [
21
+ provideZonelessChangeDetection(),
22
+ provideTanStackQuery(queryClient),
23
+ ],
24
+ })
25
+ })
26
+
27
+ afterEach(() => {
28
+ vi.useRealTimers()
29
+ })
30
+
31
+ it('should narrow type after isSuccess', () => {
32
+ const key = queryKey()
33
+ const query = TestBed.runInInjectionContext(() => {
34
+ return injectInfiniteQuery(() => ({
35
+ queryKey: key,
36
+ queryFn: ({ pageParam }) =>
37
+ sleep(0).then(() => 'data on page ' + pageParam),
38
+ initialPageParam: 0,
39
+ getNextPageParam: () => 12,
40
+ }))
41
+ })
42
+
43
+ if (query.isSuccess()) {
44
+ const data = query.data()
45
+ expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
46
+ }
47
+ })
48
+
49
+ it('should work when queryFn is skipToken', () => {
50
+ const key = queryKey()
51
+ const query = TestBed.runInInjectionContext(() => {
52
+ return injectInfiniteQuery(() => ({
53
+ queryKey: key,
54
+ queryFn: skipToken,
55
+ initialPageParam: 0,
56
+ getNextPageParam: () => 1,
57
+ }))
58
+ })
59
+
60
+ // TPageParam falls back to `unknown` here (unlike `infiniteQueryOptions`, which infers
61
+ // `number` for the same options) because there's no skipToken-specific overload to carry it.
62
+ expectTypeOf(query.data()).toEqualTypeOf<
63
+ InfiniteData<unknown, unknown> | undefined
64
+ >()
65
+ })
66
+ })