@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
2
2
import {
3
3
addToEnd ,
4
4
addToStart ,
5
+ hashKey ,
5
6
isPlainArray ,
6
7
isPlainObject ,
7
8
keepPreviousData ,
@@ -473,4 +474,37 @@ describe('core/utils', () => {
473
474
expect ( newItems ) . toEqual ( [ 4 , 1 , 2 , 3 ] )
474
475
} )
475
476
} )
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
+ } )
476
510
} )
0 commit comments