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