@tanstack/query-core 4.24.10 → 5.0.0-alpha.1

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 (184) hide show
  1. package/build/lib/focusManager.d.ts +1 -3
  2. package/build/lib/focusManager.esm.js +19 -36
  3. package/build/lib/focusManager.esm.js.map +1 -1
  4. package/build/lib/focusManager.js +19 -38
  5. package/build/lib/focusManager.js.map +1 -1
  6. package/build/lib/focusManager.mjs +19 -36
  7. package/build/lib/focusManager.mjs.map +1 -1
  8. package/build/lib/hydration.esm.js +21 -23
  9. package/build/lib/hydration.esm.js.map +1 -1
  10. package/build/lib/hydration.js +21 -25
  11. package/build/lib/hydration.js.map +1 -1
  12. package/build/lib/hydration.mjs +21 -23
  13. package/build/lib/hydration.mjs.map +1 -1
  14. package/build/lib/index.d.ts +1 -2
  15. package/build/lib/index.esm.js +1 -1
  16. package/build/lib/index.js +2 -8
  17. package/build/lib/index.js.map +1 -1
  18. package/build/lib/index.mjs +1 -1
  19. package/build/lib/infiniteQueryBehavior.d.ts +3 -7
  20. package/build/lib/infiniteQueryBehavior.esm.js +52 -75
  21. package/build/lib/infiniteQueryBehavior.esm.js.map +1 -1
  22. package/build/lib/infiniteQueryBehavior.js +50 -77
  23. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  24. package/build/lib/infiniteQueryBehavior.mjs +52 -75
  25. package/build/lib/infiniteQueryBehavior.mjs.map +1 -1
  26. package/build/lib/infiniteQueryObserver.d.ts +4 -4
  27. package/build/lib/infiniteQueryObserver.esm.js +18 -26
  28. package/build/lib/infiniteQueryObserver.esm.js.map +1 -1
  29. package/build/lib/infiniteQueryObserver.js +18 -28
  30. package/build/lib/infiniteQueryObserver.js.map +1 -1
  31. package/build/lib/infiniteQueryObserver.mjs +18 -26
  32. package/build/lib/infiniteQueryObserver.mjs.map +1 -1
  33. package/build/lib/mutation.d.ts +11 -22
  34. package/build/lib/mutation.esm.js +73 -105
  35. package/build/lib/mutation.esm.js.map +1 -1
  36. package/build/lib/mutation.js +73 -107
  37. package/build/lib/mutation.js.map +1 -1
  38. package/build/lib/mutation.mjs +73 -105
  39. package/build/lib/mutation.mjs.map +1 -1
  40. package/build/lib/mutationCache.d.ts +4 -6
  41. package/build/lib/mutationCache.esm.js +23 -32
  42. package/build/lib/mutationCache.esm.js.map +1 -1
  43. package/build/lib/mutationCache.js +23 -34
  44. package/build/lib/mutationCache.js.map +1 -1
  45. package/build/lib/mutationCache.mjs +23 -32
  46. package/build/lib/mutationCache.mjs.map +1 -1
  47. package/build/lib/mutationObserver.d.ts +4 -9
  48. package/build/lib/mutationObserver.esm.js +42 -73
  49. package/build/lib/mutationObserver.esm.js.map +1 -1
  50. package/build/lib/mutationObserver.js +42 -75
  51. package/build/lib/mutationObserver.js.map +1 -1
  52. package/build/lib/mutationObserver.mjs +42 -73
  53. package/build/lib/mutationObserver.mjs.map +1 -1
  54. package/build/lib/notifyManager.esm.js +7 -17
  55. package/build/lib/notifyManager.esm.js.map +1 -1
  56. package/build/lib/notifyManager.js +7 -19
  57. package/build/lib/notifyManager.js.map +1 -1
  58. package/build/lib/notifyManager.mjs +7 -17
  59. package/build/lib/notifyManager.mjs.map +1 -1
  60. package/build/lib/onlineManager.d.ts +1 -3
  61. package/build/lib/onlineManager.esm.js +16 -30
  62. package/build/lib/onlineManager.esm.js.map +1 -1
  63. package/build/lib/onlineManager.js +16 -32
  64. package/build/lib/onlineManager.js.map +1 -1
  65. package/build/lib/onlineManager.mjs +16 -30
  66. package/build/lib/onlineManager.mjs.map +1 -1
  67. package/build/lib/queriesObserver.d.ts +3 -10
  68. package/build/lib/queriesObserver.esm.js +47 -71
  69. package/build/lib/queriesObserver.esm.js.map +1 -1
  70. package/build/lib/queriesObserver.js +49 -75
  71. package/build/lib/queriesObserver.js.map +1 -1
  72. package/build/lib/queriesObserver.mjs +47 -71
  73. package/build/lib/queriesObserver.mjs.map +1 -1
  74. package/build/lib/query.d.ts +14 -21
  75. package/build/lib/query.esm.js +140 -194
  76. package/build/lib/query.esm.js.map +1 -1
  77. package/build/lib/query.js +139 -195
  78. package/build/lib/query.js.map +1 -1
  79. package/build/lib/query.mjs +140 -194
  80. package/build/lib/query.mjs.map +1 -1
  81. package/build/lib/queryCache.d.ts +12 -7
  82. package/build/lib/queryCache.esm.js +21 -45
  83. package/build/lib/queryCache.esm.js.map +1 -1
  84. package/build/lib/queryCache.js +20 -46
  85. package/build/lib/queryCache.js.map +1 -1
  86. package/build/lib/queryCache.mjs +21 -45
  87. package/build/lib/queryCache.mjs.map +1 -1
  88. package/build/lib/queryClient.d.ts +18 -46
  89. package/build/lib/queryClient.esm.js +137 -216
  90. package/build/lib/queryClient.esm.js.map +1 -1
  91. package/build/lib/queryClient.js +136 -217
  92. package/build/lib/queryClient.js.map +1 -1
  93. package/build/lib/queryClient.mjs +137 -216
  94. package/build/lib/queryClient.mjs.map +1 -1
  95. package/build/lib/queryObserver.d.ts +4 -29
  96. package/build/lib/queryObserver.esm.js +176 -258
  97. package/build/lib/queryObserver.esm.js.map +1 -1
  98. package/build/lib/queryObserver.js +176 -260
  99. package/build/lib/queryObserver.js.map +1 -1
  100. package/build/lib/queryObserver.mjs +176 -258
  101. package/build/lib/queryObserver.mjs.map +1 -1
  102. package/build/lib/removable.d.ts +3 -3
  103. package/build/lib/removable.esm.js +10 -14
  104. package/build/lib/removable.esm.js.map +1 -1
  105. package/build/lib/removable.js +10 -16
  106. package/build/lib/removable.js.map +1 -1
  107. package/build/lib/removable.mjs +10 -14
  108. package/build/lib/removable.mjs.map +1 -1
  109. package/build/lib/retryer.d.ts +5 -5
  110. package/build/lib/retryer.esm.js +27 -44
  111. package/build/lib/retryer.esm.js.map +1 -1
  112. package/build/lib/retryer.js +27 -46
  113. package/build/lib/retryer.js.map +1 -1
  114. package/build/lib/retryer.mjs +27 -44
  115. package/build/lib/retryer.mjs.map +1 -1
  116. package/build/lib/subscribable.esm.js +4 -7
  117. package/build/lib/subscribable.esm.js.map +1 -1
  118. package/build/lib/subscribable.js +4 -9
  119. package/build/lib/subscribable.js.map +1 -1
  120. package/build/lib/subscribable.mjs +4 -7
  121. package/build/lib/subscribable.mjs.map +1 -1
  122. package/build/lib/tests/utils.d.ts +3 -12
  123. package/build/lib/types.d.ts +111 -99
  124. package/build/lib/utils.d.ts +8 -18
  125. package/build/lib/utils.esm.js +39 -132
  126. package/build/lib/utils.esm.js.map +1 -1
  127. package/build/lib/utils.js +42 -144
  128. package/build/lib/utils.js.map +1 -1
  129. package/build/lib/utils.mjs +39 -132
  130. package/build/lib/utils.mjs.map +1 -1
  131. package/build/umd/index.development.js +867 -1399
  132. package/build/umd/index.development.js.map +1 -1
  133. package/build/umd/index.production.js +1 -1
  134. package/build/umd/index.production.js.map +1 -1
  135. package/package.json +1 -1
  136. package/src/focusManager.ts +17 -24
  137. package/src/index.ts +1 -11
  138. package/src/infiniteQueryBehavior.ts +54 -94
  139. package/src/infiniteQueryObserver.ts +10 -12
  140. package/src/mutation.ts +68 -92
  141. package/src/mutationCache.ts +27 -27
  142. package/src/mutationObserver.ts +58 -97
  143. package/src/onlineManager.ts +14 -14
  144. package/src/queriesObserver.ts +50 -54
  145. package/src/query.ts +107 -111
  146. package/src/queryCache.ts +42 -41
  147. package/src/queryClient.ts +155 -434
  148. package/src/queryObserver.ts +155 -192
  149. package/src/removable.ts +13 -13
  150. package/src/retryer.ts +5 -5
  151. package/src/subscribable.ts +1 -1
  152. package/src/tests/focusManager.test.tsx +25 -25
  153. package/src/tests/hydration.test.tsx +167 -81
  154. package/src/tests/infiniteQueryBehavior.test.tsx +209 -17
  155. package/src/tests/infiniteQueryObserver.test.tsx +6 -2
  156. package/src/tests/mutationCache.test.tsx +127 -127
  157. package/src/tests/mutationObserver.test.tsx +1 -31
  158. package/src/tests/mutations.test.tsx +62 -43
  159. package/src/tests/onlineManager.test.tsx +12 -4
  160. package/src/tests/queriesObserver.test.tsx +41 -77
  161. package/src/tests/query.test.tsx +175 -243
  162. package/src/tests/queryCache.test.tsx +170 -93
  163. package/src/tests/queryClient.test.tsx +229 -378
  164. package/src/tests/queryObserver.test.tsx +23 -147
  165. package/src/tests/utils.test.tsx +84 -29
  166. package/src/tests/utils.ts +9 -18
  167. package/src/types.ts +187 -140
  168. package/src/utils.ts +31 -124
  169. package/build/lib/logger.d.ts +0 -8
  170. package/build/lib/logger.esm.js +0 -4
  171. package/build/lib/logger.esm.js.map +0 -1
  172. package/build/lib/logger.js +0 -8
  173. package/build/lib/logger.js.map +0 -1
  174. package/build/lib/logger.mjs +0 -4
  175. package/build/lib/logger.mjs.map +0 -1
  176. package/build/lib/logger.native.d.ts +0 -6
  177. package/build/lib/logger.native.esm.js +0 -12
  178. package/build/lib/logger.native.esm.js.map +0 -1
  179. package/build/lib/logger.native.js +0 -16
  180. package/build/lib/logger.native.js.map +0 -1
  181. package/build/lib/logger.native.mjs +0 -12
  182. package/build/lib/logger.native.mjs.map +0 -1
  183. package/src/logger.native.ts +0 -11
  184. package/src/logger.ts +0 -9
@@ -29,7 +29,7 @@ describe('mutationObserver', () => {
29
29
  const unsubscribe1 = mutation.subscribe(subscription1Handler)
30
30
  const unsubscribe2 = mutation.subscribe(subscription2Handler)
31
31
 
32
- mutation.mutate()
32
+ mutation.mutate('input')
33
33
 
34
34
  unsubscribe1()
35
35
 
@@ -43,34 +43,4 @@ describe('mutationObserver', () => {
43
43
  // Clean-up
44
44
  unsubscribe2()
45
45
  })
46
-
47
- test('should not notify listeners if options.listeners is set to false', async () => {
48
- const mutation = new MutationObserver(queryClient, {
49
- mutationFn: async (text: string) => {
50
- await sleep(20)
51
- return text
52
- },
53
- })
54
-
55
- const subscriptionHandler = jest.fn()
56
- const unsubscribe = mutation.subscribe(subscriptionHandler)
57
- mutation.mutate()
58
-
59
- await waitFor(() => {
60
- // 2 calls: loading, success
61
- expect(subscriptionHandler).toBeCalledTimes(2)
62
- })
63
- subscriptionHandler.mockReset()
64
-
65
- // Force a notification with listeners set to false
66
- // because there is no existing usage of notify with listeners set to false
67
- mutation['notify']({ listeners: false })
68
-
69
- await waitFor(() => {
70
- // 0 call because no notification has been sent
71
- expect(subscriptionHandler).toBeCalledTimes(0)
72
- })
73
-
74
- unsubscribe()
75
- })
76
46
  })
@@ -15,15 +15,6 @@ describe('mutations', () => {
15
15
  queryClient.clear()
16
16
  })
17
17
 
18
- test('mutate should trigger a mutation', async () => {
19
- const result = await executeMutation(queryClient, {
20
- mutationFn: async (text: string) => text,
21
- variables: 'todo',
22
- })
23
-
24
- expect(result).toBe(result)
25
- })
26
-
27
18
  test('mutate should accept null values', async () => {
28
19
  let variables
29
20
 
@@ -34,26 +25,29 @@ describe('mutations', () => {
34
25
  },
35
26
  })
36
27
 
37
- mutation.mutate(null)
38
-
39
- await sleep(10)
28
+ await mutation.mutate(null)
40
29
 
41
30
  expect(variables).toBe(null)
42
31
  })
43
32
 
44
33
  test('setMutationDefaults should be able to set defaults', async () => {
45
34
  const key = queryKey()
35
+ const fn = jest.fn()
46
36
 
47
37
  queryClient.setMutationDefaults(key, {
48
- mutationFn: async (text: string) => text,
38
+ mutationFn: fn,
49
39
  })
50
40
 
51
- const result = await executeMutation(queryClient, {
52
- mutationKey: key,
53
- variables: 'todo',
54
- })
41
+ await executeMutation(
42
+ queryClient,
43
+ {
44
+ mutationKey: key,
45
+ },
46
+ 'vars',
47
+ )
55
48
 
56
- expect(result).toBe(result)
49
+ expect(fn).toHaveBeenCalledTimes(1)
50
+ expect(fn).toHaveBeenCalledWith('vars')
57
51
  })
58
52
 
59
53
  test('mutation should set correct success states', async () => {
@@ -63,7 +57,6 @@ describe('mutations', () => {
63
57
  return text
64
58
  },
65
59
  onMutate: (text) => text,
66
- variables: 'todo',
67
60
  })
68
61
 
69
62
  expect(mutation.getCurrentResult()).toEqual({
@@ -74,13 +67,14 @@ describe('mutations', () => {
74
67
  failureReason: null,
75
68
  isError: false,
76
69
  isIdle: true,
77
- isLoading: false,
70
+ isPending: false,
78
71
  isPaused: false,
79
72
  isSuccess: false,
80
73
  mutate: expect.any(Function),
81
74
  reset: expect.any(Function),
82
75
  status: 'idle',
83
76
  variables: undefined,
77
+ submittedAt: 0,
84
78
  })
85
79
 
86
80
  const states: MutationState<string, unknown, string, string>[] = []
@@ -89,7 +83,7 @@ describe('mutations', () => {
89
83
  states.push(state)
90
84
  })
91
85
 
92
- mutation.mutate()
86
+ mutation.mutate('todo')
93
87
 
94
88
  await sleep(0)
95
89
 
@@ -101,13 +95,14 @@ describe('mutations', () => {
101
95
  failureReason: null,
102
96
  isError: false,
103
97
  isIdle: false,
104
- isLoading: true,
98
+ isPending: true,
105
99
  isPaused: false,
106
100
  isSuccess: false,
107
101
  mutate: expect.any(Function),
108
102
  reset: expect.any(Function),
109
- status: 'loading',
103
+ status: 'pending',
110
104
  variables: 'todo',
105
+ submittedAt: expect.any(Number),
111
106
  })
112
107
 
113
108
  await sleep(5)
@@ -120,13 +115,14 @@ describe('mutations', () => {
120
115
  failureReason: null,
121
116
  isError: false,
122
117
  isIdle: false,
123
- isLoading: true,
118
+ isPending: true,
124
119
  isPaused: false,
125
120
  isSuccess: false,
126
121
  mutate: expect.any(Function),
127
122
  reset: expect.any(Function),
128
- status: 'loading',
123
+ status: 'pending',
129
124
  variables: 'todo',
125
+ submittedAt: expect.any(Number),
130
126
  })
131
127
 
132
128
  await sleep(20)
@@ -139,24 +135,24 @@ describe('mutations', () => {
139
135
  failureReason: null,
140
136
  isError: false,
141
137
  isIdle: false,
142
- isLoading: false,
138
+ isPending: false,
143
139
  isPaused: false,
144
140
  isSuccess: true,
145
141
  mutate: expect.any(Function),
146
142
  reset: expect.any(Function),
147
143
  status: 'success',
148
144
  variables: 'todo',
145
+ submittedAt: expect.any(Number),
149
146
  })
150
147
  })
151
148
 
152
149
  test('mutation should set correct error states', async () => {
153
150
  const mutation = new MutationObserver(queryClient, {
154
- mutationFn: async () => {
151
+ mutationFn: async (_: string) => {
155
152
  await sleep(20)
156
- return Promise.reject('err')
153
+ return Promise.reject(new Error('err'))
157
154
  },
158
155
  onMutate: (text) => text,
159
- variables: 'todo',
160
156
  retry: 1,
161
157
  retryDelay: 1,
162
158
  })
@@ -167,7 +163,7 @@ describe('mutations', () => {
167
163
  states.push(state)
168
164
  })
169
165
 
170
- mutation.mutate().catch(() => undefined)
166
+ mutation.mutate('todo').catch(() => undefined)
171
167
 
172
168
  await sleep(0)
173
169
 
@@ -179,13 +175,14 @@ describe('mutations', () => {
179
175
  failureReason: null,
180
176
  isError: false,
181
177
  isIdle: false,
182
- isLoading: true,
178
+ isPending: true,
183
179
  isPaused: false,
184
180
  isSuccess: false,
185
181
  mutate: expect.any(Function),
186
182
  reset: expect.any(Function),
187
- status: 'loading',
183
+ status: 'pending',
188
184
  variables: 'todo',
185
+ submittedAt: expect.any(Number),
189
186
  })
190
187
 
191
188
  await sleep(10)
@@ -198,13 +195,14 @@ describe('mutations', () => {
198
195
  failureReason: null,
199
196
  isError: false,
200
197
  isIdle: false,
201
- isLoading: true,
198
+ isPending: true,
202
199
  isPaused: false,
203
200
  isSuccess: false,
204
201
  mutate: expect.any(Function),
205
202
  reset: expect.any(Function),
206
- status: 'loading',
203
+ status: 'pending',
207
204
  variables: 'todo',
205
+ submittedAt: expect.any(Number),
208
206
  })
209
207
 
210
208
  await sleep(20)
@@ -214,16 +212,17 @@ describe('mutations', () => {
214
212
  data: undefined,
215
213
  error: null,
216
214
  failureCount: 1,
217
- failureReason: 'err',
215
+ failureReason: new Error('err'),
218
216
  isError: false,
219
217
  isIdle: false,
220
- isLoading: true,
218
+ isPending: true,
221
219
  isPaused: false,
222
220
  isSuccess: false,
223
221
  mutate: expect.any(Function),
224
222
  reset: expect.any(Function),
225
- status: 'loading',
223
+ status: 'pending',
226
224
  variables: 'todo',
225
+ submittedAt: expect.any(Number),
227
226
  })
228
227
 
229
228
  await sleep(30)
@@ -231,18 +230,19 @@ describe('mutations', () => {
231
230
  expect(states[3]).toEqual({
232
231
  context: 'todo',
233
232
  data: undefined,
234
- error: 'err',
233
+ error: new Error('err'),
235
234
  failureCount: 2,
236
- failureReason: 'err',
235
+ failureReason: new Error('err'),
237
236
  isError: true,
238
237
  isIdle: false,
239
- isLoading: false,
238
+ isPending: false,
240
239
  isPaused: false,
241
240
  isSuccess: false,
242
241
  mutate: expect.any(Function),
243
242
  reset: expect.any(Function),
244
243
  status: 'error',
245
244
  variables: 'todo',
245
+ submittedAt: expect.any(Number),
246
246
  })
247
247
  })
248
248
 
@@ -274,8 +274,9 @@ describe('mutations', () => {
274
274
  failureCount: 1,
275
275
  failureReason: 'err',
276
276
  isPaused: true,
277
- status: 'loading',
277
+ status: 'pending',
278
278
  variables: 'todo',
279
+ submittedAt: 1,
279
280
  },
280
281
  )
281
282
 
@@ -286,8 +287,9 @@ describe('mutations', () => {
286
287
  failureCount: 1,
287
288
  failureReason: 'err',
288
289
  isPaused: true,
289
- status: 'loading',
290
+ status: 'pending',
290
291
  variables: 'todo',
292
+ submittedAt: 1,
291
293
  })
292
294
 
293
295
  await queryClient.resumePausedMutations()
@@ -301,6 +303,7 @@ describe('mutations', () => {
301
303
  isPaused: false,
302
304
  status: 'success',
303
305
  variables: 'todo',
306
+ submittedAt: 1,
304
307
  })
305
308
 
306
309
  expect(onMutate).not.toHaveBeenCalled()
@@ -340,7 +343,23 @@ describe('mutations', () => {
340
343
  } catch (err) {
341
344
  error = err
342
345
  }
343
- expect(error).toEqual('No mutationFn found')
346
+ expect(error).toEqual(new Error('No mutationFn found'))
347
+ })
348
+
349
+ test('mutate update the mutation state even without an active subscription', async () => {
350
+ const onSuccess = jest.fn()
351
+ const onSettled = jest.fn()
352
+
353
+ const mutation = new MutationObserver(queryClient, {
354
+ mutationFn: async () => {
355
+ return 'update'
356
+ },
357
+ })
358
+
359
+ await mutation.mutate(undefined, { onSuccess, onSettled })
360
+ expect(mutation.getCurrentResult().data).toEqual('update')
361
+ expect(onSuccess).not.toHaveBeenCalled()
362
+ expect(onSettled).not.toHaveBeenCalled()
344
363
  })
345
364
 
346
365
  test('mutate update the mutation state even without an active subscription', async () => {
@@ -56,26 +56,34 @@ describe('onlineManager', () => {
56
56
  expect(remove2Spy).not.toHaveBeenCalled()
57
57
  })
58
58
 
59
- test('cleanup should still be undefined if window is not defined', async () => {
59
+ test('cleanup (removeEventListener) should not be called if window is not defined', async () => {
60
60
  const restoreIsServer = setIsServer(true)
61
61
 
62
+ const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
63
+
62
64
  const unsubscribe = onlineManager.subscribe(() => undefined)
63
- expect(onlineManager['cleanup']).toBeUndefined()
64
65
 
65
66
  unsubscribe()
67
+
68
+ expect(removeEventListenerSpy).not.toHaveBeenCalled()
69
+
66
70
  restoreIsServer()
67
71
  })
68
72
 
69
- test('cleanup should still be undefined if window.addEventListener is not defined', async () => {
73
+ test('cleanup (removeEventListener) should not be called if window.addEventListener is not defined', async () => {
70
74
  const { addEventListener } = globalThis.window
71
75
 
72
76
  // @ts-expect-error
73
77
  globalThis.window.addEventListener = undefined
74
78
 
79
+ const removeEventListenerSpy = jest.spyOn(globalThis, 'removeEventListener')
80
+
75
81
  const unsubscribe = onlineManager.subscribe(() => undefined)
76
- expect(onlineManager['cleanup']).toBeUndefined()
77
82
 
78
83
  unsubscribe()
84
+
85
+ expect(removeEventListenerSpy).not.toHaveBeenCalled()
86
+
79
87
  globalThis.window.addEventListener = addEventListener
80
88
  })
81
89
 
@@ -1,8 +1,7 @@
1
1
  import { waitFor } from '@testing-library/react'
2
- import { sleep, queryKey, createQueryClient, mockLogger } from './utils'
2
+ import { sleep, queryKey, createQueryClient } from './utils'
3
3
  import type { QueryClient, QueryObserverResult } from '..'
4
- import { QueriesObserver, QueryObserver } from '..'
5
- import type { QueryKey } from '..'
4
+ import { QueriesObserver } from '..'
6
5
 
7
6
  describe('queriesObserver', () => {
8
7
  let queryClient: QueryClient
@@ -35,6 +34,8 @@ describe('queriesObserver', () => {
35
34
  })
36
35
 
37
36
  test('should still return value for undefined query key', async () => {
37
+ const consoleMock = jest.spyOn(console, 'error')
38
+ consoleMock.mockImplementation(() => undefined)
38
39
  const key1 = queryKey()
39
40
  const queryFn1 = jest.fn().mockReturnValue(1)
40
41
  const queryFn2 = jest.fn().mockReturnValue(2)
@@ -50,13 +51,11 @@ describe('queriesObserver', () => {
50
51
  unsubscribe()
51
52
  expect(observerResult).toMatchObject([{ data: 1 }, { data: 2 }])
52
53
 
53
- expect(mockLogger.error).toHaveBeenCalledTimes(2)
54
- expect(mockLogger.error).toHaveBeenCalledWith(
55
- 'Passing a custom logger has been deprecated and will be removed in the next major version.',
56
- )
57
- expect(mockLogger.error).toHaveBeenCalledWith(
58
- 'Passing a custom logger has been deprecated and will be removed in the next major version.',
54
+ expect(consoleMock).toHaveBeenCalledTimes(1)
55
+ expect(consoleMock).toHaveBeenCalledWith(
56
+ "As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']",
59
57
  )
58
+ consoleMock.mockRestore()
60
59
  })
61
60
 
62
61
  test('should update when a query updates', async () => {
@@ -79,20 +78,20 @@ describe('queriesObserver', () => {
79
78
  unsubscribe()
80
79
  expect(results.length).toBe(6)
81
80
  expect(results[0]).toMatchObject([
82
- { status: 'loading', fetchStatus: 'idle', data: undefined },
83
- { status: 'loading', fetchStatus: 'idle', data: undefined },
81
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
82
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
84
83
  ])
85
84
  expect(results[1]).toMatchObject([
86
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
87
- { status: 'loading', fetchStatus: 'idle', data: undefined },
85
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
86
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
88
87
  ])
89
88
  expect(results[2]).toMatchObject([
90
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
91
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
89
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
90
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
92
91
  ])
93
92
  expect(results[3]).toMatchObject([
94
93
  { status: 'success', data: 1 },
95
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
94
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
96
95
  ])
97
96
  expect(results[4]).toMatchObject([
98
97
  { status: 'success', data: 1 },
@@ -122,27 +121,27 @@ describe('queriesObserver', () => {
122
121
  observer.setQueries([{ queryKey: key2, queryFn: queryFn2 }])
123
122
  await sleep(1)
124
123
  const queryCache = queryClient.getQueryCache()
125
- expect(queryCache.find(key1, { type: 'active' })).toBeUndefined()
126
- expect(queryCache.find(key2, { type: 'active' })).toBeDefined()
124
+ expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
125
+ expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeDefined()
127
126
  unsubscribe()
128
- expect(queryCache.find(key1, { type: 'active' })).toBeUndefined()
129
- expect(queryCache.find(key2, { type: 'active' })).toBeUndefined()
127
+ expect(queryCache.find({ queryKey: key1, type: 'active' })).toBeUndefined()
128
+ expect(queryCache.find({ queryKey: key2, type: 'active' })).toBeUndefined()
130
129
  expect(results.length).toBe(6)
131
130
  expect(results[0]).toMatchObject([
132
- { status: 'loading', fetchStatus: 'idle', data: undefined },
133
- { status: 'loading', fetchStatus: 'idle', data: undefined },
131
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
132
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
134
133
  ])
135
134
  expect(results[1]).toMatchObject([
136
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
137
- { status: 'loading', fetchStatus: 'idle', data: undefined },
135
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
136
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
138
137
  ])
139
138
  expect(results[2]).toMatchObject([
140
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
141
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
139
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
140
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
142
141
  ])
143
142
  expect(results[3]).toMatchObject([
144
143
  { status: 'success', data: 1 },
145
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
144
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
146
145
  ])
147
146
  expect(results[4]).toMatchObject([
148
147
  { status: 'success', data: 1 },
@@ -174,20 +173,20 @@ describe('queriesObserver', () => {
174
173
  unsubscribe()
175
174
  expect(results.length).toBe(6)
176
175
  expect(results[0]).toMatchObject([
177
- { status: 'loading', fetchStatus: 'idle', data: undefined },
178
- { status: 'loading', fetchStatus: 'idle', data: undefined },
176
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
177
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
179
178
  ])
180
179
  expect(results[1]).toMatchObject([
181
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
182
- { status: 'loading', fetchStatus: 'idle', data: undefined },
180
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
181
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
183
182
  ])
184
183
  expect(results[2]).toMatchObject([
185
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
186
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
184
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
185
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
187
186
  ])
188
187
  expect(results[3]).toMatchObject([
189
188
  { status: 'success', data: 1 },
190
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
189
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
191
190
  ])
192
191
  expect(results[4]).toMatchObject([
193
192
  { status: 'success', data: 1 },
@@ -222,20 +221,20 @@ describe('queriesObserver', () => {
222
221
  unsubscribe()
223
222
  expect(results.length).toBe(5)
224
223
  expect(results[0]).toMatchObject([
225
- { status: 'loading', fetchStatus: 'idle', data: undefined },
226
- { status: 'loading', fetchStatus: 'idle', data: undefined },
224
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
225
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
227
226
  ])
228
227
  expect(results[1]).toMatchObject([
229
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
230
- { status: 'loading', fetchStatus: 'idle', data: undefined },
228
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
229
+ { status: 'pending', fetchStatus: 'idle', data: undefined },
231
230
  ])
232
231
  expect(results[2]).toMatchObject([
233
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
234
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
232
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
233
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
235
234
  ])
236
235
  expect(results[3]).toMatchObject([
237
236
  { status: 'success', data: 1 },
238
- { status: 'loading', fetchStatus: 'fetching', data: undefined },
237
+ { status: 'pending', fetchStatus: 'fetching', data: undefined },
239
238
  ])
240
239
  expect(results[4]).toMatchObject([
241
240
  { status: 'success', data: 1 },
@@ -280,7 +279,7 @@ describe('queriesObserver', () => {
280
279
  unsubscribe1()
281
280
 
282
281
  await waitFor(() => {
283
- // 1 call: loading
282
+ // 1 call: pending
284
283
  expect(subscription1Handler).toBeCalledTimes(1)
285
284
  // 1 call: success
286
285
  expect(subscription2Handler).toBeCalledTimes(1)
@@ -289,39 +288,4 @@ describe('queriesObserver', () => {
289
288
  // Clean-up
290
289
  unsubscribe2()
291
290
  })
292
-
293
- test('onUpdate should not update the result for an unknown observer', async () => {
294
- const key1 = queryKey()
295
- const key2 = queryKey()
296
-
297
- const queriesObserver = new QueriesObserver(queryClient, [
298
- {
299
- queryKey: key1,
300
- queryFn: () => 1,
301
- },
302
- ])
303
-
304
- const newQueryObserver = new QueryObserver<
305
- unknown,
306
- unknown,
307
- unknown,
308
- unknown,
309
- QueryKey
310
- >(queryClient, {
311
- queryKey: key2,
312
- queryFn: () => 2,
313
- })
314
-
315
- // Force onUpdate with an unknown QueryObserver
316
- // because no existing use case has been found in the lib
317
- queriesObserver['onUpdate'](
318
- newQueryObserver,
319
- // The current queries observer result is re-used here
320
- // to use a typescript friendly result
321
- queriesObserver.getCurrentResult()[0]!,
322
- )
323
-
324
- // Should not alter the result
325
- expect(queriesObserver.getCurrentResult()[-1]).toBeUndefined()
326
- })
327
291
  })