Skip to content

Commit

Permalink
fix: should warn unknown components inside <keep-alive>
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 17, 2017
1 parent f5cd29e commit 6d6b373
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 68 deletions.
5 changes: 3 additions & 2 deletions src/core/components/keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export default {
},

render () {
const vnode: VNode = getFirstComponentChild(this.$slots.default)
const slot = this.$slots.default
const vnode: VNode = getFirstComponentChild(slot)
const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
if (componentOptions) {
// check pattern
Expand Down Expand Up @@ -115,6 +116,6 @@ export default {

vnode.data.keepAlive = true
}
return vnode
return vnode || (slot && slot[0])
}
}
139 changes: 73 additions & 66 deletions test/unit/features/component/component-keep-alive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,79 @@ describe('Component keep-alive', () => {
}).then(done)
})

it('max', done => {
const spyA = jasmine.createSpy()
const spyB = jasmine.createSpy()
const spyC = jasmine.createSpy()
const spyAD = jasmine.createSpy()
const spyBD = jasmine.createSpy()
const spyCD = jasmine.createSpy()

function assertCount (calls) {
expect([
spyA.calls.count(),
spyAD.calls.count(),
spyB.calls.count(),
spyBD.calls.count(),
spyC.calls.count(),
spyCD.calls.count()
]).toEqual(calls)
}

const vm = new Vue({
template: `
<keep-alive max="2">
<component :is="n"></component>
</keep-alive>
`,
data: {
n: 'aa'
},
components: {
aa: {
template: '<div>a</div>',
created: spyA,
destroyed: spyAD
},
bb: {
template: '<div>bbb</div>',
created: spyB,
destroyed: spyBD
},
cc: {
template: '<div>ccc</div>',
created: spyC,
destroyed: spyCD
}
}
}).$mount()

assertCount([1, 0, 0, 0, 0, 0])
vm.n = 'bb'
waitForUpdate(() => {
assertCount([1, 0, 1, 0, 0, 0])
vm.n = 'cc'
}).then(() => {
// should prune A because max cache reached
assertCount([1, 1, 1, 0, 1, 0])
vm.n = 'bb'
}).then(() => {
// B should be reused, and made latest
assertCount([1, 1, 1, 0, 1, 0])
vm.n = 'aa'
}).then(() => {
// C should be pruned because B was used last so C is the oldest cached
assertCount([2, 1, 1, 0, 1, 1])
}).then(done)
})

it('should warn unknown component inside', () => {
new Vue({
template: `<keep-alive><foo/></keep-alive>`
}).$mount()
expect(`Unknown custom element: <foo>`).toHaveBeenWarned()
})

if (!isIE9) {
it('with transition-mode out-in', done => {
let next
Expand Down Expand Up @@ -946,71 +1019,5 @@ describe('Component keep-alive', () => {
}).then(done)
}
})

it('max', done => {
const spyA = jasmine.createSpy()
const spyB = jasmine.createSpy()
const spyC = jasmine.createSpy()
const spyAD = jasmine.createSpy()
const spyBD = jasmine.createSpy()
const spyCD = jasmine.createSpy()

function assertCount (calls) {
expect([
spyA.calls.count(),
spyAD.calls.count(),
spyB.calls.count(),
spyBD.calls.count(),
spyC.calls.count(),
spyCD.calls.count()
]).toEqual(calls)
}

const vm = new Vue({
template: `
<keep-alive max="2">
<component :is="n"></component>
</keep-alive>
`,
data: {
n: 'aa'
},
components: {
aa: {
template: '<div>a</div>',
created: spyA,
destroyed: spyAD
},
bb: {
template: '<div>bbb</div>',
created: spyB,
destroyed: spyBD
},
cc: {
template: '<div>ccc</div>',
created: spyC,
destroyed: spyCD
}
}
}).$mount()

assertCount([1, 0, 0, 0, 0, 0])
vm.n = 'bb'
waitForUpdate(() => {
assertCount([1, 0, 1, 0, 0, 0])
vm.n = 'cc'
}).then(() => {
// should prune A because max cache reached
assertCount([1, 1, 1, 0, 1, 0])
vm.n = 'bb'
}).then(() => {
// B should be reused, and made latest
assertCount([1, 1, 1, 0, 1, 0])
vm.n = 'aa'
}).then(() => {
// C should be pruned because B was used last so C is the oldest cached
assertCount([2, 1, 1, 0, 1, 1])
}).then(done)
})
}
})

0 comments on commit 6d6b373

Please sign in to comment.