@@ -18,7 +18,6 @@ import {
18
18
unsetExpressionVariables ,
19
19
deleteVariableMutable ,
20
20
} from "./data-variables" ;
21
- import { getInstancePath } from "./awareness" ;
22
21
23
22
test ( "encode data variable name when necessary" , ( ) => {
24
23
expect ( encodeDataVariableName ( "formState" ) ) . toEqual ( "formState" ) ;
@@ -153,10 +152,7 @@ test("find unset variable names", () => {
153
152
</ $ . Body >
154
153
) ;
155
154
expect (
156
- findUnsetVariableNames ( {
157
- instancePath : getInstancePath ( [ "body" ] , data . instances ) ,
158
- ...data ,
159
- } )
155
+ findUnsetVariableNames ( { startingInstanceId : "body" , ...data } )
160
156
) . toEqual ( [
161
157
"one" ,
162
158
"two" ,
@@ -181,10 +177,7 @@ test("restore tree variables in children", () => {
181
177
</ $ . Box >
182
178
</ $ . Body >
183
179
) ;
184
- rebindTreeVariablesMutable ( {
185
- instancePath : getInstancePath ( [ "boxId" , "bodyId" ] , data . instances ) ,
186
- ...data ,
187
- } ) ;
180
+ rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
188
181
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
189
182
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
190
183
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -212,10 +205,7 @@ test("restore tree variables in props", () => {
212
205
</ $ . Box >
213
206
</ $ . Body >
214
207
) ;
215
- rebindTreeVariablesMutable ( {
216
- instancePath : getInstancePath ( [ "boxId" , "bodyId" ] , data . instances ) ,
217
- ...data ,
218
- } ) ;
208
+ rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
219
209
const [ _bodyVariableId , boxOneVariableId , boxTwoVariableId ] =
220
210
data . dataSources . keys ( ) ;
221
211
const boxOneIdentifier = encodeDataVariableId ( boxOneVariableId ) ;
@@ -262,10 +252,7 @@ test("rebind tree variables in props and children", () => {
262
252
</ $ . Box >
263
253
</ $ . Body >
264
254
) ;
265
- rebindTreeVariablesMutable ( {
266
- instancePath : getInstancePath ( [ "boxId" , "bodyId" ] , data . instances ) ,
267
- ...data ,
268
- } ) ;
255
+ rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
269
256
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
270
257
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
271
258
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -312,10 +299,7 @@ test("restore tree variables in resources", () => {
312
299
</ $ . Box >
313
300
</ $ . Body >
314
301
) ;
315
- rebindTreeVariablesMutable ( {
316
- instancePath : getInstancePath ( [ "boxId" , "bodyId" ] , data . instances ) ,
317
- ...data ,
318
- } ) ;
302
+ rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
319
303
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
320
304
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
321
305
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -365,10 +349,7 @@ test("rebind tree variables in resources", () => {
365
349
</ $ . Box >
366
350
</ $ . Body >
367
351
) ;
368
- rebindTreeVariablesMutable ( {
369
- instancePath : getInstancePath ( [ "boxId" , "bodyId" ] , data . instances ) ,
370
- ...data ,
371
- } ) ;
352
+ rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
372
353
expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
373
354
expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
374
355
expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
@@ -392,6 +373,23 @@ test("rebind tree variables in resources", () => {
392
373
] ) ;
393
374
} ) ;
394
375
376
+ test ( "prevent rebinding tree variables from slots" , ( ) => {
377
+ const bodyVariable = new Variable ( "myVariable" , "one value of body" ) ;
378
+ const data = renderData (
379
+ < $ . Body ws :id = "bodyId" data-body-vars = { expression `${ bodyVariable } ` } >
380
+ < $ . Slot ws :id = "slotId" >
381
+ < $ . Fragment ws :id = "fragmentId" >
382
+ < $ . Box ws :id = "boxId" > { expression `myVariable` } </ $ . Box >
383
+ </ $ . Fragment >
384
+ </ $ . Slot >
385
+ </ $ . Body >
386
+ ) ;
387
+ rebindTreeVariablesMutable ( { startingInstanceId : "boxId" , ...data } ) ;
388
+ expect ( data . instances . get ( "boxId" ) ?. children ) . toEqual ( [
389
+ { type : "expression" , value : "myVariable" } ,
390
+ ] ) ;
391
+ } ) ;
392
+
395
393
test ( "delete variable and unset it in expressions" , ( ) => {
396
394
const bodyVariable = new Variable ( "bodyVariable" , "one value of body" ) ;
397
395
const data = renderData (
@@ -465,3 +463,50 @@ test("delete variable and unset it in resources", () => {
465
463
} ) ,
466
464
] ) ;
467
465
} ) ;
466
+
467
+ test ( "rebind expressions with parent variable when delete variable on child" , ( ) => {
468
+ const bodyVariable = new Variable ( "myVariable" , "one value of body" ) ;
469
+ const boxVariable = new Variable ( "myVariable" , "one value of body" ) ;
470
+ const data = renderData (
471
+ < $ . Body ws :id = "bodyId" data-body-vars = { expression `${ bodyVariable } ` } >
472
+ < $ . Box ws :id = "boxId" data-box-vars = { expression `${ boxVariable } ` } >
473
+ < $ . Text ws :id = "textId" > { expression `${ boxVariable } ` } </ $ . Text >
474
+ </ $ . Box >
475
+ </ $ . Body >
476
+ ) ;
477
+ expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
478
+ expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
479
+ expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
480
+ ] ) ;
481
+ const [ bodyVariableId , boxVariableId ] = data . dataSources . keys ( ) ;
482
+ deleteVariableMutable ( data , boxVariableId ) ;
483
+ const bodyIdentifier = encodeDataVariableId ( bodyVariableId ) ;
484
+ expect ( data . instances . get ( "textId" ) ?. children ) . toEqual ( [
485
+ { type : "expression" , value : bodyIdentifier } ,
486
+ ] ) ;
487
+ } ) ;
488
+
489
+ test ( "prevent rebinding with variables outside of slot content scope" , ( ) => {
490
+ const bodyVariable = new Variable ( "myVariable" , "one value of body" ) ;
491
+ const boxVariable = new Variable ( "myVariable" , "one value of body" ) ;
492
+ const data = renderData (
493
+ < $ . Body ws :id = "bodyId" data-body-vars = { expression `${ bodyVariable } ` } >
494
+ < $ . Slot >
495
+ < $ . Fragment >
496
+ < $ . Box ws :id = "boxId" data-box-vars = { expression `${ boxVariable } ` } >
497
+ < $ . Text ws :id = "textId" > { expression `${ boxVariable } ` } </ $ . Text >
498
+ </ $ . Box >
499
+ </ $ . Fragment >
500
+ </ $ . Slot >
501
+ </ $ . Body >
502
+ ) ;
503
+ expect ( Array . from ( data . dataSources . values ( ) ) ) . toEqual ( [
504
+ expect . objectContaining ( { scopeInstanceId : "bodyId" } ) ,
505
+ expect . objectContaining ( { scopeInstanceId : "boxId" } ) ,
506
+ ] ) ;
507
+ const [ _bodyVariableId , boxVariableId ] = data . dataSources . keys ( ) ;
508
+ deleteVariableMutable ( data , boxVariableId ) ;
509
+ expect ( data . instances . get ( "textId" ) ?. children ) . toEqual ( [
510
+ { type : "expression" , value : "myVariable" } ,
511
+ ] ) ;
512
+ } ) ;
0 commit comments