Skip to content

Commit

Permalink
test(query-core): add hashKey test code (#8735)
Browse files Browse the repository at this point in the history
* test: add hashKey test code

* fix: testcode
  • Loading branch information
Collection50 authored Mar 3, 2025
1 parent 18e357c commit e85df7a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/query-core/src/__tests__/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import {
addToEnd,
addToStart,
hashKey,
isPlainArray,
isPlainObject,
keepPreviousData,
Expand Down Expand Up @@ -473,4 +474,37 @@ describe('core/utils', () => {
expect(newItems).toEqual([4, 1, 2, 3])
})
})

describe('hashKey', () => {
it('should hash primitives correctly', () => {
expect(hashKey(['test'])).toEqual(JSON.stringify(['test']))
expect(hashKey([123])).toEqual(JSON.stringify([123]))
expect(hashKey([null])).toEqual(JSON.stringify([null]))
})

it('should hash objects with sorted keys consistently', () => {
const key1 = [{ b: 2, a: 1 }]
const key2 = [{ a: 1, b: 2 }]

const hash1 = hashKey(key1)
const hash2 = hashKey(key2)

expect(hash1).toEqual(hash2)
expect(hash1).toEqual(JSON.stringify([{ a: 1, b: 2 }]))
})

it('should hash arrays consistently', () => {
const arr1 = [{ b: 2, a: 1 }, 'test', 123]
const arr2 = [{ a: 1, b: 2 }, 'test', 123]

expect(hashKey(arr1)).toEqual(hashKey(arr2))
})

it('should handle nested objects with sorted keys', () => {
const nested1 = [{ a: { d: 4, c: 3 }, b: 2 }]
const nested2 = [{ b: 2, a: { c: 3, d: 4 } }]

expect(hashKey(nested1)).toEqual(hashKey(nested2))
})
})
})

0 comments on commit e85df7a

Please sign in to comment.