@@ -676,4 +676,74 @@ describe('useTreeData', function () {
676
676
expect ( result . current . items [ 1 ] . value ) . toEqual ( initialResult . items [ 2 ] . value ) ;
677
677
expect ( result . current . items [ 2 ] ) . toEqual ( initialResult . items [ 1 ] ) ;
678
678
} ) ;
679
+
680
+
681
+ it ( 'should move an item within its same level before the target' , function ( ) {
682
+ const initialItems = [ ...initial , { name : 'Emily' } , { name : 'Eli' } ] ;
683
+
684
+ let { result} = renderHook ( ( ) =>
685
+ useTreeData ( { initialItems, getChildren, getKey} )
686
+ ) ;
687
+ act ( ( ) => {
688
+ result . current . moveBefore ( 'Eli' , null , 0 ) ;
689
+ } ) ;
690
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'Eli' ) ;
691
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'David' ) ;
692
+ expect ( result . current . items [ 2 ] . key ) . toEqual ( 'Emily' ) ;
693
+ expect ( result . current . items . length ) . toEqual ( initialItems . length ) ;
694
+ } ) ;
695
+
696
+ it ( 'should move an item to a different level before the target' , function ( ) {
697
+ const initialItems = [ ...initial , { name : 'Emily' } , { name : 'Eli' } ] ;
698
+
699
+ let { result} = renderHook ( ( ) =>
700
+ useTreeData ( { initialItems, getChildren, getKey} )
701
+ ) ;
702
+ act ( ( ) => {
703
+ result . current . moveBefore ( 'Eli' , 'David' , 1 ) ;
704
+ } ) ;
705
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'David' ) ;
706
+ expect ( result . current . items [ 0 ] . children [ 0 ] . key ) . toEqual ( 'John' ) ;
707
+ expect ( result . current . items [ 0 ] . children [ 1 ] . key ) . toEqual ( 'Eli' ) ;
708
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'Emily' ) ;
709
+ expect ( result . current . items . length ) . toEqual ( 2 ) ;
710
+ } ) ;
711
+
712
+ it ( 'should move an item to a different level after the target' , function ( ) {
713
+ const initialItems = [ ...initial , { name : 'Emily' } , { name : 'Eli' } ] ;
714
+ let { result} = renderHook ( ( ) =>
715
+ useTreeData ( { initialItems, getChildren, getKey} )
716
+ ) ;
717
+
718
+ act ( ( ) => {
719
+ result . current . moveAfter ( 'Eli' , 'David' , 1 ) ;
720
+ } ) ;
721
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'David' ) ;
722
+
723
+ expect ( result . current . items [ 0 ] . children [ 0 ] . key ) . toEqual ( 'John' ) ;
724
+ expect ( result . current . items [ 0 ] . children [ 1 ] . key ) . toEqual ( 'Sam' ) ;
725
+ expect ( result . current . items [ 0 ] . children [ 2 ] . key ) . toEqual ( 'Eli' ) ;
726
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'Emily' ) ;
727
+ expect ( result . current . items . length ) . toEqual ( 2 ) ;
728
+ } ) ;
729
+
730
+ it ( 'should move an item to a different level at the end when the index is greater than the node list length' , function ( ) {
731
+ const initialItems = [ ...initial , { name : 'Emily' } , { name : 'Eli' } ] ;
732
+ console . log ( 'initialItems' , initialItems [ 0 ] ) ;
733
+ let { result} = renderHook ( ( ) =>
734
+ useTreeData ( { initialItems, getChildren, getKey} )
735
+ ) ;
736
+
737
+ act ( ( ) => {
738
+ result . current . moveAfter ( 'Eli' , 'David' , 100 ) ;
739
+ } ) ;
740
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'David' ) ;
741
+
742
+ expect ( result . current . items [ 0 ] . children [ 0 ] . key ) . toEqual ( 'John' ) ;
743
+ expect ( result . current . items [ 0 ] . children [ 1 ] . key ) . toEqual ( 'Sam' ) ;
744
+ expect ( result . current . items [ 0 ] . children [ 2 ] . key ) . toEqual ( 'Jane' ) ;
745
+ expect ( result . current . items [ 0 ] . children [ 3 ] . key ) . toEqual ( 'Eli' ) ;
746
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'Emily' ) ;
747
+ expect ( result . current . items . length ) . toEqual ( 2 ) ;
748
+ } ) ;
679
749
} ) ;
0 commit comments