test programmatic $slot access

This commit is contained in:
Evan You 2016-05-28 21:39:59 -04:00
parent 7bb5234074
commit 43da334ec1
2 changed files with 50 additions and 0 deletions

View File

@ -63,6 +63,12 @@ export function renderMixin (Vue: Class<Component>) {
let vnode = render.call(vm._renderProxy)
// return empty vnode in case the render function errored out
if (!(vnode instanceof VNode)) {
if (process.env.NODE_ENV !== 'production' && Array.isArray(vnode)) {
warn(
'Multiple root nodes returned from render function. Render function ' +
'should return a single root node.'
)
}
vnode = emptyVNode
}
// set parent

View File

@ -268,4 +268,48 @@ describe('Component slot', () => {
)
Vue.config.preserveWhitespace = false
})
it('programmatic access to $slots', () => {
const vm = new Vue({
template: '<test><p slot="a">A</p><div>C</div><p slot="b">B</div></p></test>',
components: {
test: {
render () {
expect(this.$slots.a.length).toBe(1)
expect(this.$slots.a[0].tag).toBe('p')
expect(this.$slots.a[0].children.length).toBe(1)
expect(this.$slots.a[0].children[0].text).toBe('A')
expect(this.$slots.b.length).toBe(1)
expect(this.$slots.b[0].tag).toBe('p')
expect(this.$slots.b[0].children.length).toBe(1)
expect(this.$slots.b[0].children[0].text).toBe('B')
expect(this.$slots.default.length).toBe(1)
expect(this.$slots.default[0].tag).toBe('div')
expect(this.$slots.default[0].children.length).toBe(1)
expect(this.$slots.default[0].children[0].text).toBe('C')
return this.$slots.default[0]
}
}
}
}).$mount()
expect(vm.$el.tagName).toBe('DIV')
expect(vm.$el.textContent).toBe('C')
})
it('warn if user directly returns array', () => {
new Vue({
template: '<test><div></div></test>',
components: {
test: {
render () {
return this.$slots.default
}
}
}
}).$mount()
expect('Render function should return a single root node').toHaveBeenWarned()
})
})