@@ -29,16 +29,19 @@ import { createObjectMatcher } from '../testUtils'
29
29
function parseWithIfTransform (
30
30
template : string ,
31
31
options : CompilerOptions = { } ,
32
- returnIndex : number = 0
32
+ returnIndex : number = 0 ,
33
+ childrenLen : number = 1
33
34
) {
34
35
const ast = parse ( template , options )
35
36
transform ( ast , {
36
37
nodeTransforms : [ transformIf , transformSlotOutlet , transformElement ] ,
37
38
...options
38
39
} )
39
40
if ( ! options . onError ) {
40
- expect ( ast . children . length ) . toBe ( 1 )
41
- expect ( ast . children [ 0 ] . type ) . toBe ( NodeTypes . IF )
41
+ expect ( ast . children . length ) . toBe ( childrenLen )
42
+ for ( let i = 0 ; i < childrenLen ; i ++ ) {
43
+ expect ( ast . children [ i ] . type ) . toBe ( NodeTypes . IF )
44
+ }
42
45
}
43
46
return {
44
47
root : ast ,
@@ -459,6 +462,68 @@ describe('compiler: v-if', () => {
459
462
expect ( generate ( root ) . code ) . toMatchSnapshot ( )
460
463
} )
461
464
465
+ test ( 'multiple v-if that are sibling nodes should have different keys' , ( ) => {
466
+ const { root } = parseWithIfTransform (
467
+ `<div v-if="ok"/><p v-if="orNot"/>` ,
468
+ { } ,
469
+ 0 /* returnIndex, just give the default value */ ,
470
+ 2 /* childrenLen */
471
+ )
472
+
473
+ const ifNode = root . children [ 0 ] as IfNode & {
474
+ codegenNode : IfConditionalExpression
475
+ }
476
+ expect ( ifNode . codegenNode . consequent ) . toMatchObject ( {
477
+ tag : `"div"` ,
478
+ props : createObjectMatcher ( { key : `[0]` } )
479
+ } )
480
+ const ifNode2 = root . children [ 1 ] as IfNode & {
481
+ codegenNode : IfConditionalExpression
482
+ }
483
+ expect ( ifNode2 . codegenNode . consequent ) . toMatchObject ( {
484
+ tag : `"p"` ,
485
+ props : createObjectMatcher ( { key : `[1]` } )
486
+ } )
487
+ expect ( generate ( root ) . code ) . toMatchSnapshot ( )
488
+ } )
489
+
490
+ test ( 'increasing key: v-if + v-else-if + v-else' , ( ) => {
491
+ const { root } = parseWithIfTransform (
492
+ `<div v-if="ok"/><p v-else/><div v-if="another"/><p v-else-if="orNot"/><p v-else/>` ,
493
+ { } ,
494
+ 0 /* returnIndex, just give the default value */ ,
495
+ 2 /* childrenLen */
496
+ )
497
+ const ifNode = root . children [ 0 ] as IfNode & {
498
+ codegenNode : IfConditionalExpression
499
+ }
500
+ expect ( ifNode . codegenNode . consequent ) . toMatchObject ( {
501
+ tag : `"div"` ,
502
+ props : createObjectMatcher ( { key : `[0]` } )
503
+ } )
504
+ expect ( ifNode . codegenNode . alternate ) . toMatchObject ( {
505
+ tag : `"p"` ,
506
+ props : createObjectMatcher ( { key : `[1]` } )
507
+ } )
508
+ const ifNode2 = root . children [ 1 ] as IfNode & {
509
+ codegenNode : IfConditionalExpression
510
+ }
511
+ expect ( ifNode2 . codegenNode . consequent ) . toMatchObject ( {
512
+ tag : `"div"` ,
513
+ props : createObjectMatcher ( { key : `[2]` } )
514
+ } )
515
+ const branch = ifNode2 . codegenNode . alternate as IfConditionalExpression
516
+ expect ( branch . consequent ) . toMatchObject ( {
517
+ tag : `"p"` ,
518
+ props : createObjectMatcher ( { key : `[3]` } )
519
+ } )
520
+ expect ( branch . alternate ) . toMatchObject ( {
521
+ tag : `"p"` ,
522
+ props : createObjectMatcher ( { key : `[4]` } )
523
+ } )
524
+ expect ( generate ( root ) . code ) . toMatchSnapshot ( )
525
+ } )
526
+
462
527
test ( 'key injection (only v-bind)' , ( ) => {
463
528
const {
464
529
node : { codegenNode }
0 commit comments