Skip to content

Commit 6d6b373

Browse files
committed
fix: should warn unknown components inside <keep-alive>
1 parent f5cd29e commit 6d6b373

File tree

2 files changed

+76
-68
lines changed

2 files changed

+76
-68
lines changed

Diff for: src/core/components/keep-alive.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export default {
8181
},
8282

8383
render () {
84-
const vnode: VNode = getFirstComponentChild(this.$slots.default)
84+
const slot = this.$slots.default
85+
const vnode: VNode = getFirstComponentChild(slot)
8586
const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
8687
if (componentOptions) {
8788
// check pattern
@@ -115,6 +116,6 @@ export default {
115116

116117
vnode.data.keepAlive = true
117118
}
118-
return vnode
119+
return vnode || (slot && slot[0])
119120
}
120121
}

Diff for: test/unit/features/component/component-keep-alive.spec.js

+73-66
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,79 @@ describe('Component keep-alive', () => {
477477
}).then(done)
478478
})
479479

480+
it('max', done => {
481+
const spyA = jasmine.createSpy()
482+
const spyB = jasmine.createSpy()
483+
const spyC = jasmine.createSpy()
484+
const spyAD = jasmine.createSpy()
485+
const spyBD = jasmine.createSpy()
486+
const spyCD = jasmine.createSpy()
487+
488+
function assertCount (calls) {
489+
expect([
490+
spyA.calls.count(),
491+
spyAD.calls.count(),
492+
spyB.calls.count(),
493+
spyBD.calls.count(),
494+
spyC.calls.count(),
495+
spyCD.calls.count()
496+
]).toEqual(calls)
497+
}
498+
499+
const vm = new Vue({
500+
template: `
501+
<keep-alive max="2">
502+
<component :is="n"></component>
503+
</keep-alive>
504+
`,
505+
data: {
506+
n: 'aa'
507+
},
508+
components: {
509+
aa: {
510+
template: '<div>a</div>',
511+
created: spyA,
512+
destroyed: spyAD
513+
},
514+
bb: {
515+
template: '<div>bbb</div>',
516+
created: spyB,
517+
destroyed: spyBD
518+
},
519+
cc: {
520+
template: '<div>ccc</div>',
521+
created: spyC,
522+
destroyed: spyCD
523+
}
524+
}
525+
}).$mount()
526+
527+
assertCount([1, 0, 0, 0, 0, 0])
528+
vm.n = 'bb'
529+
waitForUpdate(() => {
530+
assertCount([1, 0, 1, 0, 0, 0])
531+
vm.n = 'cc'
532+
}).then(() => {
533+
// should prune A because max cache reached
534+
assertCount([1, 1, 1, 0, 1, 0])
535+
vm.n = 'bb'
536+
}).then(() => {
537+
// B should be reused, and made latest
538+
assertCount([1, 1, 1, 0, 1, 0])
539+
vm.n = 'aa'
540+
}).then(() => {
541+
// C should be pruned because B was used last so C is the oldest cached
542+
assertCount([2, 1, 1, 0, 1, 1])
543+
}).then(done)
544+
})
545+
546+
it('should warn unknown component inside', () => {
547+
new Vue({
548+
template: `<keep-alive><foo/></keep-alive>`
549+
}).$mount()
550+
expect(`Unknown custom element: <foo>`).toHaveBeenWarned()
551+
})
552+
480553
if (!isIE9) {
481554
it('with transition-mode out-in', done => {
482555
let next
@@ -946,71 +1019,5 @@ describe('Component keep-alive', () => {
9461019
}).then(done)
9471020
}
9481021
})
949-
950-
it('max', done => {
951-
const spyA = jasmine.createSpy()
952-
const spyB = jasmine.createSpy()
953-
const spyC = jasmine.createSpy()
954-
const spyAD = jasmine.createSpy()
955-
const spyBD = jasmine.createSpy()
956-
const spyCD = jasmine.createSpy()
957-
958-
function assertCount (calls) {
959-
expect([
960-
spyA.calls.count(),
961-
spyAD.calls.count(),
962-
spyB.calls.count(),
963-
spyBD.calls.count(),
964-
spyC.calls.count(),
965-
spyCD.calls.count()
966-
]).toEqual(calls)
967-
}
968-
969-
const vm = new Vue({
970-
template: `
971-
<keep-alive max="2">
972-
<component :is="n"></component>
973-
</keep-alive>
974-
`,
975-
data: {
976-
n: 'aa'
977-
},
978-
components: {
979-
aa: {
980-
template: '<div>a</div>',
981-
created: spyA,
982-
destroyed: spyAD
983-
},
984-
bb: {
985-
template: '<div>bbb</div>',
986-
created: spyB,
987-
destroyed: spyBD
988-
},
989-
cc: {
990-
template: '<div>ccc</div>',
991-
created: spyC,
992-
destroyed: spyCD
993-
}
994-
}
995-
}).$mount()
996-
997-
assertCount([1, 0, 0, 0, 0, 0])
998-
vm.n = 'bb'
999-
waitForUpdate(() => {
1000-
assertCount([1, 0, 1, 0, 0, 0])
1001-
vm.n = 'cc'
1002-
}).then(() => {
1003-
// should prune A because max cache reached
1004-
assertCount([1, 1, 1, 0, 1, 0])
1005-
vm.n = 'bb'
1006-
}).then(() => {
1007-
// B should be reused, and made latest
1008-
assertCount([1, 1, 1, 0, 1, 0])
1009-
vm.n = 'aa'
1010-
}).then(() => {
1011-
// C should be pruned because B was used last so C is the oldest cached
1012-
assertCount([2, 1, 1, 0, 1, 1])
1013-
}).then(done)
1014-
})
10151022
}
10161023
})

0 commit comments

Comments
 (0)