@@ -262,7 +262,11 @@ test("restore tree variables in children", () => {
262262 </ $ . Box >
263263 </ $ . Body >
264264 ) ;
265- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
265+ rebindTreeVariablesMutable ( {
266+ startingInstanceId : "boxId" ,
267+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
268+ ...data ,
269+ } ) ;
266270 expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
267271 expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
268272 expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -290,7 +294,11 @@ test("restore tree variables in props", () => {
290294 </ $ . Box >
291295 </ $ . Body >
292296 ) ;
293- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
297+ rebindTreeVariablesMutable ( {
298+ startingInstanceId : "boxId" ,
299+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
300+ ...data ,
301+ } ) ;
294302 const [ _bodyVariableId , boxOneVariableId , boxTwoVariableId ] =
295303 data . dataSources . keys ( ) ;
296304 const boxOneIdentifier = encodeDataVariableId ( boxOneVariableId ) ;
@@ -337,7 +345,11 @@ test("rebind tree variables in props and children", () => {
337345 </ $ . Box >
338346 </ $ . Body >
339347 ) ;
340- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
348+ rebindTreeVariablesMutable ( {
349+ startingInstanceId : "boxId" ,
350+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
351+ ...data ,
352+ } ) ;
341353 expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
342354 expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
343355 expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -358,6 +370,32 @@ test("rebind tree variables in props and children", () => {
358370 ] ) ;
359371} ) ;
360372
373+ test ( "preserve nested variables with the same name when rebind" , ( ) => {
374+ const bodyVariable = new Variable ( "one" , "one value of body" ) ;
375+ const textVariable = new Variable ( "one" , "one value of box" ) ;
376+ const data = renderData (
377+ < $ . Body ws :id = "bodyId" data-body-vars = { expression `${ bodyVariable } ` } >
378+ < $ . Text ws :id = "textId" data-text-vars = { expression `${ textVariable } ` } >
379+ { expression `${ textVariable } ` }
380+ </ $ . Text >
381+ </ $ . Body >
382+ ) ;
383+ rebindTreeVariablesMutable ( {
384+ startingInstanceId : "bodyId" ,
385+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
386+ ...data ,
387+ } ) ;
388+ expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
389+ expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
390+ expect . objectContaining ( { scopeInstanceId : "textId" } ) ,
391+ ] ) ;
392+ const [ _bodyVariableId , textVariableId ] = data . dataSources . keys ( ) ;
393+ const textIdentifier = encodeDataVariableId ( textVariableId ) ;
394+ expect ( data . instances . get ( "textId" ) ?. children ) . toEqual ( [
395+ { type : "expression" , value : textIdentifier } ,
396+ ] ) ;
397+ } ) ;
398+
361399test ( "restore tree variables in resources" , ( ) => {
362400 const bodyVariable = new Variable ( "one" , "one value of body" ) ;
363401 const boxVariable = new Variable ( "one" , "one value of box" ) ;
@@ -384,7 +422,11 @@ test("restore tree variables in resources", () => {
384422 </ $ . Box >
385423 </ $ . Body >
386424 ) ;
387- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
425+ rebindTreeVariablesMutable ( {
426+ startingInstanceId : "boxId" ,
427+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
428+ ...data ,
429+ } ) ;
388430 expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
389431 expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
390432 expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -434,7 +476,11 @@ test("rebind tree variables in resources", () => {
434476 </ $ . Box >
435477 </ $ . Body >
436478 ) ;
437- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
479+ rebindTreeVariablesMutable ( {
480+ startingInstanceId : "boxId" ,
481+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
482+ ...data ,
483+ } ) ;
438484 expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
439485 expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
440486 expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -458,6 +504,55 @@ test("rebind tree variables in resources", () => {
458504 ] ) ;
459505} ) ;
460506
507+ test ( "rebind global variables in resources" , ( ) => {
508+ const globalVariable = new Variable ( "globalVariable" , "" ) ;
509+ const data = renderData (
510+ < ws . root ws :id = { ROOT_INSTANCE_ID } data-vars = { expression `${ globalVariable } ` } >
511+ < $ . Body ws :id = "bodyId" >
512+ < $ . Text ws :id = "textId" > { expression `globalVariable` } </ $ . Text >
513+ </ $ . Body >
514+ </ ws . root >
515+ ) ;
516+ data . instances . delete ( ROOT_INSTANCE_ID ) ;
517+ rebindTreeVariablesMutable ( {
518+ startingInstanceId : ROOT_INSTANCE_ID ,
519+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
520+ ...data ,
521+ } ) ;
522+ expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
523+ expect . objectContaining ( { scopeInstanceId : ROOT_INSTANCE_ID } ) ,
524+ ] ) ;
525+ const [ globalVariableId ] = data . dataSources . keys ( ) ;
526+ const globalIdentifier = encodeDataVariableId ( globalVariableId ) ;
527+ expect ( data . instances . get ( "textId" ) ?. children ) . toEqual ( [
528+ { type : "expression" , value : globalIdentifier } ,
529+ ] ) ;
530+ } ) ;
531+
532+ test ( "preserve other variables when rebind" , ( ) => {
533+ const bodyVariable = new Variable ( "globalVariable" , "" ) ;
534+ const textVariable = new Variable ( "textVariable" , "" ) ;
535+ const data = renderData (
536+ < $ . Body ws :id = "bodyId" data-vars = { expression `${ bodyVariable } ` } >
537+ < $ . Text ws :id = "textId" > { expression `${ textVariable } ` } </ $ . Text >
538+ </ $ . Body >
539+ ) ;
540+ rebindTreeVariablesMutable ( {
541+ startingInstanceId : "bodyId" ,
542+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
543+ ...data ,
544+ } ) ;
545+ expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
546+ expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
547+ expect . objectContaining ( { scopeInstanceId : "textId" } ) ,
548+ ] ) ;
549+ const [ _globalVariableId , textVariableId ] = data . dataSources . keys ( ) ;
550+ const textIdentifier = encodeDataVariableId ( textVariableId ) ;
551+ expect ( data . instances . get ( "textId" ) ?. children ) . toEqual ( [
552+ { type : "expression" , value : textIdentifier } ,
553+ ] ) ;
554+ } ) ;
555+
461556test ( "prevent rebinding tree variables from slots" , ( ) => {
462557 const bodyVariable = new Variable ( "myVariable" , "one value of body" ) ;
463558 const data = renderData (
@@ -469,7 +564,11 @@ test("prevent rebinding tree variables from slots", () => {
469564 </ $ . Slot >
470565 </ $ . Body >
471566 ) ;
472- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
567+ rebindTreeVariablesMutable ( {
568+ startingInstanceId : "boxId" ,
569+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
570+ ...data ,
571+ } ) ;
473572 expect ( data . instances . get ( "boxId" ) ?. children ) . toEqual ( [
474573 { type : "expression" , value : "myVariable" } ,
475574 ] ) ;
0 commit comments