@tanstack/query-core 5.59.17 → 5.60.5

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 (30) hide show
  1. package/build/legacy/queriesObserver.cjs +16 -10
  2. package/build/legacy/queriesObserver.cjs.map +1 -1
  3. package/build/legacy/queriesObserver.js +16 -10
  4. package/build/legacy/queriesObserver.js.map +1 -1
  5. package/build/modern/queriesObserver.cjs +13 -9
  6. package/build/modern/queriesObserver.cjs.map +1 -1
  7. package/build/modern/queriesObserver.js +13 -9
  8. package/build/modern/queriesObserver.js.map +1 -1
  9. package/package.json +3 -2
  10. package/src/queriesObserver.ts +21 -12
  11. package/src/__tests__/OmitKeyof.test-d.ts +0 -175
  12. package/src/__tests__/focusManager.test.tsx +0 -163
  13. package/src/__tests__/hydration.test.tsx +0 -1069
  14. package/src/__tests__/infiniteQueryBehavior.test.tsx +0 -427
  15. package/src/__tests__/infiniteQueryObserver.test-d.tsx +0 -64
  16. package/src/__tests__/infiniteQueryObserver.test.tsx +0 -198
  17. package/src/__tests__/mutationCache.test.tsx +0 -376
  18. package/src/__tests__/mutationObserver.test.tsx +0 -326
  19. package/src/__tests__/mutations.test.tsx +0 -603
  20. package/src/__tests__/notifyManager.test.tsx +0 -85
  21. package/src/__tests__/onlineManager.test.tsx +0 -168
  22. package/src/__tests__/queriesObserver.test.tsx +0 -267
  23. package/src/__tests__/query.test.tsx +0 -1049
  24. package/src/__tests__/queryCache.test.tsx +0 -350
  25. package/src/__tests__/queryClient.test-d.tsx +0 -156
  26. package/src/__tests__/queryClient.test.tsx +0 -2031
  27. package/src/__tests__/queryObserver.test-d.tsx +0 -108
  28. package/src/__tests__/queryObserver.test.tsx +0 -1236
  29. package/src/__tests__/utils.test.tsx +0 -468
  30. package/src/__tests__/utils.ts +0 -59
@@ -1,468 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import {
3
- addToEnd,
4
- addToStart,
5
- isPlainArray,
6
- isPlainObject,
7
- matchMutation,
8
- partialMatchKey,
9
- replaceEqualDeep,
10
- shallowEqualObjects,
11
- } from '../utils'
12
- import { Mutation } from '../mutation'
13
- import { createQueryClient } from './utils'
14
-
15
- describe('core/utils', () => {
16
- describe('shallowEqualObjects', () => {
17
- it('should return `true` for shallow equal objects', () => {
18
- expect(shallowEqualObjects({ a: 1 }, { a: 1 })).toEqual(true)
19
- })
20
-
21
- it('should return `false` for non shallow equal objects', () => {
22
- expect(shallowEqualObjects({ a: 1 }, { a: 2 })).toEqual(false)
23
- })
24
-
25
- it('should return `false` if lengths are not equal', () => {
26
- expect(shallowEqualObjects({ a: 1 }, { a: 1, b: 2 })).toEqual(false)
27
- })
28
-
29
- it('should return false if b is undefined', () => {
30
- expect(shallowEqualObjects({ a: 1 }, undefined)).toEqual(false)
31
- })
32
- })
33
- describe('isPlainObject', () => {
34
- it('should return `true` for a plain object', () => {
35
- expect(isPlainObject({})).toEqual(true)
36
- })
37
-
38
- it('should return `false` for an array', () => {
39
- expect(isPlainObject([])).toEqual(false)
40
- })
41
-
42
- it('should return `false` for null', () => {
43
- expect(isPlainObject(null)).toEqual(false)
44
- })
45
-
46
- it('should return `false` for undefined', () => {
47
- expect(isPlainObject(undefined)).toEqual(false)
48
- })
49
-
50
- it('should return `true` for object with an undefined constructor', () => {
51
- expect(isPlainObject(Object.create(null))).toBeTruthy()
52
- })
53
-
54
- it('should return `false` if constructor does not have an Object-specific method', () => {
55
- class Foo {
56
- abc: any
57
- constructor() {
58
- this.abc = {}
59
- }
60
- }
61
- expect(isPlainObject(new Foo())).toBeFalsy()
62
- })
63
-
64
- it('should return `false` if the object has a modified prototype', () => {
65
- function Graph(this: any) {
66
- this.vertices = []
67
- this.edges = []
68
- }
69
-
70
- Graph.prototype.addVertex = function (v: any) {
71
- this.vertices.push(v)
72
- }
73
-
74
- expect(isPlainObject(Object.create(Graph))).toBeFalsy()
75
- })
76
-
77
- it('should return `false` for object with custom prototype', () => {
78
- const CustomProto = Object.create({ a: 1 })
79
- const obj = Object.create(CustomProto)
80
- obj.b = 2
81
-
82
- expect(isPlainObject(obj)).toBeFalsy()
83
- })
84
- })
85
-
86
- describe('isPlainArray', () => {
87
- it('should return `true` for plain arrays', () => {
88
- expect(isPlainArray([1, 2])).toEqual(true)
89
- })
90
-
91
- it('should return `false` for non plain arrays', () => {
92
- expect(isPlainArray(Object.assign([1, 2], { a: 'b' }))).toEqual(false)
93
- })
94
- })
95
-
96
- describe('partialMatchKey', () => {
97
- it('should return `true` if a includes b', () => {
98
- const a = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
99
- const b = [{ a: { b: 'b' }, c: 'c', d: [] }]
100
- expect(partialMatchKey(a, b)).toEqual(true)
101
- })
102
-
103
- it('should return `false` if a does not include b', () => {
104
- const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
105
- const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
106
- expect(partialMatchKey(a, b)).toEqual(false)
107
- })
108
-
109
- it('should return `true` if array a includes array b', () => {
110
- const a = [1, 2, 3]
111
- const b = [1, 2]
112
- expect(partialMatchKey(a, b)).toEqual(true)
113
- })
114
-
115
- it('should return `false` if a is null and b is not', () => {
116
- const a = [null]
117
- const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
118
- expect(partialMatchKey(a, b)).toEqual(false)
119
- })
120
-
121
- it('should return `false` if a contains null and b is not', () => {
122
- const a = [{ a: null, c: 'c', d: [] }]
123
- const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
124
- expect(partialMatchKey(a, b)).toEqual(false)
125
- })
126
-
127
- it('should return `false` if b is null and a is not', () => {
128
- const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
129
- const b = [null]
130
- expect(partialMatchKey(a, b)).toEqual(false)
131
- })
132
-
133
- it('should return `false` if b contains null and a is not', () => {
134
- const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
135
- const b = [{ a: null, c: 'c', d: [{ d: 'd ' }] }]
136
- expect(partialMatchKey(a, b)).toEqual(false)
137
- })
138
- })
139
-
140
- describe('replaceEqualDeep', () => {
141
- it('should return the previous value when the next value is an equal primitive', () => {
142
- expect(replaceEqualDeep(1, 1)).toBe(1)
143
- expect(replaceEqualDeep('1', '1')).toBe('1')
144
- expect(replaceEqualDeep(true, true)).toBe(true)
145
- expect(replaceEqualDeep(false, false)).toBe(false)
146
- expect(replaceEqualDeep(null, null)).toBe(null)
147
- expect(replaceEqualDeep(undefined, undefined)).toBe(undefined)
148
- })
149
- it('should return the next value when the previous value is a different value', () => {
150
- const date1 = new Date()
151
- const date2 = new Date()
152
- expect(replaceEqualDeep(1, 0)).toBe(0)
153
- expect(replaceEqualDeep(1, 2)).toBe(2)
154
- expect(replaceEqualDeep('1', '2')).toBe('2')
155
- expect(replaceEqualDeep(true, false)).toBe(false)
156
- expect(replaceEqualDeep(false, true)).toBe(true)
157
- expect(replaceEqualDeep(date1, date2)).toBe(date2)
158
- })
159
-
160
- it('should return the next value when the previous value is a different type', () => {
161
- const array = [1]
162
- const object = { a: 'a' }
163
- expect(replaceEqualDeep(0, undefined)).toBe(undefined)
164
- expect(replaceEqualDeep(undefined, 0)).toBe(0)
165
- expect(replaceEqualDeep(2, undefined)).toBe(undefined)
166
- expect(replaceEqualDeep(undefined, 2)).toBe(2)
167
- expect(replaceEqualDeep(undefined, null)).toBe(null)
168
- expect(replaceEqualDeep(null, undefined)).toBe(undefined)
169
- expect(replaceEqualDeep({}, undefined)).toBe(undefined)
170
- expect(replaceEqualDeep([], undefined)).toBe(undefined)
171
- expect(replaceEqualDeep(array, object)).toBe(object)
172
- expect(replaceEqualDeep(object, array)).toBe(array)
173
- })
174
-
175
- it('should return the previous value when the next value is an equal array', () => {
176
- const prev = [1, 2]
177
- const next = [1, 2]
178
- expect(replaceEqualDeep(prev, next)).toBe(prev)
179
- })
180
-
181
- it('should return a copy when the previous value is a different array subset', () => {
182
- const prev = [1, 2]
183
- const next = [1, 2, 3]
184
- const result = replaceEqualDeep(prev, next)
185
- expect(result).toEqual(next)
186
- expect(result).not.toBe(prev)
187
- expect(result).not.toBe(next)
188
- })
189
-
190
- it('should return a copy when the previous value is a different array superset', () => {
191
- const prev = [1, 2, 3]
192
- const next = [1, 2]
193
- const result = replaceEqualDeep(prev, next)
194
- expect(result).toEqual(next)
195
- expect(result).not.toBe(prev)
196
- expect(result).not.toBe(next)
197
- })
198
-
199
- it('should return the previous value when the next value is an equal empty array', () => {
200
- const prev: Array<any> = []
201
- const next: Array<any> = []
202
- expect(replaceEqualDeep(prev, next)).toBe(prev)
203
- })
204
-
205
- it('should return the previous value when the next value is an equal empty object', () => {
206
- const prev = {}
207
- const next = {}
208
- expect(replaceEqualDeep(prev, next)).toBe(prev)
209
- })
210
-
211
- it('should return the previous value when the next value is an equal object', () => {
212
- const prev = { a: 'a' }
213
- const next = { a: 'a' }
214
- expect(replaceEqualDeep(prev, next)).toBe(prev)
215
- })
216
-
217
- it('should replace different values in objects', () => {
218
- const prev = { a: { b: 'b' }, c: 'c' }
219
- const next = { a: { b: 'b' }, c: 'd' }
220
- const result = replaceEqualDeep(prev, next)
221
- expect(result).toEqual(next)
222
- expect(result).not.toBe(prev)
223
- expect(result).not.toBe(next)
224
- expect(result.a).toBe(prev.a)
225
- expect(result.c).toBe(next.c)
226
- })
227
-
228
- it('should replace different values in arrays', () => {
229
- const prev = [1, { a: 'a' }, { b: { b: 'b' } }, [1]] as const
230
- const next = [1, { a: 'a' }, { b: { b: 'c' } }, [1]] as const
231
- const result = replaceEqualDeep(prev, next)
232
- expect(result).toEqual(next)
233
- expect(result).not.toBe(prev)
234
- expect(result).not.toBe(next)
235
- expect(result[0]).toBe(prev[0])
236
- expect(result[1]).toBe(prev[1])
237
- expect(result[2]).not.toBe(next[2])
238
- expect(result[2].b.b).toBe(next[2].b.b)
239
- expect(result[3]).toBe(prev[3])
240
- })
241
-
242
- it('should replace different values in arrays when the next value is a subset', () => {
243
- const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
244
- const next = [{ a: 'a' }, { b: 'b' }]
245
- const result = replaceEqualDeep(prev, next)
246
- expect(result).toEqual(next)
247
- expect(result).not.toBe(prev)
248
- expect(result).not.toBe(next)
249
- expect(result[0]).toBe(prev[0])
250
- expect(result[1]).toBe(prev[1])
251
- expect(result[2]).toBeUndefined()
252
- })
253
-
254
- it('should replace different values in arrays when the next value is a superset', () => {
255
- const prev = [{ a: 'a' }, { b: 'b' }]
256
- const next = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
257
- const result = replaceEqualDeep(prev, next)
258
- expect(result).toEqual(next)
259
- expect(result).not.toBe(prev)
260
- expect(result).not.toBe(next)
261
- expect(result[0]).toBe(prev[0])
262
- expect(result[1]).toBe(prev[1])
263
- expect(result[2]).toBe(next[2])
264
- })
265
-
266
- it('should copy objects which are not arrays or objects', () => {
267
- const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }, 1]
268
- const next = [{ a: 'a' }, new Map(), { c: 'c' }, 2]
269
- const result = replaceEqualDeep(prev, next)
270
- expect(result).not.toBe(prev)
271
- expect(result).not.toBe(next)
272
- expect(result[0]).toBe(prev[0])
273
- expect(result[1]).toBe(next[1])
274
- expect(result[2]).toBe(prev[2])
275
- expect(result[3]).toBe(next[3])
276
- })
277
-
278
- it('should support equal objects which are not arrays or objects', () => {
279
- const map = new Map()
280
- const prev = [map, [1]]
281
- const next = [map, [1]]
282
- const result = replaceEqualDeep(prev, next)
283
- expect(result).toBe(prev)
284
- })
285
-
286
- it('should support non equal objects which are not arrays or objects', () => {
287
- const map1 = new Map()
288
- const map2 = new Map()
289
- const prev = [map1, [1]]
290
- const next = [map2, [1]]
291
- const result = replaceEqualDeep(prev, next)
292
- expect(result).not.toBe(prev)
293
- expect(result).not.toBe(next)
294
- expect(result[0]).toBe(next[0])
295
- expect(result[1]).toBe(prev[1])
296
- })
297
-
298
- it('should support objects which are not plain arrays', () => {
299
- const prev = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
300
- const next = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
301
- const result = replaceEqualDeep(prev, next)
302
- expect(result).toBe(next)
303
- })
304
-
305
- it('should replace all parent objects if some nested value changes', () => {
306
- const prev = {
307
- todo: { id: '1', meta: { createdAt: 0 }, state: { done: false } },
308
- otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
309
- }
310
- const next = {
311
- todo: { id: '1', meta: { createdAt: 0 }, state: { done: true } },
312
- otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
313
- }
314
- const result = replaceEqualDeep(prev, next)
315
- expect(result).toEqual(next)
316
- expect(result).not.toBe(prev)
317
- expect(result).not.toBe(next)
318
- expect(result.todo).not.toBe(prev.todo)
319
- expect(result.todo).not.toBe(next.todo)
320
- expect(result.todo.id).toBe(next.todo.id)
321
- expect(result.todo.meta).toBe(prev.todo.meta)
322
- expect(result.todo.state).not.toBe(next.todo.state)
323
- expect(result.todo.state.done).toBe(next.todo.state.done)
324
- expect(result.otherTodo).toBe(prev.otherTodo)
325
- })
326
-
327
- it('should replace all parent arrays if some nested value changes', () => {
328
- const prev = {
329
- todos: [
330
- { id: '1', meta: { createdAt: 0 }, state: { done: false } },
331
- { id: '2', meta: { createdAt: 0 }, state: { done: true } },
332
- ],
333
- }
334
- const next = {
335
- todos: [
336
- { id: '1', meta: { createdAt: 0 }, state: { done: true } },
337
- { id: '2', meta: { createdAt: 0 }, state: { done: true } },
338
- ],
339
- }
340
- const result = replaceEqualDeep(prev, next)
341
- expect(result).toEqual(next)
342
- expect(result).not.toBe(prev)
343
- expect(result).not.toBe(next)
344
- expect(result.todos).not.toBe(prev.todos)
345
- expect(result.todos).not.toBe(next.todos)
346
- expect(result.todos[0]).not.toBe(prev.todos[0])
347
- expect(result.todos[0]).not.toBe(next.todos[0])
348
- expect(result.todos[0]?.id).toBe(next.todos[0]?.id)
349
- expect(result.todos[0]?.meta).toBe(prev.todos[0]?.meta)
350
- expect(result.todos[0]?.state).not.toBe(next.todos[0]?.state)
351
- expect(result.todos[0]?.state.done).toBe(next.todos[0]?.state.done)
352
- expect(result.todos[1]).toBe(prev.todos[1])
353
- })
354
-
355
- it('should correctly handle objects with the same number of properties and one property being undefined', () => {
356
- const obj1 = { a: 2, c: 123 }
357
- const obj2 = { a: 2, b: undefined }
358
- const result = replaceEqualDeep(obj1, obj2)
359
- expect(result).toStrictEqual(obj2)
360
- })
361
-
362
- it('should be able to share values that contain undefined', () => {
363
- const current = [
364
- {
365
- data: undefined,
366
- foo: true,
367
- },
368
- ]
369
-
370
- const next = replaceEqualDeep(current, [
371
- {
372
- data: undefined,
373
- foo: true,
374
- },
375
- ])
376
-
377
- expect(current).toBe(next)
378
- })
379
-
380
- it('should return the previous value when both values are an array of undefined', () => {
381
- const current = [undefined]
382
- const next = replaceEqualDeep(current, [undefined])
383
-
384
- expect(next).toBe(current)
385
- })
386
-
387
- it('should return the previous value when both values are an array that contains undefined', () => {
388
- const current = [{ foo: 1 }, undefined]
389
- const next = replaceEqualDeep(current, [{ foo: 1 }, undefined])
390
-
391
- expect(next).toBe(current)
392
- })
393
- })
394
-
395
- describe('matchMutation', () => {
396
- it('should return false if mutationKey options is undefined', () => {
397
- const filters = { mutationKey: ['key1'] }
398
- const queryClient = createQueryClient()
399
- const mutation = new Mutation({
400
- mutationId: 1,
401
- mutationCache: queryClient.getMutationCache(),
402
- options: {},
403
- })
404
- expect(matchMutation(filters, mutation)).toBeFalsy()
405
- })
406
- })
407
-
408
- describe('addToEnd', () => {
409
- it('should add item to the end of the array', () => {
410
- const items = [1, 2, 3]
411
- const newItems = addToEnd(items, 4)
412
- expect(newItems).toEqual([1, 2, 3, 4])
413
- })
414
-
415
- it('should not exceed max if provided', () => {
416
- const items = [1, 2, 3]
417
- const newItems = addToEnd(items, 4, 3)
418
- expect(newItems).toEqual([2, 3, 4])
419
- })
420
-
421
- it('should add item to the end of the array when max = 0', () => {
422
- const items = [1, 2, 3]
423
- const item = 4
424
- const max = 0
425
- expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
426
- })
427
-
428
- it('should add item to the end of the array when max is undefined', () => {
429
- const items = [1, 2, 3]
430
- const item = 4
431
- const max = undefined
432
- expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
433
- })
434
- })
435
-
436
- describe('addToStart', () => {
437
- it('should add an item to the start of the array', () => {
438
- const items = [1, 2, 3]
439
- const item = 4
440
- const newItems = addToStart(items, item)
441
- expect(newItems).toEqual([4, 1, 2, 3])
442
- })
443
-
444
- it('should respect the max argument', () => {
445
- const items = [1, 2, 3]
446
- const item = 4
447
- const max = 2
448
- const newItems = addToStart(items, item, max)
449
- expect(newItems).toEqual([4, 1, 2])
450
- })
451
-
452
- it('should not remove any items if max = 0', () => {
453
- const items = [1, 2, 3]
454
- const item = 4
455
- const max = 0
456
- const newItems = addToStart(items, item, max)
457
- expect(newItems).toEqual([4, 1, 2, 3])
458
- })
459
-
460
- it('should not remove any items if max is undefined', () => {
461
- const items = [1, 2, 3]
462
- const item = 4
463
- const max = 0
464
- const newItems = addToStart(items, item, max)
465
- expect(newItems).toEqual([4, 1, 2, 3])
466
- })
467
- })
468
- })
@@ -1,59 +0,0 @@
1
- import { vi } from 'vitest'
2
- import { QueryClient, onlineManager } from '..'
3
- import * as utils from '../utils'
4
- import type { MockInstance } from 'vitest'
5
- import type { MutationOptions, QueryClientConfig } from '..'
6
-
7
- export function createQueryClient(config?: QueryClientConfig): QueryClient {
8
- return new QueryClient(config)
9
- }
10
-
11
- export function mockVisibilityState(
12
- value: DocumentVisibilityState,
13
- ): MockInstance<() => DocumentVisibilityState> {
14
- return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
15
- }
16
-
17
- export function mockOnlineManagerIsOnline(
18
- value: boolean,
19
- ): MockInstance<() => boolean> {
20
- return vi.spyOn(onlineManager, 'isOnline').mockReturnValue(value)
21
- }
22
-
23
- let queryKeyCount = 0
24
- export function queryKey(): Array<string> {
25
- queryKeyCount++
26
- return [`query_${queryKeyCount}`]
27
- }
28
-
29
- export function sleep(timeout: number): Promise<void> {
30
- return new Promise((resolve, _reject) => {
31
- setTimeout(resolve, timeout)
32
- })
33
- }
34
-
35
- export function executeMutation<TVariables>(
36
- queryClient: QueryClient,
37
- options: MutationOptions<any, any, TVariables, any>,
38
- variables: TVariables,
39
- ) {
40
- return queryClient
41
- .getMutationCache()
42
- .build(queryClient, options)
43
- .execute(variables)
44
- }
45
-
46
- // This monkey-patches the isServer-value from utils,
47
- // so that we can pretend to be in a server environment
48
- export function setIsServer(isServer: boolean) {
49
- const original = utils.isServer
50
- Object.defineProperty(utils, 'isServer', {
51
- get: () => isServer,
52
- })
53
-
54
- return () => {
55
- Object.defineProperty(utils, 'isServer', {
56
- get: () => original,
57
- })
58
- }
59
- }