@tanstack/angular-query-experimental 5.103.2 → 5.104.0

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 (218) hide show
  1. package/dist/create-base-query.d.mts +168 -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 +5 -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 +106 -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} +4 -1
  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} +4 -1
  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 +5 -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 +51 -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} +28 -25
  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} +13 -10
  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} +11 -8
  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} +11 -8
  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} +10 -8
  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} +13 -11
  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} +12 -9
  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} +33 -53
  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} +6 -3
  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} +13 -10
  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} +5 -2
  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 +9 -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} +7 -5
  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} +34 -31
  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/{signal-proxy.d.ts → dist/signal-proxy.d.mts} +5 -4
  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} +24 -26
  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 +50 -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 +176 -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 +480 -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 -103
  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.mjs +0 -29
  218. package/signal-proxy.mjs.map +0 -1
@@ -0,0 +1,234 @@
1
+ import { assertType, describe, expectTypeOf, it } from 'vitest'
2
+ import { queryKey } from '@tanstack/query-test-utils'
3
+ import { QueryClient } from '@tanstack/query-core'
4
+ import {
5
+ injectIsMutating,
6
+ injectMutation,
7
+ injectMutationState,
8
+ mutationOptions,
9
+ } from '..'
10
+ import type {
11
+ DefaultError,
12
+ MutationFunctionContext,
13
+ MutationState,
14
+ WithRequired,
15
+ } from '@tanstack/query-core'
16
+ import type { CreateMutationOptions, CreateMutationResult } from '../types'
17
+
18
+ describe('mutationOptions', () => {
19
+ it('should not allow excess properties', () => {
20
+ const key = queryKey()
21
+ mutationOptions({
22
+ mutationFn: () => Promise.resolve(5),
23
+ // @ts-expect-error this is a good error, because onMutates does not exist!
24
+ onMutates: 1000,
25
+ mutationKey: key,
26
+ onSuccess: (data) => {
27
+ expectTypeOf(data).toEqualTypeOf<number>()
28
+ },
29
+ })
30
+ })
31
+
32
+ it('should infer types for callbacks', () => {
33
+ const key = queryKey()
34
+ mutationOptions({
35
+ mutationFn: () => Promise.resolve(5),
36
+ mutationKey: key,
37
+ onSuccess: (data) => {
38
+ expectTypeOf(data).toEqualTypeOf<number>()
39
+ },
40
+ })
41
+ })
42
+
43
+ it('should infer types for onError callback', () => {
44
+ const key = queryKey()
45
+ mutationOptions({
46
+ mutationFn: () => {
47
+ throw new Error('fail')
48
+ },
49
+ mutationKey: key,
50
+ onError: (error) => {
51
+ expectTypeOf(error).toEqualTypeOf<DefaultError>()
52
+ },
53
+ })
54
+ })
55
+
56
+ it('should infer types for variables', () => {
57
+ const key = queryKey()
58
+ mutationOptions<number, DefaultError, { id: string }>({
59
+ mutationFn: (vars) => {
60
+ expectTypeOf(vars).toEqualTypeOf<{ id: string }>()
61
+ return Promise.resolve(5)
62
+ },
63
+ mutationKey: key,
64
+ })
65
+ })
66
+
67
+ it('should infer result type correctly', () => {
68
+ const key = queryKey()
69
+ mutationOptions<number, DefaultError, void, { name: string }>({
70
+ mutationFn: () => Promise.resolve(5),
71
+ mutationKey: key,
72
+ onMutate: () => {
73
+ return { name: 'onMutateResult' }
74
+ },
75
+ onSuccess: (_data, _variables, onMutateResult) => {
76
+ expectTypeOf(onMutateResult).toEqualTypeOf<{ name: string }>()
77
+ },
78
+ })
79
+ })
80
+
81
+ it('should infer context type correctly', () => {
82
+ const key = queryKey()
83
+ mutationOptions<number>({
84
+ mutationFn: (_variables, context) => {
85
+ expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
86
+ return Promise.resolve(5)
87
+ },
88
+ mutationKey: key,
89
+ onMutate: (_variables, context) => {
90
+ expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
91
+ },
92
+ onSuccess: (_data, _variables, _onMutateResult, context) => {
93
+ expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
94
+ },
95
+ onError: (_error, _variables, _onMutateResult, context) => {
96
+ expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
97
+ },
98
+ onSettled: (_data, _error, _variables, _onMutateResult, context) => {
99
+ expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
100
+ },
101
+ })
102
+ })
103
+
104
+ it('should error if mutationFn return type mismatches TData', () => {
105
+ assertType(
106
+ mutationOptions<number>({
107
+ // @ts-expect-error this is a good error, because return type is string, not number
108
+ mutationFn: async () => Promise.resolve('wrong return'),
109
+ }),
110
+ )
111
+ })
112
+
113
+ it('should allow mutationKey to be omitted', () => {
114
+ return mutationOptions({
115
+ mutationFn: () => Promise.resolve(123),
116
+ onSuccess: (data) => {
117
+ expectTypeOf(data).toEqualTypeOf<number>()
118
+ },
119
+ })
120
+ })
121
+
122
+ it('should infer all types when not explicitly provided', () => {
123
+ const key = queryKey()
124
+ expectTypeOf(
125
+ mutationOptions({
126
+ mutationFn: (id: string) => Promise.resolve(id.length),
127
+ mutationKey: key,
128
+ onSuccess: (data) => {
129
+ expectTypeOf(data).toEqualTypeOf<number>()
130
+ },
131
+ }),
132
+ ).toEqualTypeOf<
133
+ WithRequired<
134
+ CreateMutationOptions<number, DefaultError, string>,
135
+ 'mutationKey'
136
+ >
137
+ >()
138
+ expectTypeOf(
139
+ mutationOptions({
140
+ mutationFn: (id: string) => Promise.resolve(id.length),
141
+ onSuccess: (data) => {
142
+ expectTypeOf(data).toEqualTypeOf<number>()
143
+ },
144
+ }),
145
+ ).toEqualTypeOf<
146
+ Omit<CreateMutationOptions<number, DefaultError, string>, 'mutationKey'>
147
+ >()
148
+ })
149
+
150
+ it('should infer types when used with injectMutation', () => {
151
+ const key = queryKey()
152
+ const mutation = injectMutation(() =>
153
+ mutationOptions({
154
+ mutationKey: key,
155
+ mutationFn: () => Promise.resolve('data'),
156
+ onSuccess: (data) => {
157
+ expectTypeOf(data).toEqualTypeOf<string>()
158
+ },
159
+ }),
160
+ )
161
+ expectTypeOf(mutation).toEqualTypeOf<
162
+ CreateMutationResult<string, DefaultError, void, unknown>
163
+ >()
164
+
165
+ injectMutation(
166
+ // should allow when used with injectMutation without mutationKey
167
+ () =>
168
+ mutationOptions({
169
+ mutationFn: () => Promise.resolve('data'),
170
+ onSuccess: (data) => {
171
+ expectTypeOf(data).toEqualTypeOf<string>()
172
+ },
173
+ }),
174
+ )
175
+ })
176
+
177
+ it('should infer types when used with injectIsMutating', () => {
178
+ const key = queryKey()
179
+ const isMutating = injectIsMutating(
180
+ mutationOptions({
181
+ mutationKey: key,
182
+ mutationFn: () => Promise.resolve(5),
183
+ }),
184
+ )
185
+ expectTypeOf(isMutating()).toEqualTypeOf<number>()
186
+
187
+ injectIsMutating(
188
+ // @ts-expect-error filters should have mutationKey
189
+ mutationOptions({
190
+ mutationFn: () => Promise.resolve(5),
191
+ }),
192
+ )
193
+ })
194
+
195
+ it('should infer types when used with queryClient.isMutating', () => {
196
+ const key = queryKey()
197
+ const queryClient = new QueryClient()
198
+
199
+ const isMutating = queryClient.isMutating(
200
+ mutationOptions({
201
+ mutationKey: key,
202
+ mutationFn: () => Promise.resolve(5),
203
+ }),
204
+ )
205
+ expectTypeOf(isMutating).toEqualTypeOf<number>()
206
+
207
+ queryClient.isMutating(
208
+ // @ts-expect-error filters should have mutationKey
209
+ mutationOptions({
210
+ mutationFn: () => Promise.resolve(5),
211
+ }),
212
+ )
213
+ })
214
+
215
+ it('should infer types when used with injectMutationState', () => {
216
+ const key = queryKey()
217
+ const mutationState = injectMutationState(() => ({
218
+ filters: mutationOptions({
219
+ mutationKey: key,
220
+ mutationFn: () => Promise.resolve(5),
221
+ }),
222
+ }))
223
+ expectTypeOf(mutationState()).toEqualTypeOf<
224
+ Array<MutationState<unknown, Error, unknown, unknown>>
225
+ >()
226
+
227
+ injectMutationState({
228
+ // @ts-expect-error filters should have mutationKey
229
+ filters: mutationOptions({
230
+ mutationFn: () => Promise.resolve(5),
231
+ }),
232
+ })
233
+ })
234
+ })
@@ -0,0 +1,344 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { provideZonelessChangeDetection } from '@angular/core'
3
+ import { TestBed } from '@angular/core/testing'
4
+ import { QueryClient } from '@tanstack/query-core'
5
+ import { queryKey, sleep } from '@tanstack/query-test-utils'
6
+ import {
7
+ injectIsMutating,
8
+ injectMutation,
9
+ injectMutationState,
10
+ mutationOptions,
11
+ provideTanStackQuery,
12
+ } from '..'
13
+ import type { CreateMutationOptions } from '../types'
14
+
15
+ describe('mutationOptions', () => {
16
+ let queryClient: QueryClient
17
+
18
+ beforeEach(() => {
19
+ vi.useFakeTimers()
20
+ queryClient = new QueryClient()
21
+ TestBed.configureTestingModule({
22
+ providers: [
23
+ provideZonelessChangeDetection(),
24
+ provideTanStackQuery(queryClient),
25
+ ],
26
+ })
27
+ })
28
+
29
+ afterEach(() => {
30
+ vi.useRealTimers()
31
+ })
32
+
33
+ it('should return the object received as a parameter without any modification (with mutationKey in mutationOptions)', () => {
34
+ const key = queryKey()
35
+ const object: CreateMutationOptions = {
36
+ mutationKey: key,
37
+ mutationFn: () => sleep(10).then(() => 5),
38
+ } as const
39
+
40
+ expect(mutationOptions(object)).toBe(object)
41
+ })
42
+
43
+ it('should return the object received as a parameter without any modification (without mutationKey in mutationOptions)', () => {
44
+ const object: CreateMutationOptions = {
45
+ mutationFn: () => sleep(10).then(() => 5),
46
+ } as const
47
+
48
+ expect(mutationOptions(object)).toBe(object)
49
+ })
50
+
51
+ it('should return the number of fetching mutations when used with injectIsMutating (with mutationKey in mutationOptions)', async () => {
52
+ const key = queryKey()
53
+ const mutationOpts = mutationOptions({
54
+ mutationKey: key,
55
+ mutationFn: () => sleep(50).then(() => 'data'),
56
+ })
57
+
58
+ const [mutation, isMutating] = TestBed.runInInjectionContext(() => [
59
+ injectMutation(() => mutationOpts),
60
+ injectIsMutating(),
61
+ ])
62
+
63
+ expect(isMutating()).toBe(0)
64
+
65
+ mutation.mutate()
66
+ expect(isMutating()).toBe(0)
67
+ await vi.advanceTimersByTimeAsync(0)
68
+ expect(isMutating()).toBe(1)
69
+ await vi.advanceTimersByTimeAsync(51)
70
+ expect(isMutating()).toBe(0)
71
+ })
72
+
73
+ it('should return the number of fetching mutations when used with injectIsMutating (without mutationKey in mutationOptions)', async () => {
74
+ const mutationOpts = mutationOptions({
75
+ mutationFn: () => sleep(50).then(() => 'data'),
76
+ })
77
+
78
+ const [mutation, isMutating] = TestBed.runInInjectionContext(() => [
79
+ injectMutation(() => mutationOpts),
80
+ injectIsMutating(),
81
+ ])
82
+
83
+ expect(isMutating()).toBe(0)
84
+
85
+ mutation.mutate()
86
+ expect(isMutating()).toBe(0)
87
+ await vi.advanceTimersByTimeAsync(0)
88
+ expect(isMutating()).toBe(1)
89
+ await vi.advanceTimersByTimeAsync(51)
90
+ expect(isMutating()).toBe(0)
91
+ })
92
+
93
+ it('should return the number of fetching mutations when used with injectIsMutating', async () => {
94
+ const key = queryKey()
95
+ const mutationOpts1 = mutationOptions({
96
+ mutationKey: key,
97
+ mutationFn: () => sleep(50).then(() => 'data1'),
98
+ })
99
+ const mutationOpts2 = mutationOptions({
100
+ mutationFn: () => sleep(50).then(() => 'data2'),
101
+ })
102
+
103
+ const [mutation1, mutation2, isMutating] = TestBed.runInInjectionContext(
104
+ () => [
105
+ injectMutation(() => mutationOpts1),
106
+ injectMutation(() => mutationOpts2),
107
+ injectIsMutating(),
108
+ ],
109
+ )
110
+
111
+ expect(isMutating()).toBe(0)
112
+
113
+ mutation1.mutate()
114
+ mutation2.mutate()
115
+ expect(isMutating()).toBe(0)
116
+ await vi.advanceTimersByTimeAsync(0)
117
+ expect(isMutating()).toBe(2)
118
+ await vi.advanceTimersByTimeAsync(51)
119
+ expect(isMutating()).toBe(0)
120
+ })
121
+
122
+ it('should return the number of fetching mutations when used with injectIsMutating (filter mutationOpts1.mutationKey)', async () => {
123
+ const key = queryKey()
124
+ const mutationOpts1 = mutationOptions({
125
+ mutationKey: key,
126
+ mutationFn: () => sleep(50).then(() => 'data1'),
127
+ })
128
+ const mutationOpts2 = mutationOptions({
129
+ mutationFn: () => sleep(50).then(() => 'data2'),
130
+ })
131
+
132
+ const [mutation1, mutation2, isMutating] = TestBed.runInInjectionContext(
133
+ () => [
134
+ injectMutation(() => mutationOpts1),
135
+ injectMutation(() => mutationOpts2),
136
+ injectIsMutating({ mutationKey: mutationOpts1.mutationKey }),
137
+ ],
138
+ )
139
+
140
+ expect(isMutating()).toBe(0)
141
+
142
+ mutation1.mutate()
143
+ mutation2.mutate()
144
+ expect(isMutating()).toBe(0)
145
+ await vi.advanceTimersByTimeAsync(0)
146
+ expect(isMutating()).toBe(1)
147
+ await vi.advanceTimersByTimeAsync(51)
148
+ expect(isMutating()).toBe(0)
149
+ })
150
+
151
+ it('should return the number of fetching mutations when used with queryClient.isMutating (with mutationKey in mutationOptions)', async () => {
152
+ const key = queryKey()
153
+ const mutationOpts = mutationOptions({
154
+ mutationKey: key,
155
+ mutationFn: () => sleep(500).then(() => 'data'),
156
+ })
157
+
158
+ const mutation = TestBed.runInInjectionContext(() =>
159
+ injectMutation(() => mutationOpts),
160
+ )
161
+
162
+ expect(queryClient.isMutating(mutationOpts)).toBe(0)
163
+
164
+ mutation.mutate()
165
+ expect(queryClient.isMutating(mutationOpts)).toBe(1)
166
+ await vi.advanceTimersByTimeAsync(501)
167
+ expect(queryClient.isMutating(mutationOpts)).toBe(0)
168
+ })
169
+
170
+ it('should return the number of fetching mutations when used with queryClient.isMutating (without mutationKey in mutationOptions)', async () => {
171
+ const mutationOpts = mutationOptions({
172
+ mutationFn: () => sleep(500).then(() => 'data'),
173
+ })
174
+
175
+ const mutation = TestBed.runInInjectionContext(() =>
176
+ injectMutation(() => mutationOpts),
177
+ )
178
+
179
+ expect(queryClient.isMutating()).toBe(0)
180
+
181
+ mutation.mutate()
182
+ expect(queryClient.isMutating()).toBe(1)
183
+ await vi.advanceTimersByTimeAsync(501)
184
+ expect(queryClient.isMutating()).toBe(0)
185
+ })
186
+
187
+ it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
188
+ const key = queryKey()
189
+ const mutationOpts1 = mutationOptions({
190
+ mutationKey: key,
191
+ mutationFn: () => sleep(500).then(() => 'data1'),
192
+ })
193
+ const mutationOpts2 = mutationOptions({
194
+ mutationFn: () => sleep(500).then(() => 'data2'),
195
+ })
196
+
197
+ const [mutation1, mutation2] = TestBed.runInInjectionContext(() => [
198
+ injectMutation(() => mutationOpts1),
199
+ injectMutation(() => mutationOpts2),
200
+ ])
201
+
202
+ expect(queryClient.isMutating()).toBe(0)
203
+
204
+ mutation1.mutate()
205
+ mutation2.mutate()
206
+ expect(queryClient.isMutating()).toBe(2)
207
+ await vi.advanceTimersByTimeAsync(501)
208
+ expect(queryClient.isMutating()).toBe(0)
209
+ })
210
+
211
+ it('should return the number of fetching mutations when used with queryClient.isMutating (filter mutationOpt1.mutationKey)', async () => {
212
+ const key = queryKey()
213
+ const mutationOpts1 = mutationOptions({
214
+ mutationKey: key,
215
+ mutationFn: () => sleep(500).then(() => 'data1'),
216
+ })
217
+ const mutationOpts2 = mutationOptions({
218
+ mutationFn: () => sleep(500).then(() => 'data2'),
219
+ })
220
+
221
+ const [mutation1, mutation2] = TestBed.runInInjectionContext(() => [
222
+ injectMutation(() => mutationOpts1),
223
+ injectMutation(() => mutationOpts2),
224
+ ])
225
+
226
+ expect(
227
+ queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
228
+ ).toBe(0)
229
+
230
+ mutation1.mutate()
231
+ mutation2.mutate()
232
+ expect(
233
+ queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
234
+ ).toBe(1)
235
+ await vi.advanceTimersByTimeAsync(501)
236
+ expect(
237
+ queryClient.isMutating({ mutationKey: mutationOpts1.mutationKey }),
238
+ ).toBe(0)
239
+ })
240
+
241
+ it('should return the number of fetching mutations when used with injectMutationState (with mutationKey in mutationOptions)', async () => {
242
+ const key = queryKey()
243
+ const mutationOpts = mutationOptions({
244
+ mutationKey: key,
245
+ mutationFn: () => sleep(10).then(() => 'data'),
246
+ })
247
+
248
+ const [mutation, mutationState] = TestBed.runInInjectionContext(() => [
249
+ injectMutation(() => mutationOpts),
250
+ injectMutationState(() => ({
251
+ filters: { mutationKey: mutationOpts.mutationKey, status: 'success' },
252
+ })),
253
+ ])
254
+
255
+ expect(mutationState().length).toBe(0)
256
+
257
+ mutation.mutate()
258
+ await vi.advanceTimersByTimeAsync(11)
259
+ expect(mutationState().length).toBe(1)
260
+ expect(mutationState()[0]?.data).toBe('data')
261
+ })
262
+
263
+ it('should return the number of fetching mutations when used with injectMutationState (without mutationKey in mutationOptions)', async () => {
264
+ const mutationOpts = mutationOptions({
265
+ mutationFn: () => sleep(10).then(() => 'data'),
266
+ })
267
+
268
+ const [mutation, mutationState] = TestBed.runInInjectionContext(() => [
269
+ injectMutation(() => mutationOpts),
270
+ injectMutationState(() => ({
271
+ filters: { status: 'success' },
272
+ })),
273
+ ])
274
+
275
+ expect(mutationState().length).toBe(0)
276
+
277
+ mutation.mutate()
278
+ await vi.advanceTimersByTimeAsync(11)
279
+ expect(mutationState().length).toBe(1)
280
+ expect(mutationState()[0]?.data).toBe('data')
281
+ })
282
+
283
+ it('should return the number of fetching mutations when used with injectMutationState', async () => {
284
+ const key = queryKey()
285
+ const mutationOpts1 = mutationOptions({
286
+ mutationKey: key,
287
+ mutationFn: () => sleep(10).then(() => 'data1'),
288
+ })
289
+ const mutationOpts2 = mutationOptions({
290
+ mutationFn: () => sleep(10).then(() => 'data2'),
291
+ })
292
+
293
+ const [mutation1, mutation2, mutationState] = TestBed.runInInjectionContext(
294
+ () => [
295
+ injectMutation(() => mutationOpts1),
296
+ injectMutation(() => mutationOpts2),
297
+ injectMutationState(() => ({
298
+ filters: { status: 'success' },
299
+ })),
300
+ ],
301
+ )
302
+
303
+ expect(mutationState().length).toBe(0)
304
+
305
+ mutation1.mutate()
306
+ mutation2.mutate()
307
+ await vi.advanceTimersByTimeAsync(11)
308
+ expect(mutationState().length).toBe(2)
309
+ expect(mutationState()[0]?.data).toBe('data1')
310
+ expect(mutationState()[1]?.data).toBe('data2')
311
+ })
312
+
313
+ it('should return the number of fetching mutations when used with injectMutationState (filter mutationOpt1.mutationKey)', async () => {
314
+ const key = queryKey()
315
+ const mutationOpts1 = mutationOptions({
316
+ mutationKey: key,
317
+ mutationFn: () => sleep(10).then(() => 'data1'),
318
+ })
319
+ const mutationOpts2 = mutationOptions({
320
+ mutationFn: () => sleep(10).then(() => 'data2'),
321
+ })
322
+
323
+ const [mutation1, mutation2, mutationState] = TestBed.runInInjectionContext(
324
+ () => [
325
+ injectMutation(() => mutationOpts1),
326
+ injectMutation(() => mutationOpts2),
327
+ injectMutationState(() => ({
328
+ filters: {
329
+ mutationKey: mutationOpts1.mutationKey,
330
+ status: 'success',
331
+ },
332
+ })),
333
+ ],
334
+ )
335
+
336
+ expect(mutationState().length).toBe(0)
337
+
338
+ mutation1.mutate()
339
+ mutation2.mutate()
340
+ await vi.advanceTimersByTimeAsync(11)
341
+ expect(mutationState().length).toBe(1)
342
+ expect(mutationState()[0]?.data).toBe('data1')
343
+ })
344
+ })