@@ -137,3 +137,54 @@ describe("makeCluster cluster separation logic", () => {
137137 expect ( dist ) . toBeGreaterThan ( 4 ) ; // clusterRadius/2 = 5, allow some tolerance
138138 } ) ;
139139} ) ;
140+
141+ describe ( "Test utils deepCopy function" , ( ) => {
142+ test ( "creates a deep clone that is independent from the original object" , ( ) => {
143+ const util = new NetJSONGraphUtil ( ) ;
144+
145+ const config = {
146+ render : "map" ,
147+ mapOptions : {
148+ center : [ 50 , 50 ] ,
149+ zoom : 5 ,
150+ nodeConfig : { label : { offset : [ 0 , - 10 ] } } ,
151+ } ,
152+ linkCategories : [
153+ {
154+ name : "down" ,
155+ linkStyle : { color : "#c92517" , width : 5 } ,
156+ } ,
157+ ] ,
158+ } ;
159+
160+ const original = config ;
161+ const clone = util . deepCopy ( original ) ;
162+
163+ expect ( clone ) . not . toBe ( original ) ;
164+ expect ( clone . mapOptions ) . not . toBe ( original . mapOptions ) ;
165+ expect ( clone . linkCategories ) . not . toBe ( original . linkCategories ) ;
166+
167+ clone . render = "graph" ;
168+ clone . mapOptions . center = [ 0 , 0 ] ;
169+ clone . mapOptions . zoom = 10 ;
170+ clone . linkCategories [ 0 ] . linkStyle . color = "#000000" ;
171+ clone . linkCategories . push ( {
172+ name : "up" ,
173+ linkStyle : { color : "#00ff00" , width : 2 } ,
174+ } ) ;
175+
176+ expect ( original . render ) . toBe ( "map" ) ;
177+ expect ( clone . render ) . toBe ( "graph" ) ;
178+ expect ( original . mapOptions . center ) . toEqual ( [ 50 , 50 ] ) ;
179+ expect ( clone . mapOptions . center ) . toEqual ( [ 0 , 0 ] ) ;
180+ expect ( original . mapOptions . zoom ) . toBe ( 5 ) ;
181+ expect ( clone . mapOptions . zoom ) . toBe ( 10 ) ;
182+ expect ( original . linkCategories . length ) . toBe ( 1 ) ;
183+ expect ( clone . linkCategories . length ) . toBe ( 2 ) ;
184+ expect ( original . linkCategories [ 0 ] . linkStyle . color ) . toBe ( "#c92517" ) ;
185+ expect ( clone . linkCategories [ 0 ] . linkStyle . color ) . toBe ( "#000000" ) ;
186+ expect ( original . linkCategories [ 0 ] . name ) . toBe ( "down" ) ;
187+ expect ( clone . linkCategories [ 0 ] . name ) . toBe ( "down" ) ;
188+ expect ( clone . linkCategories [ 1 ] . name ) . toBe ( "up" ) ;
189+ } ) ;
190+ } ) ;
0 commit comments