@@ -262,7 +262,11 @@ test("restore tree variables in children", () => {
262
262
</ $ . Box >
263
263
</ $ . Body >
264
264
) ;
265
- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
265
+ rebindTreeVariablesMutable ( {
266
+ startingInstanceId : "boxId" ,
267
+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
268
+ ...data ,
269
+ } ) ;
266
270
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
267
271
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
268
272
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -290,7 +294,11 @@ test("restore tree variables in props", () => {
290
294
</ $ . Box >
291
295
</ $ . Body >
292
296
) ;
293
- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
297
+ rebindTreeVariablesMutable ( {
298
+ startingInstanceId : "boxId" ,
299
+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
300
+ ...data ,
301
+ } ) ;
294
302
const [ _bodyVariableId , boxOneVariableId , boxTwoVariableId ] =
295
303
data . dataSources . keys ( ) ;
296
304
const boxOneIdentifier = encodeDataVariableId ( boxOneVariableId ) ;
@@ -337,7 +345,11 @@ test("rebind tree variables in props and children", () => {
337
345
</ $ . Box >
338
346
</ $ . Body >
339
347
) ;
340
- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
348
+ rebindTreeVariablesMutable ( {
349
+ startingInstanceId : "boxId" ,
350
+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
351
+ ...data ,
352
+ } ) ;
341
353
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
342
354
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
343
355
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -358,6 +370,32 @@ test("rebind tree variables in props and children", () => {
358
370
] ) ;
359
371
} ) ;
360
372
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
+
361
399
test ( "restore tree variables in resources" , ( ) => {
362
400
const bodyVariable = new Variable ( "one" , "one value of body" ) ;
363
401
const boxVariable = new Variable ( "one" , "one value of box" ) ;
@@ -384,7 +422,11 @@ test("restore tree variables in resources", () => {
384
422
</ $ . Box >
385
423
</ $ . Body >
386
424
) ;
387
- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
425
+ rebindTreeVariablesMutable ( {
426
+ startingInstanceId : "boxId" ,
427
+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
428
+ ...data ,
429
+ } ) ;
388
430
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
389
431
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
390
432
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -434,7 +476,11 @@ test("rebind tree variables in resources", () => {
434
476
</ $ . Box >
435
477
</ $ . Body >
436
478
) ;
437
- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
479
+ rebindTreeVariablesMutable ( {
480
+ startingInstanceId : "boxId" ,
481
+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
482
+ ...data ,
483
+ } ) ;
438
484
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
439
485
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
440
486
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -458,6 +504,55 @@ test("rebind tree variables in resources", () => {
458
504
] ) ;
459
505
} ) ;
460
506
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
+
461
556
test ( "prevent rebinding tree variables from slots" , ( ) => {
462
557
const bodyVariable = new Variable ( "myVariable" , "one value of body" ) ;
463
558
const data = renderData (
@@ -469,7 +564,11 @@ test("prevent rebinding tree variables from slots", () => {
469
564
</ $ . Slot >
470
565
</ $ . Body >
471
566
) ;
472
- rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
567
+ rebindTreeVariablesMutable ( {
568
+ startingInstanceId : "boxId" ,
569
+ pages : createDefaultPages ( { rootInstanceId : "bodyId" } ) ,
570
+ ...data ,
571
+ } ) ;
473
572
expect ( data . instances . get ( "boxId" ) ?. children ) . toEqual ( [
474
573
{ type : "expression" , value : "myVariable" } ,
475
574
] ) ;
0 commit comments