@tanstack/react-query 4.24.9 → 5.0.0-alpha.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 (199) hide show
  1. package/build/lib/HydrationBoundary.d.ts +9 -0
  2. package/build/lib/{Hydrate.esm.js → HydrationBoundary.esm.js} +14 -17
  3. package/build/lib/HydrationBoundary.esm.js.map +1 -0
  4. package/build/lib/{Hydrate.js → HydrationBoundary.js} +17 -24
  5. package/build/lib/HydrationBoundary.js.map +1 -0
  6. package/build/lib/{Hydrate.mjs → HydrationBoundary.mjs} +14 -17
  7. package/build/lib/HydrationBoundary.mjs.map +1 -0
  8. package/build/lib/QueryClientProvider.d.ts +4 -19
  9. package/build/lib/QueryClientProvider.esm.js +11 -46
  10. package/build/lib/QueryClientProvider.esm.js.map +1 -1
  11. package/build/lib/QueryClientProvider.js +14 -52
  12. package/build/lib/QueryClientProvider.js.map +1 -1
  13. package/build/lib/QueryClientProvider.mjs +11 -46
  14. package/build/lib/QueryClientProvider.mjs.map +1 -1
  15. package/build/lib/QueryErrorResetBoundary.esm.js +7 -2
  16. package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
  17. package/build/lib/QueryErrorResetBoundary.js +10 -8
  18. package/build/lib/QueryErrorResetBoundary.js.map +1 -1
  19. package/build/lib/QueryErrorResetBoundary.mjs +7 -2
  20. package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
  21. package/build/lib/__tests__/useQuery.types.test.d.ts +1 -2
  22. package/build/lib/__tests__/utils.d.ts +7 -10
  23. package/build/lib/errorBoundaryUtils.d.ts +3 -3
  24. package/build/lib/errorBoundaryUtils.esm.js +3 -3
  25. package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
  26. package/build/lib/errorBoundaryUtils.js +6 -9
  27. package/build/lib/errorBoundaryUtils.js.map +1 -1
  28. package/build/lib/errorBoundaryUtils.mjs +3 -3
  29. package/build/lib/errorBoundaryUtils.mjs.map +1 -1
  30. package/build/lib/index.d.ts +4 -5
  31. package/build/lib/index.esm.js +3 -4
  32. package/build/lib/index.esm.js.map +1 -1
  33. package/build/lib/index.js +6 -9
  34. package/build/lib/index.js.map +1 -1
  35. package/build/lib/index.mjs +3 -4
  36. package/build/lib/index.mjs.map +1 -1
  37. package/build/lib/isRestoring.esm.js.map +1 -1
  38. package/build/lib/isRestoring.js +3 -6
  39. package/build/lib/isRestoring.js.map +1 -1
  40. package/build/lib/isRestoring.mjs.map +1 -1
  41. package/build/lib/suspense.esm.js +5 -5
  42. package/build/lib/suspense.esm.js.map +1 -1
  43. package/build/lib/suspense.js +5 -7
  44. package/build/lib/suspense.js.map +1 -1
  45. package/build/lib/suspense.mjs +5 -5
  46. package/build/lib/suspense.mjs.map +1 -1
  47. package/build/lib/types.d.ts +14 -22
  48. package/build/lib/useBaseQuery.d.ts +2 -2
  49. package/build/lib/useBaseQuery.esm.js +15 -18
  50. package/build/lib/useBaseQuery.esm.js.map +1 -1
  51. package/build/lib/useBaseQuery.js +18 -24
  52. package/build/lib/useBaseQuery.js.map +1 -1
  53. package/build/lib/useBaseQuery.mjs +15 -18
  54. package/build/lib/useBaseQuery.mjs.map +1 -1
  55. package/build/lib/useInfiniteQuery.d.ts +2 -4
  56. package/build/lib/useInfiniteQuery.esm.js +4 -4
  57. package/build/lib/useInfiniteQuery.esm.js.map +1 -1
  58. package/build/lib/useInfiniteQuery.js +3 -5
  59. package/build/lib/useInfiniteQuery.js.map +1 -1
  60. package/build/lib/useInfiniteQuery.mjs +4 -4
  61. package/build/lib/useInfiniteQuery.mjs.map +1 -1
  62. package/build/lib/useIsFetching.d.ts +2 -7
  63. package/build/lib/useIsFetching.esm.js +5 -9
  64. package/build/lib/useIsFetching.esm.js.map +1 -1
  65. package/build/lib/useIsFetching.js +7 -14
  66. package/build/lib/useIsFetching.js.map +1 -1
  67. package/build/lib/useIsFetching.mjs +5 -9
  68. package/build/lib/useIsFetching.mjs.map +1 -1
  69. package/build/lib/useMutation.d.ts +2 -5
  70. package/build/lib/useMutation.esm.js +12 -14
  71. package/build/lib/useMutation.esm.js.map +1 -1
  72. package/build/lib/useMutation.js +14 -19
  73. package/build/lib/useMutation.js.map +1 -1
  74. package/build/lib/useMutation.mjs +12 -14
  75. package/build/lib/useMutation.mjs.map +1 -1
  76. package/build/lib/useMutationState.d.ts +8 -0
  77. package/build/lib/useMutationState.esm.js +37 -0
  78. package/build/lib/useMutationState.esm.js.map +1 -0
  79. package/build/lib/useMutationState.js +59 -0
  80. package/build/lib/useMutationState.js.map +1 -0
  81. package/build/lib/useMutationState.mjs +37 -0
  82. package/build/lib/useMutationState.mjs.map +1 -0
  83. package/build/lib/useQueries.d.ts +9 -7
  84. package/build/lib/useQueries.esm.js +16 -27
  85. package/build/lib/useQueries.esm.js.map +1 -1
  86. package/build/lib/useQueries.js +19 -33
  87. package/build/lib/useQueries.js.map +1 -1
  88. package/build/lib/useQueries.mjs +16 -27
  89. package/build/lib/useQueries.mjs.map +1 -1
  90. package/build/lib/useQuery.d.ts +9 -21
  91. package/build/lib/useQuery.esm.js +5 -4
  92. package/build/lib/useQuery.esm.js.map +1 -1
  93. package/build/lib/useQuery.js +4 -5
  94. package/build/lib/useQuery.js.map +1 -1
  95. package/build/lib/useQuery.mjs +5 -4
  96. package/build/lib/useQuery.mjs.map +1 -1
  97. package/build/lib/utils.d.ts +1 -1
  98. package/build/lib/utils.esm.js +5 -6
  99. package/build/lib/utils.esm.js.map +1 -1
  100. package/build/lib/utils.js +5 -8
  101. package/build/lib/utils.js.map +1 -1
  102. package/build/lib/utils.mjs +5 -6
  103. package/build/lib/utils.mjs.map +1 -1
  104. package/build/umd/index.development.js +1007 -1826
  105. package/build/umd/index.development.js.map +1 -1
  106. package/build/umd/index.production.js +1 -1
  107. package/build/umd/index.production.js.map +1 -1
  108. package/package.json +6 -18
  109. package/src/{Hydrate.tsx → HydrationBoundary.tsx} +17 -18
  110. package/src/QueryClientProvider.tsx +11 -65
  111. package/src/__tests__/{Hydrate.test.tsx → HydrationBoundary.test.tsx} +89 -77
  112. package/src/__tests__/QueryClientProvider.test.tsx +33 -147
  113. package/src/__tests__/QueryResetErrorBoundary.test.tsx +118 -85
  114. package/src/__tests__/ssr-hydration.test.tsx +27 -33
  115. package/src/__tests__/ssr.test.tsx +23 -9
  116. package/src/__tests__/suspense.test.tsx +194 -171
  117. package/src/__tests__/useInfiniteQuery.test.tsx +249 -494
  118. package/src/__tests__/useInfiniteQuery.type.test.tsx +131 -0
  119. package/src/__tests__/useIsFetching.test.tsx +65 -108
  120. package/src/__tests__/useMutation.test.tsx +200 -268
  121. package/src/__tests__/useMutationState.test.tsx +284 -0
  122. package/src/__tests__/useQueries.test.tsx +43 -347
  123. package/src/__tests__/useQuery.test.tsx +890 -686
  124. package/src/__tests__/useQuery.types.test.tsx +23 -24
  125. package/src/__tests__/utils.tsx +14 -23
  126. package/src/errorBoundaryUtils.ts +5 -10
  127. package/src/index.ts +4 -7
  128. package/src/types.ts +33 -38
  129. package/src/useBaseQuery.ts +7 -7
  130. package/src/useInfiniteQuery.ts +12 -77
  131. package/src/useIsFetching.ts +8 -20
  132. package/src/useMutation.ts +8 -66
  133. package/src/useMutationState.ts +81 -0
  134. package/src/useQueries.ts +29 -21
  135. package/src/useQuery.ts +27 -104
  136. package/src/utils.ts +5 -5
  137. package/build/lib/Hydrate.d.ts +0 -10
  138. package/build/lib/Hydrate.esm.js.map +0 -1
  139. package/build/lib/Hydrate.js.map +0 -1
  140. package/build/lib/Hydrate.mjs.map +0 -1
  141. package/build/lib/reactBatchedUpdates.d.ts +0 -2
  142. package/build/lib/reactBatchedUpdates.esm.js +0 -6
  143. package/build/lib/reactBatchedUpdates.esm.js.map +0 -1
  144. package/build/lib/reactBatchedUpdates.js +0 -30
  145. package/build/lib/reactBatchedUpdates.js.map +0 -1
  146. package/build/lib/reactBatchedUpdates.mjs +0 -6
  147. package/build/lib/reactBatchedUpdates.mjs.map +0 -1
  148. package/build/lib/reactBatchedUpdates.native.d.ts +0 -2
  149. package/build/lib/reactBatchedUpdates.native.esm.js +0 -2
  150. package/build/lib/reactBatchedUpdates.native.esm.js.map +0 -1
  151. package/build/lib/reactBatchedUpdates.native.js +0 -13
  152. package/build/lib/reactBatchedUpdates.native.js.map +0 -1
  153. package/build/lib/reactBatchedUpdates.native.mjs +0 -2
  154. package/build/lib/reactBatchedUpdates.native.mjs.map +0 -1
  155. package/build/lib/setBatchUpdatesFn.esm.js +0 -5
  156. package/build/lib/setBatchUpdatesFn.esm.js.map +0 -1
  157. package/build/lib/setBatchUpdatesFn.js +0 -7
  158. package/build/lib/setBatchUpdatesFn.js.map +0 -1
  159. package/build/lib/setBatchUpdatesFn.mjs +0 -5
  160. package/build/lib/setBatchUpdatesFn.mjs.map +0 -1
  161. package/build/lib/useIsMutating.d.ts +0 -7
  162. package/build/lib/useIsMutating.esm.js +0 -16
  163. package/build/lib/useIsMutating.esm.js.map +0 -1
  164. package/build/lib/useIsMutating.js +0 -40
  165. package/build/lib/useIsMutating.js.map +0 -1
  166. package/build/lib/useIsMutating.mjs +0 -16
  167. package/build/lib/useIsMutating.mjs.map +0 -1
  168. package/build/lib/useSyncExternalStore.d.ts +0 -2
  169. package/build/lib/useSyncExternalStore.esm.js +0 -7
  170. package/build/lib/useSyncExternalStore.esm.js.map +0 -1
  171. package/build/lib/useSyncExternalStore.js +0 -11
  172. package/build/lib/useSyncExternalStore.js.map +0 -1
  173. package/build/lib/useSyncExternalStore.mjs +0 -7
  174. package/build/lib/useSyncExternalStore.mjs.map +0 -1
  175. package/build/lib/useSyncExternalStore.native.d.ts +0 -2
  176. package/build/lib/useSyncExternalStore.native.esm.js +0 -2
  177. package/build/lib/useSyncExternalStore.native.esm.js.map +0 -1
  178. package/build/lib/useSyncExternalStore.native.js +0 -13
  179. package/build/lib/useSyncExternalStore.native.js.map +0 -1
  180. package/build/lib/useSyncExternalStore.native.mjs +0 -2
  181. package/build/lib/useSyncExternalStore.native.mjs.map +0 -1
  182. package/codemods/v4/key-transformation.js +0 -138
  183. package/codemods/v4/replace-import-specifier.js +0 -25
  184. package/codemods/v4/utils/index.js +0 -166
  185. package/codemods/v4/utils/replacers/key-replacer.js +0 -160
  186. package/codemods/v4/utils/transformers/query-cache-transformer.js +0 -115
  187. package/codemods/v4/utils/transformers/query-client-transformer.js +0 -49
  188. package/codemods/v4/utils/transformers/use-query-like-transformer.js +0 -32
  189. package/codemods/v4/utils/unprocessable-key-error.js +0 -8
  190. package/src/__tests__/useIsMutating.test.tsx +0 -259
  191. package/src/reactBatchedUpdates.native.ts +0 -4
  192. package/src/reactBatchedUpdates.ts +0 -2
  193. package/src/setBatchUpdatesFn.ts +0 -4
  194. package/src/useIsMutating.ts +0 -39
  195. package/src/useSyncExternalStore.native.ts +0 -5
  196. package/src/useSyncExternalStore.ts +0 -4
  197. /package/build/lib/__tests__/{Hydrate.test.d.ts → HydrationBoundary.test.d.ts} +0 -0
  198. /package/build/lib/__tests__/{useIsMutating.test.d.ts → useInfiniteQuery.type.test.d.ts} +0 -0
  199. /package/build/lib/{setBatchUpdatesFn.d.ts → __tests__/useMutationState.test.d.ts} +0 -0
@@ -0,0 +1,284 @@
1
+ import { fireEvent, render, waitFor } from '@testing-library/react'
2
+ import * as React from 'react'
3
+ import { useIsMutating, useMutationState } from '../useMutationState'
4
+ import { useMutation } from '../useMutation'
5
+ import {
6
+ createQueryClient,
7
+ renderWithClient,
8
+ setActTimeout,
9
+ sleep,
10
+ } from './utils'
11
+ import * as MutationCacheModule from '../../../query-core/src/mutationCache'
12
+ import { screen } from 'solid-testing-library'
13
+
14
+ describe('useIsMutating', () => {
15
+ it('should return the number of fetching mutations', async () => {
16
+ const isMutatings: number[] = []
17
+ const queryClient = createQueryClient()
18
+
19
+ function IsMutating() {
20
+ const isMutating = useIsMutating()
21
+ isMutatings.push(isMutating)
22
+ return null
23
+ }
24
+
25
+ function Mutations() {
26
+ const { mutate: mutate1 } = useMutation({
27
+ mutationKey: ['mutation1'],
28
+ mutationFn: async () => {
29
+ await sleep(150)
30
+ return 'data'
31
+ },
32
+ })
33
+ const { mutate: mutate2 } = useMutation({
34
+ mutationKey: ['mutation2'],
35
+ mutationFn: async () => {
36
+ await sleep(50)
37
+ return 'data'
38
+ },
39
+ })
40
+
41
+ React.useEffect(() => {
42
+ mutate1()
43
+ setActTimeout(() => {
44
+ mutate2()
45
+ }, 50)
46
+ }, [mutate1, mutate2])
47
+
48
+ return null
49
+ }
50
+
51
+ function Page() {
52
+ return (
53
+ <div>
54
+ <IsMutating />
55
+ <Mutations />
56
+ </div>
57
+ )
58
+ }
59
+
60
+ renderWithClient(queryClient, <Page />)
61
+ await waitFor(() => expect(isMutatings).toEqual([0, 1, 2, 1, 0]))
62
+ })
63
+
64
+ it('should filter correctly by mutationKey', async () => {
65
+ const isMutatings: number[] = []
66
+ const queryClient = createQueryClient()
67
+
68
+ function IsMutating() {
69
+ const isMutating = useIsMutating({ mutationKey: ['mutation1'] })
70
+ isMutatings.push(isMutating)
71
+ return null
72
+ }
73
+
74
+ function Page() {
75
+ const { mutate: mutate1 } = useMutation({
76
+ mutationKey: ['mutation1'],
77
+ mutationFn: async () => {
78
+ await sleep(100)
79
+ return 'data'
80
+ },
81
+ })
82
+ const { mutate: mutate2 } = useMutation({
83
+ mutationKey: ['mutation2'],
84
+ mutationFn: async () => {
85
+ await sleep(100)
86
+ return 'data'
87
+ },
88
+ })
89
+
90
+ React.useEffect(() => {
91
+ mutate1()
92
+ mutate2()
93
+ }, [mutate1, mutate2])
94
+
95
+ return <IsMutating />
96
+ }
97
+
98
+ renderWithClient(queryClient, <Page />)
99
+ await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]))
100
+ })
101
+
102
+ it('should filter correctly by predicate', async () => {
103
+ const isMutatings: number[] = []
104
+ const queryClient = createQueryClient()
105
+
106
+ function IsMutating() {
107
+ const isMutating = useIsMutating({
108
+ predicate: (mutation) =>
109
+ mutation.options.mutationKey?.[0] === 'mutation1',
110
+ })
111
+ isMutatings.push(isMutating)
112
+ return null
113
+ }
114
+
115
+ function Page() {
116
+ const { mutate: mutate1 } = useMutation({
117
+ mutationKey: ['mutation1'],
118
+ mutationFn: async () => {
119
+ await sleep(100)
120
+ return 'data'
121
+ },
122
+ })
123
+ const { mutate: mutate2 } = useMutation({
124
+ mutationKey: ['mutation2'],
125
+ mutationFn: async () => {
126
+ await sleep(100)
127
+ return 'data'
128
+ },
129
+ })
130
+
131
+ React.useEffect(() => {
132
+ mutate1()
133
+ mutate2()
134
+ }, [mutate1, mutate2])
135
+
136
+ return <IsMutating />
137
+ }
138
+
139
+ renderWithClient(queryClient, <Page />)
140
+ await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]))
141
+ })
142
+
143
+ it('should not change state if unmounted', async () => {
144
+ // We have to mock the MutationCache to not unsubscribe
145
+ // the listener when the component is unmounted
146
+ class MutationCacheMock extends MutationCacheModule.MutationCache {
147
+ subscribe(listener: any) {
148
+ super.subscribe(listener)
149
+ return () => void 0
150
+ }
151
+ }
152
+
153
+ const MutationCacheSpy = jest
154
+ .spyOn(MutationCacheModule, 'MutationCache')
155
+ .mockImplementation((fn) => {
156
+ return new MutationCacheMock(fn)
157
+ })
158
+
159
+ const queryClient = createQueryClient()
160
+
161
+ function IsMutating() {
162
+ useIsMutating()
163
+ return null
164
+ }
165
+
166
+ function Page() {
167
+ const [mounted, setMounted] = React.useState(true)
168
+ const { mutate: mutate1 } = useMutation({
169
+ mutationKey: ['mutation1'],
170
+ mutationFn: async () => {
171
+ await sleep(10)
172
+ return 'data'
173
+ },
174
+ })
175
+
176
+ React.useEffect(() => {
177
+ mutate1()
178
+ }, [mutate1])
179
+
180
+ return (
181
+ <div>
182
+ <button onClick={() => setMounted(false)}>unmount</button>
183
+ {mounted && <IsMutating />}
184
+ </div>
185
+ )
186
+ }
187
+
188
+ const { getByText } = renderWithClient(queryClient, <Page />)
189
+ fireEvent.click(getByText('unmount'))
190
+
191
+ // Should not display the console error
192
+ // "Warning: Can't perform a React state update on an unmounted component"
193
+
194
+ await sleep(20)
195
+ MutationCacheSpy.mockRestore()
196
+ })
197
+
198
+ it('should use provided custom queryClient', async () => {
199
+ const queryClient = createQueryClient()
200
+
201
+ function Page() {
202
+ const isMutating = useIsMutating({}, queryClient)
203
+ const { mutate } = useMutation(
204
+ {
205
+ mutationKey: ['mutation1'],
206
+ mutationFn: async () => {
207
+ await sleep(10)
208
+ return 'data'
209
+ },
210
+ },
211
+ queryClient,
212
+ )
213
+
214
+ React.useEffect(() => {
215
+ mutate()
216
+ }, [mutate])
217
+
218
+ return (
219
+ <div>
220
+ <div>mutating: {isMutating}</div>
221
+ </div>
222
+ )
223
+ }
224
+
225
+ const rendered = render(<Page></Page>)
226
+
227
+ await waitFor(() => rendered.getByText('mutating: 1'))
228
+ })
229
+ })
230
+
231
+ describe('useMutationState', () => {
232
+ it('should return variables after calling mutate', async () => {
233
+ const queryClient = createQueryClient()
234
+ const variables: unknown[][] = []
235
+ const mutationKey = ['mutation']
236
+
237
+ function Variables() {
238
+ variables.push(
239
+ useMutationState({
240
+ filters: { mutationKey, status: 'pending' },
241
+ select: (mutation) => mutation.state.variables,
242
+ }),
243
+ )
244
+
245
+ return null
246
+ }
247
+
248
+ function Mutate() {
249
+ const { mutate, data } = useMutation({
250
+ mutationKey,
251
+ mutationFn: async (input: number) => {
252
+ await sleep(150)
253
+ return 'data' + input
254
+ },
255
+ })
256
+
257
+ return (
258
+ <div>
259
+ data: {data ?? 'null'}
260
+ <button onClick={() => mutate(1)}>mutate</button>
261
+ </div>
262
+ )
263
+ }
264
+
265
+ function Page() {
266
+ return (
267
+ <div>
268
+ <Variables />
269
+ <Mutate />
270
+ </div>
271
+ )
272
+ }
273
+
274
+ const rendered = renderWithClient(queryClient, <Page />)
275
+
276
+ await waitFor(() => rendered.getByText('data: null'))
277
+
278
+ fireEvent.click(screen.getByRole('button', { name: /mutate/i }))
279
+
280
+ await waitFor(() => rendered.getByText('data: data1'))
281
+
282
+ expect(variables).toEqual([[], [1], []])
283
+ })
284
+ })