@@ -343,6 +343,34 @@ describe('Sorted State Adapter', () => {
343
343
} )
344
344
} )
345
345
346
+ it ( 'should maintain a stable sorting order when updating items' , ( ) => {
347
+ interface OrderedEntity {
348
+ id : string
349
+ order : number
350
+ ts : number
351
+ }
352
+ const sortedItemsAdapter = createEntityAdapter < OrderedEntity > ( {
353
+ sortComparer : ( a , b ) => a . order - b . order ,
354
+ } )
355
+ const withInitialItems = sortedItemsAdapter . setAll (
356
+ sortedItemsAdapter . getInitialState ( ) ,
357
+ [
358
+ { id : 'A' , order : 1 , ts : 0 } ,
359
+ { id : 'B' , order : 2 , ts : 0 } ,
360
+ { id : 'C' , order : 3 , ts : 0 } ,
361
+ { id : 'D' , order : 3 , ts : 0 } ,
362
+ { id : 'E' , order : 3 , ts : 0 } ,
363
+ ]
364
+ )
365
+
366
+ const updated = sortedItemsAdapter . updateOne ( withInitialItems , {
367
+ id : 'C' ,
368
+ changes : { ts : 5 } ,
369
+ } )
370
+
371
+ expect ( updated . ids ) . toEqual ( [ 'A' , 'B' , 'C' , 'D' , 'E' ] )
372
+ } )
373
+
346
374
it ( 'should let you update many entities by id in the state' , ( ) => {
347
375
const firstChange = { title : 'Zack' }
348
376
const secondChange = { title : 'Aaron' }
@@ -652,12 +680,20 @@ describe('Sorted State Adapter', () => {
652
680
test ( 'updateMany' , ( ) => {
653
681
const firstChange = { title : 'First Change' }
654
682
const secondChange = { title : 'Second Change' }
655
- const withMany = adapter . setAll ( state , [ TheGreatGatsby , AClockworkOrange ] )
683
+ const thirdChange = { title : 'Third Change' }
684
+ const fourthChange = { author : 'Fourth Change' }
685
+ const withMany = adapter . setAll ( state , [
686
+ TheGreatGatsby ,
687
+ AClockworkOrange ,
688
+ TheHobbit ,
689
+ ] )
656
690
657
691
const result = createNextState ( withMany , ( draft ) => {
658
692
adapter . updateMany ( draft , [
659
- { id : TheGreatGatsby . id , changes : firstChange } ,
660
- { id : AClockworkOrange . id , changes : secondChange } ,
693
+ { id : TheHobbit . id , changes : firstChange } ,
694
+ { id : TheGreatGatsby . id , changes : secondChange } ,
695
+ { id : AClockworkOrange . id , changes : thirdChange } ,
696
+ { id : TheHobbit . id , changes : fourthChange } ,
661
697
] )
662
698
} )
663
699
@@ -666,14 +702,20 @@ describe('Sorted State Adapter', () => {
666
702
"entities": Object {
667
703
"aco": Object {
668
704
"id": "aco",
669
- "title": "Second Change",
705
+ "title": "Third Change",
670
706
},
671
707
"tgg": Object {
672
708
"id": "tgg",
709
+ "title": "Second Change",
710
+ },
711
+ "th": Object {
712
+ "author": "Fourth Change",
713
+ "id": "th",
673
714
"title": "First Change",
674
715
},
675
716
},
676
717
"ids": Array [
718
+ "th",
677
719
"tgg",
678
720
"aco",
679
721
],
0 commit comments