@@ -32,6 +32,7 @@ const timeout = (n: number = 0) => new Promise(r => setTimeout(r, n))
32
32
describe ( 'KeepAlive' , ( ) => {
33
33
let one : ComponentOptions
34
34
let two : ComponentOptions
35
+ let oneTest : ComponentOptions
35
36
let views : Record < string , ComponentOptions >
36
37
let root : TestElement
37
38
@@ -49,6 +50,18 @@ describe('KeepAlive', () => {
49
50
deactivated : vi . fn ( ) ,
50
51
unmounted : vi . fn ( ) ,
51
52
}
53
+ oneTest = {
54
+ name : 'oneTest' ,
55
+ data : ( ) => ( { msg : 'oneTest' } ) ,
56
+ render ( this : any ) {
57
+ return h ( 'div' , this . msg )
58
+ } ,
59
+ created : vi . fn ( ) ,
60
+ mounted : vi . fn ( ) ,
61
+ activated : vi . fn ( ) ,
62
+ deactivated : vi . fn ( ) ,
63
+ unmounted : vi . fn ( ) ,
64
+ }
52
65
two = {
53
66
name : 'two' ,
54
67
data : ( ) => ( { msg : 'two' } ) ,
@@ -63,6 +76,7 @@ describe('KeepAlive', () => {
63
76
}
64
77
views = {
65
78
one,
79
+ oneTest,
66
80
two,
67
81
}
68
82
} )
@@ -369,6 +383,128 @@ describe('KeepAlive', () => {
369
383
assertHookCalls ( two , [ 2 , 2 , 0 , 0 , 2 ] )
370
384
}
371
385
386
+ async function assertNameMatchWithFlag ( props : KeepAliveProps ) {
387
+ const outerRef = ref ( true )
388
+ const viewRef = ref ( 'one' )
389
+ const App = {
390
+ render ( ) {
391
+ return outerRef . value
392
+ ? h ( KeepAlive , props , ( ) => h ( views [ viewRef . value ] ) )
393
+ : null
394
+ } ,
395
+ }
396
+ render ( h ( App ) , root )
397
+
398
+ expect ( serializeInner ( root ) ) . toBe ( `<div>one</div>` )
399
+ assertHookCalls ( one , [ 1 , 1 , 1 , 0 , 0 ] )
400
+ assertHookCalls ( oneTest , [ 0 , 0 , 0 , 0 , 0 ] )
401
+ assertHookCalls ( two , [ 0 , 0 , 0 , 0 , 0 ] )
402
+
403
+ viewRef . value = 'oneTest'
404
+ await nextTick ( )
405
+ expect ( serializeInner ( root ) ) . toBe ( `<div>oneTest</div>` )
406
+ assertHookCalls ( one , [ 1 , 1 , 1 , 1 , 0 ] )
407
+ assertHookCalls ( oneTest , [ 1 , 1 , 1 , 0 , 0 ] )
408
+ assertHookCalls ( two , [ 0 , 0 , 0 , 0 , 0 ] )
409
+
410
+ viewRef . value = 'two'
411
+ await nextTick ( )
412
+ expect ( serializeInner ( root ) ) . toBe ( `<div>two</div>` )
413
+ assertHookCalls ( one , [ 1 , 1 , 1 , 1 , 0 ] )
414
+ assertHookCalls ( oneTest , [ 1 , 1 , 1 , 1 , 0 ] )
415
+ assertHookCalls ( two , [ 1 , 1 , 0 , 0 , 0 ] )
416
+
417
+ viewRef . value = 'one'
418
+ await nextTick ( )
419
+ expect ( serializeInner ( root ) ) . toBe ( `<div>one</div>` )
420
+ assertHookCalls ( one , [ 1 , 1 , 2 , 1 , 0 ] )
421
+ assertHookCalls ( oneTest , [ 1 , 1 , 1 , 1 , 0 ] )
422
+ assertHookCalls ( two , [ 1 , 1 , 0 , 0 , 1 ] )
423
+
424
+ viewRef . value = 'oneTest'
425
+ await nextTick ( )
426
+ expect ( serializeInner ( root ) ) . toBe ( `<div>oneTest</div>` )
427
+ assertHookCalls ( one , [ 1 , 1 , 2 , 2 , 0 ] )
428
+ assertHookCalls ( oneTest , [ 1 , 1 , 2 , 1 , 0 ] )
429
+ assertHookCalls ( two , [ 1 , 1 , 0 , 0 , 1 ] )
430
+
431
+ viewRef . value = 'two'
432
+ await nextTick ( )
433
+ expect ( serializeInner ( root ) ) . toBe ( `<div>two</div>` )
434
+ assertHookCalls ( one , [ 1 , 1 , 2 , 2 , 0 ] )
435
+ assertHookCalls ( oneTest , [ 1 , 1 , 2 , 2 , 0 ] )
436
+ assertHookCalls ( two , [ 2 , 2 , 0 , 0 , 1 ] )
437
+
438
+ // teardown
439
+ outerRef . value = false
440
+ await nextTick ( )
441
+ expect ( serializeInner ( root ) ) . toBe ( `<!---->` )
442
+ assertHookCalls ( one , [ 1 , 1 , 2 , 2 , 1 ] )
443
+ assertHookCalls ( oneTest , [ 1 , 1 , 2 , 2 , 1 ] )
444
+ assertHookCalls ( two , [ 2 , 2 , 0 , 0 , 2 ] )
445
+ }
446
+
447
+ async function assertNameMatchWithFlagExclude ( props : KeepAliveProps ) {
448
+ const outerRef = ref ( true )
449
+ const viewRef = ref ( 'one' )
450
+ const App = {
451
+ render ( ) {
452
+ return outerRef . value
453
+ ? h ( KeepAlive , props , ( ) => h ( views [ viewRef . value ] ) )
454
+ : null
455
+ } ,
456
+ }
457
+ render ( h ( App ) , root )
458
+
459
+ expect ( serializeInner ( root ) ) . toBe ( `<div>one</div>` )
460
+ assertHookCalls ( one , [ 1 , 1 , 0 , 0 , 0 ] )
461
+ assertHookCalls ( oneTest , [ 0 , 0 , 0 , 0 , 0 ] )
462
+ assertHookCalls ( two , [ 0 , 0 , 0 , 0 , 0 ] )
463
+
464
+ viewRef . value = 'oneTest'
465
+ await nextTick ( )
466
+ expect ( serializeInner ( root ) ) . toBe ( `<div>oneTest</div>` )
467
+ assertHookCalls ( one , [ 1 , 1 , 0 , 0 , 1 ] )
468
+ assertHookCalls ( oneTest , [ 1 , 1 , 0 , 0 , 0 ] )
469
+ assertHookCalls ( two , [ 0 , 0 , 0 , 0 , 0 ] )
470
+
471
+ viewRef . value = 'two'
472
+ await nextTick ( )
473
+ expect ( serializeInner ( root ) ) . toBe ( `<div>two</div>` )
474
+ assertHookCalls ( one , [ 1 , 1 , 0 , 0 , 1 ] )
475
+ assertHookCalls ( oneTest , [ 1 , 1 , 0 , 0 , 1 ] )
476
+ assertHookCalls ( two , [ 1 , 1 , 1 , 0 , 0 ] )
477
+
478
+ viewRef . value = 'one'
479
+ await nextTick ( )
480
+ expect ( serializeInner ( root ) ) . toBe ( `<div>one</div>` )
481
+ assertHookCalls ( one , [ 2 , 2 , 0 , 0 , 1 ] )
482
+ assertHookCalls ( oneTest , [ 1 , 1 , 0 , 0 , 1 ] )
483
+ assertHookCalls ( two , [ 1 , 1 , 1 , 1 , 0 ] )
484
+
485
+ viewRef . value = 'oneTest'
486
+ await nextTick ( )
487
+ expect ( serializeInner ( root ) ) . toBe ( `<div>oneTest</div>` )
488
+ assertHookCalls ( one , [ 2 , 2 , 0 , 0 , 2 ] )
489
+ assertHookCalls ( oneTest , [ 2 , 2 , 0 , 0 , 1 ] )
490
+ assertHookCalls ( two , [ 1 , 1 , 1 , 1 , 0 ] )
491
+
492
+ viewRef . value = 'two'
493
+ await nextTick ( )
494
+ expect ( serializeInner ( root ) ) . toBe ( `<div>two</div>` )
495
+ assertHookCalls ( one , [ 2 , 2 , 0 , 0 , 2 ] )
496
+ assertHookCalls ( oneTest , [ 2 , 2 , 0 , 0 , 2 ] )
497
+ assertHookCalls ( two , [ 1 , 1 , 2 , 1 , 0 ] )
498
+
499
+ // teardown
500
+ outerRef . value = false
501
+ await nextTick ( )
502
+ expect ( serializeInner ( root ) ) . toBe ( `<!---->` )
503
+ assertHookCalls ( one , [ 2 , 2 , 0 , 0 , 2 ] )
504
+ assertHookCalls ( oneTest , [ 2 , 2 , 0 , 0 , 2 ] )
505
+ assertHookCalls ( two , [ 1 , 1 , 2 , 2 , 1 ] )
506
+ }
507
+
372
508
describe ( 'props' , ( ) => {
373
509
test ( 'include (string)' , async ( ) => {
374
510
await assertNameMatch ( { include : 'one' } )
@@ -378,6 +514,10 @@ describe('KeepAlive', () => {
378
514
await assertNameMatch ( { include : / ^ o n e $ / } )
379
515
} )
380
516
517
+ test ( 'include (regex with g flag)' , async ( ) => {
518
+ await assertNameMatchWithFlag ( { include : / o n e / g } )
519
+ } )
520
+
381
521
test ( 'include (array)' , async ( ) => {
382
522
await assertNameMatch ( { include : [ 'one' ] } )
383
523
} )
@@ -390,6 +530,10 @@ describe('KeepAlive', () => {
390
530
await assertNameMatch ( { exclude : / ^ t w o $ / } )
391
531
} )
392
532
533
+ test ( 'exclude (regex with a flag)' , async ( ) => {
534
+ await assertNameMatchWithFlagExclude ( { exclude : / o n e / g } )
535
+ } )
536
+
393
537
test ( 'exclude (array)' , async ( ) => {
394
538
await assertNameMatch ( { exclude : [ 'two' ] } )
395
539
} )
0 commit comments