@@ -343,6 +343,34 @@ describe('Sorted State Adapter', () => {
343343 } )
344344 } )
345345
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+
346374 it ( 'should let you update many entities by id in the state' , ( ) => {
347375 const firstChange = { title : 'Zack' }
348376 const secondChange = { title : 'Aaron' }
@@ -652,12 +680,20 @@ describe('Sorted State Adapter', () => {
652680 test ( 'updateMany' , ( ) => {
653681 const firstChange = { title : 'First Change' }
654682 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+ ] )
656690
657691 const result = createNextState ( withMany , ( draft ) => {
658692 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 } ,
661697 ] )
662698 } )
663699
@@ -666,14 +702,20 @@ describe('Sorted State Adapter', () => {
666702 "entities": Object {
667703 "aco": Object {
668704 "id": "aco",
669- "title": "Second Change",
705+ "title": "Third Change",
670706 },
671707 "tgg": Object {
672708 "id": "tgg",
709+ "title": "Second Change",
710+ },
711+ "th": Object {
712+ "author": "Fourth Change",
713+ "id": "th",
673714 "title": "First Change",
674715 },
675716 },
676717 "ids": Array [
718+ "th",
677719 "tgg",
678720 "aco",
679721 ],
0 commit comments