Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

utils.test.tsx

533 lines17.8 KB

1import { describe, expect, it, vi } from 'vitest'
2import {
3 addToEnd,
4 addToStart,
5 hashKey,
6 hashQueryKeyByOptions,
7 isPlainArray,
8 isPlainObject,
9 keepPreviousData,
10 matchMutation,
11 partialMatchKey,
12 replaceEqualDeep,
13 shallowEqualObjects,
14} from '../utils'
15import { Mutation } from '../mutation'
16import { createQueryClient } from './utils'
17​
18describe('core/utils', () => {
19 describe('hashQueryKeyByOptions', () => {
20 it('should use custom hash function when provided in options', () => {
21 const queryKey = ['test', { a: 1, b: 2 }]
22 const customHashFn = vi.fn(() => 'custom-hash')
23​
24 const result = hashQueryKeyByOptions(queryKey, {
25 queryKeyHashFn: customHashFn,
26 })
27​
28 expect(customHashFn).toHaveBeenCalledWith(queryKey)
29 expect(result).toEqual('custom-hash')
30 })
31​
32 it('should use default hash function when no options provided', () => {
33 const queryKey = ['test', { a: 1, b: 2 }]
34 const defaultResult = hashKey(queryKey)
35 const result = hashQueryKeyByOptions(queryKey)
36​
37 expect(result).toEqual(defaultResult)
38 })
39 })
40​
41 describe('shallowEqualObjects', () => {
42 it('should return `true` for shallow equal objects', () => {
43 expect(shallowEqualObjects({ a: 1 }, { a: 1 })).toEqual(true)
44 })
45​
46 it('should return `false` for non shallow equal objects', () => {
47 expect(shallowEqualObjects({ a: 1 }, { a: 2 })).toEqual(false)
48 })
49​
50 it('should return `false` if lengths are not equal', () => {
51 expect(shallowEqualObjects({ a: 1 }, { a: 1, b: 2 })).toEqual(false)
52 })
53​
54 it('should return false if b is undefined', () => {
55 expect(shallowEqualObjects({ a: 1 }, undefined)).toEqual(false)
56 })
57 })
58 describe('isPlainObject', () => {
59 it('should return `true` for a plain object', () => {
60 expect(isPlainObject({})).toEqual(true)
61 })
62​
63 it('should return `false` for an array', () => {
64 expect(isPlainObject([])).toEqual(false)
65 })
66​
67 it('should return `false` for null', () => {
68 expect(isPlainObject(null)).toEqual(false)
69 })
70​
71 it('should return `false` for undefined', () => {
72 expect(isPlainObject(undefined)).toEqual(false)
73 })
74​
75 it('should return `true` for object with an undefined constructor', () => {
76 expect(isPlainObject(Object.create(null))).toBeTruthy()
77 })
78​
79 it('should return `false` if constructor does not have an Object-specific method', () => {
80 class Foo {
81 abc: any
82 constructor() {
83 this.abc = {}
84 }
85 }
86 expect(isPlainObject(new Foo())).toBeFalsy()
87 })
88​
89 it('should return `false` if the object has a modified prototype', () => {
90 function Graph(this: any) {
91 this.vertices = []
92 this.edges = []
93 }
94​
95 Graph.prototype.addVertex = function (v: any) {
96 this.vertices.push(v)
97 }
98​
99 expect(isPlainObject(Object.create(Graph))).toBeFalsy()
100 })
101​
102 it('should return `false` for object with custom prototype', () => {
103 const CustomProto = Object.create({ a: 1 })
104 const obj = Object.create(CustomProto)
105 obj.b = 2
106​
107 expect(isPlainObject(obj)).toBeFalsy()
108 })
109 })
110​
111 describe('isPlainArray', () => {
112 it('should return `true` for plain arrays', () => {
113 expect(isPlainArray([1, 2])).toEqual(true)
114 })
115​
116 it('should return `false` for non plain arrays', () => {
117 expect(isPlainArray(Object.assign([1, 2], { a: 'b' }))).toEqual(false)
118 })
119 })
120​
121 describe('partialMatchKey', () => {
122 it('should return `true` if a includes b', () => {
123 const a = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
124 const b = [{ a: { b: 'b' }, c: 'c', d: [] }]
125 expect(partialMatchKey(a, b)).toEqual(true)
126 })
127​
128 it('should return `false` if a does not include b', () => {
129 const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
130 const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
131 expect(partialMatchKey(a, b)).toEqual(false)
132 })
133​
134 it('should return `true` if array a includes array b', () => {
135 const a = [1, 2, 3]
136 const b = [1, 2]
137 expect(partialMatchKey(a, b)).toEqual(true)
138 })
139​
140 it('should return `false` if a is null and b is not', () => {
141 const a = [null]
142 const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
143 expect(partialMatchKey(a, b)).toEqual(false)
144 })
145​
146 it('should return `false` if a contains null and b is not', () => {
147 const a = [{ a: null, c: 'c', d: [] }]
148 const b = [{ a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }]
149 expect(partialMatchKey(a, b)).toEqual(false)
150 })
151​
152 it('should return `false` if b is null and a is not', () => {
153 const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
154 const b = [null]
155 expect(partialMatchKey(a, b)).toEqual(false)
156 })
157​
158 it('should return `false` if b contains null and a is not', () => {
159 const a = [{ a: { b: 'b' }, c: 'c', d: [] }]
160 const b = [{ a: null, c: 'c', d: [{ d: 'd ' }] }]
161 expect(partialMatchKey(a, b)).toEqual(false)
162 })
163 })
164​
165 describe('replaceEqualDeep', () => {
166 it('should return the previous value when the next value is an equal primitive', () => {
167 expect(replaceEqualDeep(1, 1)).toBe(1)
168 expect(replaceEqualDeep('1', '1')).toBe('1')
169 expect(replaceEqualDeep(true, true)).toBe(true)
170 expect(replaceEqualDeep(false, false)).toBe(false)
171 expect(replaceEqualDeep(null, null)).toBe(null)
172 expect(replaceEqualDeep(undefined, undefined)).toBe(undefined)
173 })
174 it('should return the next value when the previous value is a different value', () => {
175 const date1 = new Date()
176 const date2 = new Date()
177 expect(replaceEqualDeep(1, 0)).toBe(0)
178 expect(replaceEqualDeep(1, 2)).toBe(2)
179 expect(replaceEqualDeep('1', '2')).toBe('2')
180 expect(replaceEqualDeep(true, false)).toBe(false)
181 expect(replaceEqualDeep(false, true)).toBe(true)
182 expect(replaceEqualDeep(date1, date2)).toBe(date2)
183 })
184​
185 it('should return the next value when the previous value is a different type', () => {
186 const array = [1]
187 const object = { a: 'a' }
188 expect(replaceEqualDeep(0, undefined)).toBe(undefined)
189 expect(replaceEqualDeep(undefined, 0)).toBe(0)
190 expect(replaceEqualDeep(2, undefined)).toBe(undefined)
191 expect(replaceEqualDeep(undefined, 2)).toBe(2)
192 expect(replaceEqualDeep(undefined, null)).toBe(null)
193 expect(replaceEqualDeep(null, undefined)).toBe(undefined)
194 expect(replaceEqualDeep({}, undefined)).toBe(undefined)
195 expect(replaceEqualDeep([], undefined)).toBe(undefined)
196 expect(replaceEqualDeep(array, object)).toBe(object)
197 expect(replaceEqualDeep(object, array)).toBe(array)
198 })
199​
200 it('should return the previous value when the next value is an equal array', () => {
201 const prev = [1, 2]
202 const next = [1, 2]
203 expect(replaceEqualDeep(prev, next)).toBe(prev)
204 })
205​
206 it('should return a copy when the previous value is a different array subset', () => {
207 const prev = [1, 2]
208 const next = [1, 2, 3]
209 const result = replaceEqualDeep(prev, next)
210 expect(result).toEqual(next)
211 expect(result).not.toBe(prev)
212 expect(result).not.toBe(next)
213 })
214​
215 it('should return a copy when the previous value is a different array superset', () => {
216 const prev = [1, 2, 3]
217 const next = [1, 2]
218 const result = replaceEqualDeep(prev, next)
219 expect(result).toEqual(next)
220 expect(result).not.toBe(prev)
221 expect(result).not.toBe(next)
222 })
223​
224 it('should return the previous value when the next value is an equal empty array', () => {
225 const prev: Array<any> = []
226 const next: Array<any> = []
227 expect(replaceEqualDeep(prev, next)).toBe(prev)
228 })
229​
230 it('should return the previous value when the next value is an equal empty object', () => {
231 const prev = {}
232 const next = {}
233 expect(replaceEqualDeep(prev, next)).toBe(prev)
234 })
235​
236 it('should return the previous value when the next value is an equal object', () => {
237 const prev = { a: 'a' }
238 const next = { a: 'a' }
239 expect(replaceEqualDeep(prev, next)).toBe(prev)
240 })
241​
242 it('should replace different values in objects', () => {
243 const prev = { a: { b: 'b' }, c: 'c' }
244 const next = { a: { b: 'b' }, c: 'd' }
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.a).toBe(prev.a)
250 expect(result.c).toBe(next.c)
251 })
252​
253 it('should replace different values in arrays', () => {
254 const prev = [1, { a: 'a' }, { b: { b: 'b' } }, [1]] as const
255 const next = [1, { a: 'a' }, { b: { b: 'c' } }, [1]] as const
256 const result = replaceEqualDeep(prev, next)
257 expect(result).toEqual(next)
258 expect(result).not.toBe(prev)
259 expect(result).not.toBe(next)
260 expect(result[0]).toBe(prev[0])
261 expect(result[1]).toBe(prev[1])
262 expect(result[2]).not.toBe(next[2])
263 expect(result[2].b.b).toBe(next[2].b.b)
264 expect(result[3]).toBe(prev[3])
265 })
266​
267 it('should replace different values in arrays when the next value is a subset', () => {
268 const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
269 const next = [{ a: 'a' }, { b: 'b' }]
270 const result = replaceEqualDeep(prev, next)
271 expect(result).toEqual(next)
272 expect(result).not.toBe(prev)
273 expect(result).not.toBe(next)
274 expect(result[0]).toBe(prev[0])
275 expect(result[1]).toBe(prev[1])
276 expect(result[2]).toBeUndefined()
277 })
278​
279 it('should replace different values in arrays when the next value is a superset', () => {
280 const prev = [{ a: 'a' }, { b: 'b' }]
281 const next = [{ a: 'a' }, { b: 'b' }, { c: 'c' }]
282 const result = replaceEqualDeep(prev, next)
283 expect(result).toEqual(next)
284 expect(result).not.toBe(prev)
285 expect(result).not.toBe(next)
286 expect(result[0]).toBe(prev[0])
287 expect(result[1]).toBe(prev[1])
288 expect(result[2]).toBe(next[2])
289 })
290​
291 it('should copy objects which are not arrays or objects', () => {
292 const prev = [{ a: 'a' }, { b: 'b' }, { c: 'c' }, 1]
293 const next = [{ a: 'a' }, new Map(), { c: 'c' }, 2]
294 const result = replaceEqualDeep(prev, next)
295 expect(result).not.toBe(prev)
296 expect(result).not.toBe(next)
297 expect(result[0]).toBe(prev[0])
298 expect(result[1]).toBe(next[1])
299 expect(result[2]).toBe(prev[2])
300 expect(result[3]).toBe(next[3])
301 })
302​
303 it('should support equal objects which are not arrays or objects', () => {
304 const map = new Map()
305 const prev = [map, [1]]
306 const next = [map, [1]]
307 const result = replaceEqualDeep(prev, next)
308 expect(result).toBe(prev)
309 })
310​
311 it('should support non equal objects which are not arrays or objects', () => {
312 const map1 = new Map()
313 const map2 = new Map()
314 const prev = [map1, [1]]
315 const next = [map2, [1]]
316 const result = replaceEqualDeep(prev, next)
317 expect(result).not.toBe(prev)
318 expect(result).not.toBe(next)
319 expect(result[0]).toBe(next[0])
320 expect(result[1]).toBe(prev[1])
321 })
322​
323 it('should support objects which are not plain arrays', () => {
324 const prev = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
325 const next = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
326 const result = replaceEqualDeep(prev, next)
327 expect(result).toBe(next)
328 })
329​
330 it('should replace all parent objects if some nested value changes', () => {
331 const prev = {
332 todo: { id: '1', meta: { createdAt: 0 }, state: { done: false } },
333 otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
334 }
335 const next = {
336 todo: { id: '1', meta: { createdAt: 0 }, state: { done: true } },
337 otherTodo: { id: '2', meta: { createdAt: 0 }, state: { done: true } },
338 }
339 const result = replaceEqualDeep(prev, next)
340 expect(result).toEqual(next)
341 expect(result).not.toBe(prev)
342 expect(result).not.toBe(next)
343 expect(result.todo).not.toBe(prev.todo)
344 expect(result.todo).not.toBe(next.todo)
345 expect(result.todo.id).toBe(next.todo.id)
346 expect(result.todo.meta).toBe(prev.todo.meta)
347 expect(result.todo.state).not.toBe(next.todo.state)
348 expect(result.todo.state.done).toBe(next.todo.state.done)
349 expect(result.otherTodo).toBe(prev.otherTodo)
350 })
351​
352 it('should replace all parent arrays if some nested value changes', () => {
353 const prev = {
354 todos: [
355 { id: '1', meta: { createdAt: 0 }, state: { done: false } },
356 { id: '2', meta: { createdAt: 0 }, state: { done: true } },
357 ],
358 }
359 const next = {
360 todos: [
361 { id: '1', meta: { createdAt: 0 }, state: { done: true } },
362 { id: '2', meta: { createdAt: 0 }, state: { done: true } },
363 ],
364 }
365 const result = replaceEqualDeep(prev, next)
366 expect(result).toEqual(next)
367 expect(result).not.toBe(prev)
368 expect(result).not.toBe(next)
369 expect(result.todos).not.toBe(prev.todos)
370 expect(result.todos).not.toBe(next.todos)
371 expect(result.todos[0]).not.toBe(prev.todos[0])
372 expect(result.todos[0]).not.toBe(next.todos[0])
373 expect(result.todos[0]?.id).toBe(next.todos[0]?.id)
374 expect(result.todos[0]?.meta).toBe(prev.todos[0]?.meta)
375 expect(result.todos[0]?.state).not.toBe(next.todos[0]?.state)
376 expect(result.todos[0]?.state.done).toBe(next.todos[0]?.state.done)
377 expect(result.todos[1]).toBe(prev.todos[1])
378 })
379​
380 it('should correctly handle objects with the same number of properties and one property being undefined', () => {
381 const obj1 = { a: 2, c: 123 }
382 const obj2 = { a: 2, b: undefined }
383 const result = replaceEqualDeep(obj1, obj2)
384 expect(result).toStrictEqual(obj2)
385 })
386​
387 it('should be able to share values that contain undefined', () => {
388 const current = [
389 {
390 data: undefined,
391 foo: true,
392 },
393 ]
394​
395 const next = replaceEqualDeep(current, [
396 {
397 data: undefined,
398 foo: true,
399 },
400 ])
401​
402 expect(current).toBe(next)
403 })
404​
405 it('should return the previous value when both values are an array of undefined', () => {
406 const current = [undefined]
407 const next = replaceEqualDeep(current, [undefined])
408​
409 expect(next).toBe(current)
410 })
411​
412 it('should return the previous value when both values are an array that contains undefined', () => {
413 const current = [{ foo: 1 }, undefined]
414 const next = replaceEqualDeep(current, [{ foo: 1 }, undefined])
415​
416 expect(next).toBe(current)
417 })
418 })
419​
420 describe('matchMutation', () => {
421 it('should return false if mutationKey options is undefined', () => {
422 const filters = { mutationKey: ['key1'] }
423 const queryClient = createQueryClient()
424 const mutation = new Mutation({
425 mutationId: 1,
426 mutationCache: queryClient.getMutationCache(),
427 options: {},
428 })
429 expect(matchMutation(filters, mutation)).toBeFalsy()
430 })
431 })
432​
433 describe('keepPreviousData', () => {
434 it('should return the parameter as is', () => {
435 const x = { a: 1, b: 2 }
436 expect(keepPreviousData(x)).toEqual(x)
437 })
438 })
439​
440 describe('addToEnd', () => {
441 it('should add item to the end of the array', () => {
442 const items = [1, 2, 3]
443 const newItems = addToEnd(items, 4)
444 expect(newItems).toEqual([1, 2, 3, 4])
445 })
446​
447 it('should not exceed max if provided', () => {
448 const items = [1, 2, 3]
449 const newItems = addToEnd(items, 4, 3)
450 expect(newItems).toEqual([2, 3, 4])
451 })
452​
453 it('should add item to the end of the array when max = 0', () => {
454 const items = [1, 2, 3]
455 const item = 4
456 const max = 0
457 expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
458 })
459​
460 it('should add item to the end of the array when max is undefined', () => {
461 const items = [1, 2, 3]
462 const item = 4
463 const max = undefined
464 expect(addToEnd(items, item, max)).toEqual([1, 2, 3, 4])
465 })
466 })
467​
468 describe('addToStart', () => {
469 it('should add an item to the start of the array', () => {
470 const items = [1, 2, 3]
471 const item = 4
472 const newItems = addToStart(items, item)
473 expect(newItems).toEqual([4, 1, 2, 3])
474 })
475​
476 it('should respect the max argument', () => {
477 const items = [1, 2, 3]
478 const item = 4
479 const max = 2
480 const newItems = addToStart(items, item, max)
481 expect(newItems).toEqual([4, 1, 2])
482 })
483​
484 it('should not remove any items if max = 0', () => {
485 const items = [1, 2, 3]
486 const item = 4
487 const max = 0
488 const newItems = addToStart(items, item, max)
489 expect(newItems).toEqual([4, 1, 2, 3])
490 })
491​
492 it('should not remove any items if max is undefined', () => {
493 const items = [1, 2, 3]
494 const item = 4
495 const max = 0
496 const newItems = addToStart(items, item, max)
497 expect(newItems).toEqual([4, 1, 2, 3])
498 })
499 })
500​
501 describe('hashKey', () => {
502 it('should hash primitives correctly', () => {
503 expect(hashKey(['test'])).toEqual(JSON.stringify(['test']))
504 expect(hashKey([123])).toEqual(JSON.stringify([123]))
505 expect(hashKey([null])).toEqual(JSON.stringify([null]))
506 })
507​
508 it('should hash objects with sorted keys consistently', () => {
509 const key1 = [{ b: 2, a: 1 }]
510 const key2 = [{ a: 1, b: 2 }]
511​
512 const hash1 = hashKey(key1)
513 const hash2 = hashKey(key2)
514​
515 expect(hash1).toEqual(hash2)
516 expect(hash1).toEqual(JSON.stringify([{ a: 1, b: 2 }]))
517 })
518​
519 it('should hash arrays consistently', () => {
520 const arr1 = [{ b: 2, a: 1 }, 'test', 123]
521 const arr2 = [{ a: 1, b: 2 }, 'test', 123]
522​
523 expect(hashKey(arr1)).toEqual(hashKey(arr2))
524 })
525​
526 it('should handle nested objects with sorted keys', () => {
527 const nested1 = [{ a: { d: 4, c: 3 }, b: 2 }]
528 const nested2 = [{ b: 2, a: { c: 3, d: 4 } }]
529​
530 expect(hashKey(nested1)).toEqual(hashKey(nested2))
531 })
532 })
533})