forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeepAlive.spec.ts
906 lines (781 loc) · 23.5 KB
/
KeepAlive.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
import {
h,
TestElement,
nodeOps,
render,
ref,
KeepAlive,
serializeInner,
nextTick,
ComponentOptions,
markRaw,
inject,
defineComponent,
ComponentPublicInstance,
Ref,
cloneVNode,
provide,
defineAsyncComponent,
Component,
createApp,
onActivated
} from '@vue/runtime-test'
import { KeepAliveProps } from '../../src/components/KeepAlive'
const timeout = (n: number = 0) => new Promise(r => setTimeout(r, n))
describe('KeepAlive', () => {
let one: ComponentOptions
let two: ComponentOptions
let views: Record<string, ComponentOptions>
let root: TestElement
beforeEach(() => {
root = nodeOps.createElement('div')
one = {
name: 'one',
data: () => ({ msg: 'one' }),
render(this: any) {
return h('div', this.msg)
},
created: jest.fn(),
mounted: jest.fn(),
activated: jest.fn(),
deactivated: jest.fn(),
unmounted: jest.fn()
}
two = {
name: 'two',
data: () => ({ msg: 'two' }),
render(this: any) {
return h('div', this.msg)
},
created: jest.fn(),
mounted: jest.fn(),
activated: jest.fn(),
deactivated: jest.fn(),
unmounted: jest.fn()
}
views = {
one,
two
}
})
function assertHookCalls(component: any, callCounts: number[]) {
expect([
component.created.mock.calls.length,
component.mounted.mock.calls.length,
component.activated.mock.calls.length,
component.deactivated.mock.calls.length,
component.unmounted.mock.calls.length
]).toEqual(callCounts)
}
test('should preserve state', async () => {
const viewRef = ref('one')
const instanceRef = ref<any>(null)
const App = {
render() {
return h(KeepAlive, null, {
default: () => h(views[viewRef.value], { ref: instanceRef })
})
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>one</div>`)
instanceRef.value.msg = 'changed'
await nextTick()
expect(serializeInner(root)).toBe(`<div>changed</div>`)
viewRef.value = 'two'
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
viewRef.value = 'one'
await nextTick()
expect(serializeInner(root)).toBe(`<div>changed</div>`)
})
test('should call correct lifecycle hooks', async () => {
const toggle = ref(true)
const viewRef = ref('one')
const App = {
render() {
return toggle.value ? h(KeepAlive, () => h(views[viewRef.value])) : null
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>one</div>`)
assertHookCalls(one, [1, 1, 1, 0, 0])
assertHookCalls(two, [0, 0, 0, 0, 0])
// toggle kept-alive component
viewRef.value = 'two'
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
viewRef.value = 'one'
await nextTick()
expect(serializeInner(root)).toBe(`<div>one</div>`)
assertHookCalls(one, [1, 1, 2, 1, 0])
assertHookCalls(two, [1, 1, 1, 1, 0])
viewRef.value = 'two'
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 2, 2, 0])
assertHookCalls(two, [1, 1, 2, 1, 0])
// teardown keep-alive, should unmount all components including cached
toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 2, 2, 1])
assertHookCalls(two, [1, 1, 2, 2, 1])
})
test('should call correct lifecycle hooks when toggle the KeepAlive first', async () => {
const toggle = ref(true)
const viewRef = ref('one')
const App = {
render() {
return toggle.value ? h(KeepAlive, () => h(views[viewRef.value])) : null
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>one</div>`)
assertHookCalls(one, [1, 1, 1, 0, 0])
assertHookCalls(two, [0, 0, 0, 0, 0])
// should unmount 'one' component when toggle the KeepAlive first
toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 1, 1, 1])
assertHookCalls(two, [0, 0, 0, 0, 0])
toggle.value = true
await nextTick()
expect(serializeInner(root)).toBe(`<div>one</div>`)
assertHookCalls(one, [2, 2, 2, 1, 1])
assertHookCalls(two, [0, 0, 0, 0, 0])
// 1. the first time toggle kept-alive component
viewRef.value = 'two'
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [2, 2, 2, 2, 1])
assertHookCalls(two, [1, 1, 1, 0, 0])
// 2. should unmount all components including cached
toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [2, 2, 2, 2, 2])
assertHookCalls(two, [1, 1, 1, 1, 1])
})
test('should call lifecycle hooks on nested components', async () => {
one.render = () => h(two)
const toggle = ref(true)
const App = {
render() {
return h(KeepAlive, () => (toggle.value ? h(one) : null))
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 1, 0, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 1, 0])
toggle.value = true
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 2, 1, 0])
assertHookCalls(two, [1, 1, 2, 1, 0])
toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 2, 2, 0])
assertHookCalls(two, [1, 1, 2, 2, 0])
})
// #1742
test('should call lifecycle hooks on nested components when root component no hooks', async () => {
const two = {
name: 'two',
data: () => ({ msg: 'two' }),
render(this: any) {
return h('div', this.msg)
},
activated: jest.fn()
}
const one = {
name: 'one',
data: () => ({ msg: 'one' }),
render(this: any) {
return h(two)
}
}
const toggle = ref(true)
const App = {
render() {
return h(KeepAlive, () => (toggle.value ? h(one) : null))
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>two</div>`)
expect(two.activated).toHaveBeenCalledTimes(1)
})
test('should call correct hooks for nested keep-alive', async () => {
const toggle2 = ref(true)
one.render = () => h(KeepAlive, () => (toggle2.value ? h(two) : null))
const toggle1 = ref(true)
const App = {
render() {
return h(KeepAlive, () => (toggle1.value ? h(one) : null))
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 1, 0, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
toggle1.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 1, 0])
toggle1.value = true
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 2, 1, 0])
assertHookCalls(two, [1, 1, 2, 1, 0])
// toggle nested instance
toggle2.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 2, 1, 0])
assertHookCalls(two, [1, 1, 2, 2, 0])
toggle2.value = true
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 2, 1, 0])
assertHookCalls(two, [1, 1, 3, 2, 0])
toggle1.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 2, 2, 0])
assertHookCalls(two, [1, 1, 3, 3, 0])
// toggle nested instance when parent is deactivated
toggle2.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 2, 2, 0])
assertHookCalls(two, [1, 1, 3, 3, 0]) // should not be affected
toggle2.value = true
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 2, 2, 0])
assertHookCalls(two, [1, 1, 3, 3, 0]) // should not be affected
toggle1.value = true
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 3, 2, 0])
assertHookCalls(two, [1, 1, 4, 3, 0])
toggle1.value = false
toggle2.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 3, 3, 0])
assertHookCalls(two, [1, 1, 4, 4, 0])
toggle1.value = true
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 4, 3, 0])
assertHookCalls(two, [1, 1, 4, 4, 0]) // should remain inactive
})
async function assertNameMatch(props: KeepAliveProps) {
const outerRef = ref(true)
const viewRef = ref('one')
const App = {
render() {
return outerRef.value
? h(KeepAlive, props, () => h(views[viewRef.value]))
: null
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>one</div>`)
assertHookCalls(one, [1, 1, 1, 0, 0])
assertHookCalls(two, [0, 0, 0, 0, 0])
viewRef.value = 'two'
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 0, 0, 0])
viewRef.value = 'one'
await nextTick()
expect(serializeInner(root)).toBe(`<div>one</div>`)
assertHookCalls(one, [1, 1, 2, 1, 0])
assertHookCalls(two, [1, 1, 0, 0, 1])
viewRef.value = 'two'
await nextTick()
expect(serializeInner(root)).toBe(`<div>two</div>`)
assertHookCalls(one, [1, 1, 2, 2, 0])
assertHookCalls(two, [2, 2, 0, 0, 1])
// teardown
outerRef.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
assertHookCalls(one, [1, 1, 2, 2, 1])
assertHookCalls(two, [2, 2, 0, 0, 2])
}
describe('props', () => {
test('include (string)', async () => {
await assertNameMatch({ include: 'one' })
})
test('include (regex)', async () => {
await assertNameMatch({ include: /^one$/ })
})
test('include (array)', async () => {
await assertNameMatch({ include: ['one'] })
})
test('exclude (string)', async () => {
await assertNameMatch({ exclude: 'two' })
})
test('exclude (regex)', async () => {
await assertNameMatch({ exclude: /^two$/ })
})
test('exclude (array)', async () => {
await assertNameMatch({ exclude: ['two'] })
})
test('include + exclude', async () => {
await assertNameMatch({ include: 'one,two', exclude: 'two' })
})
test('max', async () => {
const spyAC = jest.fn()
const spyBC = jest.fn()
const spyCC = jest.fn()
const spyAA = jest.fn()
const spyBA = jest.fn()
const spyCA = jest.fn()
const spyADA = jest.fn()
const spyBDA = jest.fn()
const spyCDA = jest.fn()
const spyAUM = jest.fn()
const spyBUM = jest.fn()
const spyCUM = jest.fn()
function assertCount(calls: number[]) {
expect([
spyAC.mock.calls.length,
spyAA.mock.calls.length,
spyADA.mock.calls.length,
spyAUM.mock.calls.length,
spyBC.mock.calls.length,
spyBA.mock.calls.length,
spyBDA.mock.calls.length,
spyBUM.mock.calls.length,
spyCC.mock.calls.length,
spyCA.mock.calls.length,
spyCDA.mock.calls.length,
spyCUM.mock.calls.length
]).toEqual(calls)
}
const viewRef = ref('a')
const views: Record<string, ComponentOptions> = {
a: {
render: () => `one`,
created: spyAC,
activated: spyAA,
deactivated: spyADA,
unmounted: spyAUM
},
b: {
render: () => `two`,
created: spyBC,
activated: spyBA,
deactivated: spyBDA,
unmounted: spyBUM
},
c: {
render: () => `three`,
created: spyCC,
activated: spyCA,
deactivated: spyCDA,
unmounted: spyCUM
}
}
const App = {
render() {
return h(KeepAlive, { max: 2 }, () => {
return h(views[viewRef.value])
})
}
}
render(h(App), root)
assertCount([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
viewRef.value = 'b'
await nextTick()
assertCount([1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0])
viewRef.value = 'c'
await nextTick()
// should prune A because max cache reached
assertCount([1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0])
viewRef.value = 'b'
await nextTick()
// B should be reused, and made latest
assertCount([1, 1, 1, 1, 1, 2, 1, 0, 1, 1, 1, 0])
viewRef.value = 'a'
await nextTick()
// C should be pruned because B was used last so C is the oldest cached
assertCount([2, 2, 1, 1, 1, 2, 2, 0, 1, 1, 1, 1])
})
})
describe('cache invalidation', () => {
function setup() {
const viewRef = ref('one')
const includeRef = ref('one,two')
const App = {
render() {
return h(
KeepAlive,
{
include: includeRef.value
},
() => h(views[viewRef.value])
)
}
}
render(h(App), root)
return { viewRef, includeRef }
}
function setupExclude() {
const viewRef = ref('one')
const excludeRef = ref('')
const App = {
render() {
return h(
KeepAlive,
{
exclude: excludeRef.value
},
() => h(views[viewRef.value])
)
}
}
render(h(App), root)
return { viewRef, excludeRef }
}
test('on include change', async () => {
const { viewRef, includeRef } = setup()
viewRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
includeRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 1])
assertHookCalls(two, [1, 1, 1, 0, 0])
viewRef.value = 'one'
await nextTick()
assertHookCalls(one, [2, 2, 1, 1, 1])
assertHookCalls(two, [1, 1, 1, 1, 0])
})
test('on exclude change', async () => {
const { viewRef, excludeRef } = setupExclude()
viewRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
excludeRef.value = 'one'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 1])
assertHookCalls(two, [1, 1, 1, 0, 0])
viewRef.value = 'one'
await nextTick()
assertHookCalls(one, [2, 2, 1, 1, 1])
assertHookCalls(two, [1, 1, 1, 1, 0])
})
test('on include change + view switch', async () => {
const { viewRef, includeRef } = setup()
viewRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
includeRef.value = 'one'
viewRef.value = 'one'
await nextTick()
assertHookCalls(one, [1, 1, 2, 1, 0])
// two should be pruned
assertHookCalls(two, [1, 1, 1, 1, 1])
})
test('on exclude change + view switch', async () => {
const { viewRef, excludeRef } = setupExclude()
viewRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 1, 0])
assertHookCalls(two, [1, 1, 1, 0, 0])
excludeRef.value = 'two'
viewRef.value = 'one'
await nextTick()
assertHookCalls(one, [1, 1, 2, 1, 0])
// two should be pruned
assertHookCalls(two, [1, 1, 1, 1, 1])
})
test('should not prune current active instance', async () => {
const { viewRef, includeRef } = setup()
includeRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 0, 0])
assertHookCalls(two, [0, 0, 0, 0, 0])
viewRef.value = 'two'
await nextTick()
assertHookCalls(one, [1, 1, 1, 0, 1])
assertHookCalls(two, [1, 1, 1, 0, 0])
})
async function assertAnonymous(include: boolean) {
const one = {
name: 'one',
created: jest.fn(),
render: () => 'one'
}
const two = {
// anonymous
created: jest.fn(),
render: () => 'two'
}
const views: any = { one, two }
const viewRef = ref('one')
const App = {
render() {
return h(
KeepAlive,
{
include: include ? 'one' : undefined
},
() => h(views[viewRef.value])
)
}
}
render(h(App), root)
function assert(oneCreateCount: number, twoCreateCount: number) {
expect(one.created.mock.calls.length).toBe(oneCreateCount)
expect(two.created.mock.calls.length).toBe(twoCreateCount)
}
assert(1, 0)
viewRef.value = 'two'
await nextTick()
assert(1, 1)
viewRef.value = 'one'
await nextTick()
assert(1, 1)
viewRef.value = 'two'
await nextTick()
// two should be re-created if include is specified, since it's not matched
// otherwise it should be cached.
assert(1, include ? 2 : 1)
}
// 2.x #6938
test('should not cache anonymous component when include is specified', async () => {
await assertAnonymous(true)
})
test('should cache anonymous components if include is not specified', async () => {
await assertAnonymous(false)
})
// 2.x #7105
test('should not destroy active instance when pruning cache', async () => {
const Foo = {
render: () => 'foo',
unmounted: jest.fn()
}
const includeRef = ref(['foo'])
const App = {
render() {
return h(
KeepAlive,
{
include: includeRef.value
},
() => h(Foo)
)
}
}
render(h(App), root)
// condition: a render where a previous component is reused
includeRef.value = ['foo', 'bar']
await nextTick()
includeRef.value = []
await nextTick()
expect(Foo.unmounted).not.toHaveBeenCalled()
})
test('should update re-activated component if props have changed', async () => {
const Foo = (props: { n: number }) => props.n
const toggle = ref(true)
const n = ref(0)
const App = {
setup() {
return () =>
h(KeepAlive, () => (toggle.value ? h(Foo, { n: n.value }) : null))
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`0`)
toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)
n.value++
await nextTick()
toggle.value = true
await nextTick()
expect(serializeInner(root)).toBe(`1`)
})
})
it('should call correct vnode hooks', async () => {
const Foo = markRaw({
name: 'Foo',
render() {
return h('Foo')
}
})
const Bar = markRaw({
name: 'Bar',
render() {
return h('Bar')
}
})
const spyMounted = jest.fn()
const spyUnmounted = jest.fn()
const RouterView = defineComponent({
setup(_, { slots }) {
const Component = inject<Ref<ComponentPublicInstance>>('component')
const refView = ref()
let componentProps = {
ref: refView,
onVnodeMounted() {
spyMounted()
},
onVnodeUnmounted() {
spyUnmounted()
}
}
return () => {
const child: any = slots.default!({
Component: Component!.value
})[0]
const innerChild = child.children[0]
child.children[0] = cloneVNode(innerChild, componentProps)
return child
}
}
})
let toggle: () => void = () => {}
const App = defineComponent({
setup() {
const component = ref(Foo)
provide('component', component)
toggle = () => {
component.value = component.value === Foo ? Bar : Foo
}
return {
component,
toggle
}
},
render() {
return h(RouterView, null, {
default: ({ Component }: any) => h(KeepAlive, null, [h(Component)])
})
}
})
render(h(App), root)
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(1)
expect(spyUnmounted).toHaveBeenCalledTimes(0)
toggle()
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(2)
expect(spyUnmounted).toHaveBeenCalledTimes(1)
toggle()
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(3)
expect(spyUnmounted).toHaveBeenCalledTimes(2)
render(null, root)
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(3)
expect(spyUnmounted).toHaveBeenCalledTimes(4)
})
// #1513
test('should work with cloned root due to scopeId / fallthrough attrs', async () => {
const viewRef = ref('one')
const instanceRef = ref<any>(null)
const App = {
__scopeId: 'foo',
render: () => {
return h(KeepAlive, null, {
default: () => h(views[viewRef.value], { ref: instanceRef })
})
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div foo>one</div>`)
instanceRef.value.msg = 'changed'
await nextTick()
expect(serializeInner(root)).toBe(`<div foo>changed</div>`)
viewRef.value = 'two'
await nextTick()
expect(serializeInner(root)).toBe(`<div foo>two</div>`)
viewRef.value = 'one'
await nextTick()
expect(serializeInner(root)).toBe(`<div foo>changed</div>`)
})
test('should work with async component', async () => {
let resolve: (comp: Component) => void
const AsyncComp = defineAsyncComponent(
() =>
new Promise(r => {
resolve = r as any
})
)
const toggle = ref(true)
const instanceRef = ref<any>(null)
const App = {
render: () => {
return h(KeepAlive, { include: 'Foo' }, () =>
toggle.value ? h(AsyncComp, { ref: instanceRef }) : null
)
}
}
render(h(App), root)
// async component has not been resolved
expect(serializeInner(root)).toBe('<!---->')
resolve!({
name: 'Foo',
data: () => ({ count: 0 }),
render() {
return h('p', this.count)
}
})
await timeout()
// resolved
expect(serializeInner(root)).toBe('<p>0</p>')
// change state + toggle out
instanceRef.value.count++
toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe('<!---->')
// toggle in, state should be maintained
toggle.value = true
await nextTick()
expect(serializeInner(root)).toBe('<p>1</p>')
})
// #4976
test('handle error in async onActivated', async () => {
const err = new Error('foo')
const handler = jest.fn()
const app = createApp({
setup() {
return () => h(KeepAlive, null, () => h(Child))
}
})
const Child = {
setup() {
onActivated(async () => {
throw err
})
},
render() {}
}
app.config.errorHandler = handler
app.mount(nodeOps.createElement('div'))
await nextTick()
expect(handler).toHaveBeenCalledWith(err, {}, 'activated hook')
})
})