Skip to content

Commit e85df7a

Browse files
authored
test(query-core): add hashKey test code (#8735)
* test: add hashKey test code * fix: testcode
1 parent 18e357c commit e85df7a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

packages/query-core/src/__tests__/utils.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
22
import {
33
addToEnd,
44
addToStart,
5+
hashKey,
56
isPlainArray,
67
isPlainObject,
78
keepPreviousData,
@@ -473,4 +474,37 @@ describe('core/utils', () => {
473474
expect(newItems).toEqual([4, 1, 2, 3])
474475
})
475476
})
477+
478+
describe('hashKey', () => {
479+
it('should hash primitives correctly', () => {
480+
expect(hashKey(['test'])).toEqual(JSON.stringify(['test']))
481+
expect(hashKey([123])).toEqual(JSON.stringify([123]))
482+
expect(hashKey([null])).toEqual(JSON.stringify([null]))
483+
})
484+
485+
it('should hash objects with sorted keys consistently', () => {
486+
const key1 = [{ b: 2, a: 1 }]
487+
const key2 = [{ a: 1, b: 2 }]
488+
489+
const hash1 = hashKey(key1)
490+
const hash2 = hashKey(key2)
491+
492+
expect(hash1).toEqual(hash2)
493+
expect(hash1).toEqual(JSON.stringify([{ a: 1, b: 2 }]))
494+
})
495+
496+
it('should hash arrays consistently', () => {
497+
const arr1 = [{ b: 2, a: 1 }, 'test', 123]
498+
const arr2 = [{ a: 1, b: 2 }, 'test', 123]
499+
500+
expect(hashKey(arr1)).toEqual(hashKey(arr2))
501+
})
502+
503+
it('should handle nested objects with sorted keys', () => {
504+
const nested1 = [{ a: { d: 4, c: 3 }, b: 2 }]
505+
const nested2 = [{ b: 2, a: { c: 3, d: 4 } }]
506+
507+
expect(hashKey(nested1)).toEqual(hashKey(nested2))
508+
})
509+
})
476510
})

0 commit comments

Comments
 (0)