@@ -62,4 +62,52 @@ describe('selectors -> nodes ->', () => {
62
62
expect ( nodeSelectors . getNodeRenderOptions ( { children : [ { } ] } ) ) . toMatchSnapshot ( ) ;
63
63
} ) ;
64
64
} ) ;
65
+
66
+ describe ( 'getNodeFromPath' , ( ) => {
67
+ test ( 'should get a node from a path in the root of the tree' , ( ) => {
68
+ expect ( nodeSelectors . getNodeFromPath ( [ Nodes [ 0 ] . id ] , Nodes ) ) . toEqual ( Nodes [ 0 ] ) ;
69
+ expect ( nodeSelectors . getNodeFromPath ( [ Nodes [ 1 ] . id ] , Nodes ) ) . toEqual ( Nodes [ 1 ] ) ;
70
+ expect ( nodeSelectors . getNodeFromPath ( [ Nodes [ 2 ] . id ] , Nodes ) ) . toEqual ( Nodes [ 2 ] ) ;
71
+ } ) ;
72
+
73
+ test ( 'should get a node from a path in the first set of children of the tree' , ( ) => {
74
+ expect ( nodeSelectors . getNodeFromPath ( [ Nodes [ 0 ] . id , Nodes [ 0 ] . children [ 1 ] . id ] , Nodes ) ) . toEqual (
75
+ Nodes [ 0 ] . children [ 1 ] ,
76
+ ) ;
77
+ } ) ;
78
+
79
+ test ( 'should get a node from a path deep in the tree' , ( ) => {
80
+ expect (
81
+ nodeSelectors . getNodeFromPath (
82
+ [ Nodes [ 0 ] . id , Nodes [ 0 ] . children [ 0 ] . id , Nodes [ 0 ] . children [ 0 ] . children [ 1 ] . id ] ,
83
+ Nodes ,
84
+ ) ,
85
+ ) . toEqual ( Nodes [ 0 ] . children [ 0 ] . children [ 1 ] ) ;
86
+ } ) ;
87
+
88
+ test ( 'should throw custom error when the path is invalid' , ( ) => {
89
+ expect ( ( ) => nodeSelectors . getNodeFromPath ( '' , Nodes ) ) . toThrowError ( 'path is not an array' ) ;
90
+ expect ( ( ) => nodeSelectors . getNodeFromPath ( { } , Nodes ) ) . toThrowError ( 'path is not an array' ) ;
91
+ expect ( ( ) => nodeSelectors . getNodeFromPath ( 1245 , Nodes ) ) . toThrowError ( 'path is not an array' ) ;
92
+ expect ( ( ) => nodeSelectors . getNodeFromPath ( true , Nodes ) ) . toThrowError ( 'path is not an array' ) ;
93
+ } ) ;
94
+
95
+ test ( 'should throw custom error when path does not exist in a middle node' , ( ) => {
96
+ const { id : existingId1 } = Nodes [ 0 ] ;
97
+ const { id : existingId2 } = Nodes [ 0 ] . children [ 0 ] . children [ 1 ] ;
98
+
99
+ expect ( ( ) => nodeSelectors . getNodeFromPath ( [ existingId1 , 25 , existingId2 ] , Nodes ) ) . toThrowError (
100
+ `Could not find node at ${ existingId1 } ,25,${ existingId2 } ` ,
101
+ ) ;
102
+ } ) ;
103
+
104
+ test ( 'should throw custom error when path does not exist in the final node' , ( ) => {
105
+ const { id : existingId1 } = Nodes [ 0 ] ;
106
+ const { id : existingId2 } = Nodes [ 0 ] . children [ 0 ] ;
107
+
108
+ expect ( ( ) => nodeSelectors . getNodeFromPath ( [ existingId1 , existingId2 , 25 ] , Nodes ) ) . toThrowError (
109
+ `Could not find node at ${ existingId1 } ,${ existingId2 } ,25` ,
110
+ ) ;
111
+ } ) ;
112
+ } ) ;
65
113
} ) ;
0 commit comments