@@ -490,7 +490,7 @@ describe('useTreeData', function () {
490
490
expect ( result . current . items ) . not . toBe ( initialResult . items ) ;
491
491
expect ( result . current . items ) . toHaveLength ( 1 ) ;
492
492
expect ( result . current . items [ 0 ] . value ) . toEqual ( { expanded : true , name : 'Danny' } ) ;
493
- expect ( result . current . items [ 0 ] . parentKey ) . toBeUndefined ( ) ;
493
+ expect ( result . current . items [ 0 ] . parentKey ) . toBeNull ( ) ;
494
494
} ) ;
495
495
496
496
it ( 'should update an item' , function ( ) {
@@ -869,6 +869,165 @@ describe('useTreeData', function () {
869
869
expect ( result . current . items . length ) . toEqual ( 2 ) ;
870
870
} ) ;
871
871
872
+ it ( 'can move an item multiple times' , function ( ) {
873
+ const initialItems = [ ...initial , { name : 'Eli' } ] ;
874
+
875
+ let { result} = renderHook ( ( ) =>
876
+ useTreeData ( { initialItems, getChildren, getKey} )
877
+ ) ;
878
+ act ( ( ) => {
879
+ result . current . moveAfter ( 'Eli' , [ 'David' ] ) ;
880
+ } ) ;
881
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'Eli' ) ;
882
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'David' ) ;
883
+ act ( ( ) => {
884
+ result . current . moveAfter ( 'David' , [ 'Eli' ] ) ;
885
+ } ) ;
886
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'David' ) ;
887
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'Eli' ) ;
888
+ act ( ( ) => {
889
+ result . current . moveAfter ( 'Eli' , [ 'David' ] ) ;
890
+ } ) ;
891
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'Eli' ) ;
892
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'David' ) ;
893
+ act ( ( ) => {
894
+ result . current . moveAfter ( 'David' , [ 'Eli' ] ) ;
895
+ } ) ;
896
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'David' ) ;
897
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'Eli' ) ;
898
+
899
+ // do the same with moveBefore
900
+ act ( ( ) => {
901
+ result . current . moveBefore ( 'David' , [ 'Eli' ] ) ;
902
+ } ) ;
903
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'Eli' ) ;
904
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'David' ) ;
905
+ act ( ( ) => {
906
+ result . current . moveBefore ( 'Eli' , [ 'David' ] ) ;
907
+ } ) ;
908
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'David' ) ;
909
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'Eli' ) ;
910
+ act ( ( ) => {
911
+ result . current . moveBefore ( 'David' , [ 'Eli' ] ) ;
912
+ } ) ;
913
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'Eli' ) ;
914
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'David' ) ;
915
+ act ( ( ) => {
916
+ result . current . moveBefore ( 'Eli' , [ 'David' ] ) ;
917
+ } ) ;
918
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'David' ) ;
919
+ expect ( result . current . items [ 1 ] . key ) . toEqual ( 'Eli' ) ;
920
+ } ) ;
921
+
922
+ it ( 'can move an item within its same level' , function ( ) {
923
+ const initialItems = [
924
+ { id : 'projects' , name : 'Projects' , childItems : [
925
+ { id : 'project-1' , name : 'Project 1' } ,
926
+ { id : 'project-2' , name : 'Project 2' , childItems : [
927
+ { id : 'project-2A' , name : 'Project 2A' } ,
928
+ { id : 'project-2B' , name : 'Project 2B' } ,
929
+ { id : 'project-2C' , name : 'Project 2C' }
930
+ ] } ,
931
+ { id : 'project-3' , name : 'Project 3' } ,
932
+ { id : 'project-4' , name : 'Project 4' } ,
933
+ { id : 'project-5' , name : 'Project 5' , childItems : [
934
+ { id : 'project-5A' , name : 'Project 5A' } ,
935
+ { id : 'project-5B' , name : 'Project 5B' } ,
936
+ { id : 'project-5C' , name : 'Project 5C' }
937
+ ] }
938
+ ] } ,
939
+ { id : 'reports' , name : 'Reports' , childItems : [
940
+ { id : 'reports-1' , name : 'Reports 1' , childItems : [
941
+ { id : 'reports-1A' , name : 'Reports 1A' , childItems : [
942
+ { id : 'reports-1AB' , name : 'Reports 1AB' , childItems : [
943
+ { id : 'reports-1ABC' , name : 'Reports 1ABC' }
944
+ ] }
945
+ ] } ,
946
+ { id : 'reports-1B' , name : 'Reports 1B' } ,
947
+ { id : 'reports-1C' , name : 'Reports 1C' }
948
+ ] } ,
949
+ { id : 'reports-2' , name : 'Reports 2' }
950
+ ] }
951
+ ] ;
952
+
953
+ let { result} = renderHook ( ( ) =>
954
+ useTreeData ( { initialItems, getChildren : ( item ) => item . childItems , getKey : ( item ) => item . id } )
955
+ ) ;
956
+ act ( ( ) => {
957
+ result . current . moveAfter ( 'project-3' , [ 'project-2' ] ) ;
958
+ } ) ;
959
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'projects' ) ;
960
+ expect ( result . current . items [ 0 ] . children [ 0 ] . key ) . toEqual ( 'project-1' ) ;
961
+ expect ( result . current . items [ 0 ] . children [ 1 ] . key ) . toEqual ( 'project-3' ) ;
962
+ expect ( result . current . items [ 0 ] . children [ 2 ] . key ) . toEqual ( 'project-2' ) ;
963
+
964
+ // move again to the same place
965
+ act ( ( ) => {
966
+ result . current . moveAfter ( 'project-3' , [ 'project-2' ] ) ;
967
+ } ) ;
968
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'projects' ) ;
969
+ expect ( result . current . items [ 0 ] . children [ 0 ] . key ) . toEqual ( 'project-1' ) ;
970
+ expect ( result . current . items [ 0 ] . children [ 1 ] . key ) . toEqual ( 'project-3' ) ;
971
+ expect ( result . current . items [ 0 ] . children [ 2 ] . key ) . toEqual ( 'project-2' ) ;
972
+
973
+ // move to a different place
974
+ act ( ( ) => {
975
+ result . current . moveAfter ( 'project-4' , [ 'project-2' ] ) ;
976
+ } ) ;
977
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'projects' ) ;
978
+ expect ( result . current . items [ 0 ] . children [ 0 ] . key ) . toEqual ( 'project-1' ) ;
979
+ expect ( result . current . items [ 0 ] . children [ 1 ] . key ) . toEqual ( 'project-3' ) ;
980
+ expect ( result . current . items [ 0 ] . children [ 2 ] . key ) . toEqual ( 'project-4' ) ;
981
+ expect ( result . current . items [ 0 ] . children [ 3 ] . key ) . toEqual ( 'project-2' ) ;
982
+ } ) ;
983
+
984
+
985
+ it ( 'can move an item to a different level' , function ( ) {
986
+ const initialItems = [
987
+ { id : 'projects' , name : 'Projects' , childItems : [
988
+ { id : 'project-1' , name : 'Project 1' } ,
989
+ { id : 'project-2' , name : 'Project 2' , childItems : [
990
+ { id : 'project-2A' , name : 'Project 2A' } ,
991
+ { id : 'project-2B' , name : 'Project 2B' } ,
992
+ { id : 'project-2C' , name : 'Project 2C' }
993
+ ] } ,
994
+ { id : 'project-3' , name : 'Project 3' } ,
995
+ { id : 'project-4' , name : 'Project 4' } ,
996
+ { id : 'project-5' , name : 'Project 5' , childItems : [
997
+ { id : 'project-5A' , name : 'Project 5A' } ,
998
+ { id : 'project-5B' , name : 'Project 5B' } ,
999
+ { id : 'project-5C' , name : 'Project 5C' }
1000
+ ] }
1001
+ ] } ,
1002
+ { id : 'reports' , name : 'Reports' , childItems : [
1003
+ { id : 'reports-1' , name : 'Reports 1' , childItems : [
1004
+ { id : 'reports-1A' , name : 'Reports 1A' , childItems : [
1005
+ { id : 'reports-1AB' , name : 'Reports 1AB' , childItems : [
1006
+ { id : 'reports-1ABC' , name : 'Reports 1ABC' }
1007
+ ] }
1008
+ ] } ,
1009
+ { id : 'reports-1B' , name : 'Reports 1B' } ,
1010
+ { id : 'reports-1C' , name : 'Reports 1C' }
1011
+ ] } ,
1012
+ { id : 'reports-2' , name : 'Reports 2' }
1013
+ ] }
1014
+ ] ;
1015
+
1016
+ let { result} = renderHook ( ( ) =>
1017
+ useTreeData ( { initialItems, getChildren : ( item ) => item . childItems , getKey : ( item ) => item . id } )
1018
+ ) ;
1019
+ act ( ( ) => {
1020
+ result . current . moveBefore ( 'project-2B' , [ 'project-3' ] ) ;
1021
+ } ) ;
1022
+ expect ( result . current . items [ 0 ] . key ) . toEqual ( 'projects' ) ;
1023
+ expect ( result . current . items [ 0 ] . children [ 0 ] . key ) . toEqual ( 'project-1' ) ;
1024
+ expect ( result . current . items [ 0 ] . children [ 1 ] . key ) . toEqual ( 'project-2' ) ;
1025
+
1026
+ expect ( result . current . items [ 0 ] . children [ 1 ] . children [ 0 ] . key ) . toEqual ( 'project-2A' ) ;
1027
+ expect ( result . current . items [ 0 ] . children [ 1 ] . children [ 1 ] . key ) . toEqual ( 'project-3' ) ;
1028
+ expect ( result . current . items [ 0 ] . children [ 1 ] . children [ 2 ] . key ) . toEqual ( 'project-2B' ) ;
1029
+ } ) ;
1030
+
872
1031
it ( 'should move an item to a different level after the target' , function ( ) {
873
1032
const initialItems = [ ...initial , { name : 'Emily' } , { name : 'Eli' } ] ;
874
1033
let { result} = renderHook ( ( ) =>
0 commit comments