import Vue from 'vue'
describe('Component scoped slot', () => {
it('default slot', done => {
const vm = new Vue({
template: `
{{ props.msg }}
`,
components: {
test: {
data() {
return { msg: 'hello' }
},
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello')
vm.$refs.test.msg = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('world')
}).then(done)
})
it('default slot (plain element)', done => {
const vm = new Vue({
template: `
{{ props.msg }}
`,
components: {
test: {
data() {
return { msg: 'hello' }
},
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello')
vm.$refs.test.msg = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('world')
}).then(done)
})
it('with v-bind', done => {
const vm = new Vue({
template: `
{{ props.msg }} {{ props.msg2 }} {{ props.msg3 }}
`,
components: {
test: {
data() {
return {
msg: 'hello',
obj: { msg2: 'world', msg3: '.' }
}
},
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello world !')
vm.$refs.test.msg = 'bye'
vm.$refs.test.obj.msg2 = 'bye'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('bye bye !')
}).then(done)
})
it('should warn when using v-bind with no object', () => {
new Vue({
template: `
`,
components: {
test: {
data() {
return {
text: 'some text'
}
},
template: `
`
}
}
}).$mount()
expect('slot v-bind without argument expects an Object').toHaveBeenWarned()
})
it('should not warn when using v-bind with object', () => {
new Vue({
template: `
`,
components: {
test: {
data() {
return {
foo: {
text: 'some text'
}
}
},
template: `
`
}
}
}).$mount()
expect(
'slot v-bind without argument expects an Object'
).not.toHaveBeenWarned()
})
it('named scoped slot', done => {
const vm = new Vue({
template: `
{{ props.foo }}{{ props.bar }}
`,
components: {
test: {
data() {
return { foo: 'FOO', bar: 'BAR' }
},
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('FOOBAR')
vm.$refs.test.foo = 'BAZ'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('BAZBAR')
}).then(done)
})
it('named scoped slot (plain element)', done => {
const vm = new Vue({
template: `
{{ props.foo }} {{ props.bar }}
`,
components: {
test: {
data() {
return { foo: 'FOO', bar: 'BAR' }
},
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('FOO BAR')
vm.$refs.test.foo = 'BAZ'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('BAZ BAR')
}).then(done)
})
it('fallback content', () => {
const vm = new Vue({
template: ``,
components: {
test: {
data() {
return { msg: 'hello' }
},
template: `
{{ msg }} fallback
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello fallback')
})
it('slot with v-for', done => {
const vm = new Vue({
template: `
{{ props.text }}
`,
components: {
test: {
data() {
return {
items: ['foo', 'bar', 'baz']
}
},
template: `
`
}
}
}).$mount()
function assertOutput() {
expect(vm.$el.innerHTML).toBe(
vm.$refs.test.items
.map(item => {
return `${item}`
})
.join('')
)
}
assertOutput()
vm.$refs.test.items.reverse()
waitForUpdate(assertOutput)
.then(() => {
vm.$refs.test.items.push('qux')
})
.then(assertOutput)
.then(done)
})
it('slot inside v-for', done => {
const vm = new Vue({
template: `
{{ props.text }}
`,
components: {
test: {
data() {
return {
items: ['foo', 'bar', 'baz']
}
},
template: `
`
}
}
}).$mount()
function assertOutput() {
expect(vm.$el.innerHTML).toBe(
vm.$refs.test.items
.map(item => {
return `${item}`
})
.join('')
)
}
assertOutput()
vm.$refs.test.items.reverse()
waitForUpdate(assertOutput)
.then(() => {
vm.$refs.test.items.push('qux')
})
.then(assertOutput)
.then(done)
})
it('scoped slot without scope alias', () => {
const vm = new Vue({
template: `
I am static
`,
components: {
test: {
data() {
return { msg: 'hello' }
},
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('I am static')
})
it('non-scoped slot with scope alias', () => {
const vm = new Vue({
template: `
{{ props.text || 'meh' }}
`,
components: {
test: {
data() {
return { msg: 'hello' }
},
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('meh')
})
it('warn key on slot', () => {
new Vue({
template: `
{{ props.text }}
`,
components: {
test: {
data() {
return {
items: ['foo', 'bar', 'baz']
}
},
template: `
`
}
}
}).$mount()
expect(`\`key\` does not work on `).toHaveBeenWarned()
})
it('render function usage (named, via data)', done => {
const vm = new Vue({
render(h) {
return h('test', {
ref: 'test',
scopedSlots: {
item: props => h('span', props.text)
}
})
},
components: {
test: {
data() {
return { msg: 'hello' }
},
render(h) {
return h(
'div',
this.$scopedSlots.item({
text: this.msg
})
)
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello')
vm.$refs.test.msg = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('world')
}).then(done)
})
it('render function usage (default, as children)', () => {
const vm = new Vue({
render(h) {
return h('test', [props => h('span', [props.msg])])
},
components: {
test: {
data() {
return { msg: 'hello' }
},
render(h) {
return h('div', this.$scopedSlots.default({ msg: this.msg }))
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello')
})
it('render function usage (default, as root)', () => {
const vm = new Vue({
render(h) {
return h('test', [props => h('span', [props.msg])])
},
components: {
test: {
data() {
return { msg: 'hello' }
},
render(h) {
const res = this.$scopedSlots.default({ msg: this.msg })
// all scoped slots should be normalized into arrays
expect(Array.isArray(res)).toBe(true)
return res
}
}
}
}).$mount()
expect(vm.$el.outerHTML).toBe('hello')
})
// new in 2.6, unifying all slots as functions
it('non-scoped slots should also be available on $scopedSlots', () => {
const vm = new Vue({
template: `before {{ scope.msg }}
after`,
components: {
foo: {
render(h) {
return h('div', [
this.$scopedSlots.default(),
this.$scopedSlots.bar({ msg: 'hi' })
])
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe(`before afterhi
`)
})
// #4779
it('should support dynamic slot target', done => {
const Child = {
template: `
`
}
const vm = new Vue({
data: {
a: 'a',
b: 'b'
},
template: `
A {{ props.msg }}
B {{ props.msg }}
`,
components: { Child }
}).$mount()
expect(vm.$el.textContent.trim()).toBe('A a B b')
// switch slots
vm.a = 'b'
vm.b = 'a'
waitForUpdate(() => {
expect(vm.$el.textContent.trim()).toBe('B a A b')
}).then(done)
})
// it('render function usage (JSX)', () => {
// const vm = new Vue({
// render (h) {
// return ({
// props => {props.msg}
// })
// },
// components: {
// test: {
// data () {
// return { msg: 'hello' }
// },
// render (h) {
// return
// {this.$scopedSlots.default({ msg: this.msg })}
//
// }
// }
// }
// }).$mount()
// expect(vm.$el.innerHTML).toBe('hello')
// })
// #5615
it('scoped slot with v-for', done => {
const vm = new Vue({
data: { names: ['foo', 'bar'] },
template: `
{{ props.msg }}
{{ props.msg }}
`,
components: {
test: {
data: () => ({ msg: 'hello' }),
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe(
'hello foo hello bar hello abc'
)
vm.$refs.test.msg = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe(
'world foo world bar world abc'
)
}).then(done)
})
it('scoped slot with v-for (plain elements)', done => {
const vm = new Vue({
data: { names: ['foo', 'bar'] },
template: `
{{ props.msg }}
{{ props.msg }}
`,
components: {
test: {
data: () => ({ msg: 'hello' }),
template: `
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe(
'hello foo hello bar hello abc'
)
vm.$refs.test.msg = 'world'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe(
'world foo world bar world abc'
)
}).then(done)
})
// #6725
it('scoped slot with v-if', done => {
const vm = new Vue({
data: {
ok: false
},
template: `
{{ foo.text }}
`,
components: {
test: {
data() {
return { msg: 'hello' }
},
template: `
{{ msg }} fallback
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello fallback')
vm.ok = true
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('hello
')
}).then(done)
})
// #9422
// the behavior of the new syntax is slightly different.
it('scoped slot v-if using slot-scope value', () => {
const Child = {
template: '
'
}
const vm = new Vue({
components: { Child },
template: `
foo {{ value }}
`
}).$mount()
expect(vm.$el.textContent).toMatch(`foo foo`)
})
// 2.6 new slot syntax
describe('v-slot syntax', () => {
const Foo = {
render(h) {
return h('div', [
this.$scopedSlots.default &&
this.$scopedSlots.default('from foo default'),
this.$scopedSlots.one && this.$scopedSlots.one('from foo one'),
this.$scopedSlots.two && this.$scopedSlots.two('from foo two')
])
}
}
const Bar = {
render(h) {
return (
this.$scopedSlots.default && this.$scopedSlots.default('from bar')
)
}
}
const Baz = {
render(h) {
return (
this.$scopedSlots.default && this.$scopedSlots.default('from baz')
)
}
}
const toNamed = (syntax, name) =>
syntax[0] === '#'
? `#${name}` // shorthand
: `${syntax}:${name}` // full syntax
function runSuite(syntax) {
it('default slot', () => {
const vm = new Vue({
template: `{{ foo }}{{ foo }}
`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML).toBe(
`from foo defaultfrom foo default
`
)
})
it('nested default slots', () => {
const vm = new Vue({
template: `
{{ foo }} | {{ bar }} | {{ baz }}
`,
components: { Foo, Bar, Baz }
}).$mount()
expect(vm.$el.innerHTML.trim()).toBe(
`from foo default | from bar | from baz`
)
})
it('named slots', () => {
const vm = new Vue({
template: `
{{ foo }}
{{ one }}
{{ two }}
`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
`from foo default from foo one from foo two`
)
})
it('nested + named + default slots', () => {
const vm = new Vue({
template: `
{{ one }} {{ bar }}
{{ two }} {{ baz }}
`,
components: { Foo, Bar, Baz }
}).$mount()
expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
`from foo one from bar from foo two from baz`
)
})
it('should warn v-slot usage on non-component elements', () => {
new Vue({
template: ``
}).$mount()
expect(
`v-slot can only be used on components or `
).toHaveBeenWarned()
})
it('should warn mixed usage', () => {
new Vue({
template: ``,
components: { Foo, Bar }
}).$mount()
expect(
`Unexpected mixed usage of different slot syntaxes`
).toHaveBeenWarned()
})
it('should warn invalid parameter expression', () => {
new Vue({
template: ``,
components: { Foo }
}).$mount()
expect('invalid function parameter expression').toHaveBeenWarned()
})
it('should allow destructuring props with default value', () => {
new Vue({
template: ``,
components: { Foo }
}).$mount()
expect('invalid function parameter expression').not.toHaveBeenWarned()
})
}
// run tests for both full syntax and shorthand
runSuite('v-slot')
runSuite('#default')
it('shorthand named slots', () => {
const vm = new Vue({
template: `
{{ foo }}
{{ one }}
{{ two }}
`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
`from foo default from foo one from foo two`
)
})
it('should warn mixed root-default and named slots', () => {
new Vue({
template: `
{{ foo }}
{{ one }}
`,
components: { Foo }
}).$mount()
expect(`default slot should also use `).toHaveBeenWarned()
})
it('shorthand without scope variable', () => {
const vm = new Vue({
template: `
one
two
`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`onetwo`)
})
it('shorthand named slots on root', () => {
const vm = new Vue({
template: `
{{ one }}
`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo one`)
})
it('dynamic slot name', done => {
const vm = new Vue({
data: {
a: 'one',
b: 'two'
},
template: `
a {{ one }}
b {{ two }}
`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
`a from foo one b from foo two`
)
vm.a = 'two'
vm.b = 'one'
waitForUpdate(() => {
expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
`b from foo one a from foo two `
)
}).then(done)
})
it('should work with v-if/v-else', done => {
const vm = new Vue({
data: { flag: true },
template: `
a {{ one }}
b {{ two }}
`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML).toBe(`a from foo one `)
vm.flag = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe(`b from foo two `)
}).then(done)
})
it('warn when v-slot used on non-root ', () => {
// @ts-ignore unused
const vm = new Vue({
template: `
foo
`,
components: { Foo }
}).$mount()
expect(
` can only appear at the root level`
).toHaveBeenWarned()
})
})
// 2.6 scoped slot perf optimization
it('should have accurate tracking for scoped slots', done => {
const parentUpdate = vi.fn()
const childUpdate = vi.fn()
const vm = new Vue({
template: `
{{ parentCount }}{{ childCount }}
`,
data: {
parentCount: 0,
childCount: 0
},
updated: parentUpdate,
components: {
foo: {
template: `
`,
updated: childUpdate
}
}
}).$mount()
expect(vm.$el.innerHTML).toMatch(`00
`)
vm.parentCount++
waitForUpdate(() => {
expect(vm.$el.innerHTML).toMatch(`10
`)
// should only trigger parent update
expect(parentUpdate.mock.calls.length).toBe(1)
expect(childUpdate.mock.calls.length).toBe(0)
vm.childCount++
})
.then(() => {
expect(vm.$el.innerHTML).toMatch(`11
`)
// should only trigger child update
expect(parentUpdate.mock.calls.length).toBe(1)
expect(childUpdate.mock.calls.length).toBe(1)
})
.then(done)
})
// #9432: async components inside a scoped slot should trigger update of the
// component that invoked the scoped slot, not the lexical context component.
it('async component inside scoped slot', done => {
const vm = new Vue({
template: `
`,
components: {
foo: {
template: `foo
`
},
bar: resolve => {
setTimeout(() => {
resolve({
template: `bar
`
})
next()
}, 0)
}
}
}).$mount()
function next() {
waitForUpdate(() => {
expect(vm.$el.textContent).toBe(`foobar`)
}).then(done)
}
})
// regression #9396
it('should not force update child with no slot content', done => {
const Child = {
updated: vi.fn(),
template: ``
}
const parent = new Vue({
template: `{{ count }}
`,
data: {
count: 0
},
components: { Child }
}).$mount()
expect(parent.$el.textContent).toBe(`0`)
parent.count++
waitForUpdate(() => {
expect(parent.$el.textContent).toBe(`1`)
expect(Child.updated).not.toHaveBeenCalled()
}).then(done)
})
// regression #9438
it('nested scoped slots update', done => {
const Wrapper = {
template: `
`
}
const Inner = {
props: ['foo'],
template: `{{ foo }}
`
}
const Outer = {
data: () => ({ foo: 1 }),
template: `
`
}
const vm = new Vue({
components: { Outer, Wrapper, Inner },
template: `
`
}).$mount()
expect(vm.$el.textContent).toBe(`1`)
vm.$refs.outer.foo++
waitForUpdate(() => {
expect(vm.$el.textContent).toBe(`2`)
}).then(done)
})
it('dynamic v-bind arguments on ', done => {
const Foo = {
data() {
return {
key: 'msg'
}
},
template: `
`
}
const vm = new Vue({
components: { Foo },
template: `
{{ props }}
`
}).$mount()
expect(vm.$el.textContent).toBe(JSON.stringify({ msg: 'hello' }, null, 2))
vm.$refs.foo.key = 'changed'
waitForUpdate(() => {
expect(vm.$el.textContent).toBe(
JSON.stringify({ changed: 'hello' }, null, 2)
)
}).then(done)
})
// #9452
it('fallback for scoped slots passed multiple levels down', () => {
const inner = {
template: `fallback
`
}
const wrapper = {
template: `
`,
components: { inner }
}
const vm = new Vue({
components: { wrapper, inner },
template: ``
}).$mount()
expect(vm.$el.textContent).toBe(`fallback`)
})
it('should expose v-slot without scope on this.$slots', () => {
const vm = new Vue({
template: `hello`,
components: {
foo: {
render(h) {
return h('div', this.$slots.default)
}
}
}
}).$mount()
expect(vm.$el.textContent).toBe('hello')
})
it('should not expose legacy syntax scoped slot on this.$slots', () => {
const vm = new Vue({
template: `hello`,
components: {
foo: {
render(h) {
expect(this.$slots.default).toBeUndefined()
return h('div', this.$slots.default)
}
}
}
}).$mount()
expect(vm.$el.textContent).toBe('')
})
it('should expose v-slot without scope on ctx.slots() in functional', () => {
const vm = new Vue({
template: `hello`,
components: {
foo: {
functional: true,
render(h, ctx) {
return h('div', ctx.slots().default)
}
}
}
}).$mount()
expect(vm.$el.textContent).toBe('hello')
})
it('should not cache scoped slot normalization when there are a mix of normal and scoped slots', done => {
const foo = {
template: `
`
}
const vm = new Vue({
data: {
msg: 'foo'
},
template: `
{{ msg }}
bar
`,
components: { foo }
}).$mount()
expect(vm.$el.textContent).toBe(`foo bar`)
vm.msg = 'baz'
waitForUpdate(() => {
expect(vm.$el.textContent).toBe(`baz bar`)
}).then(done)
})
// #9468
it('should support passing multiple args to scoped slot function', () => {
const foo = {
render() {
return this.$scopedSlots.default('foo', 'bar')
}
}
const vm = new Vue({
template: `{{ foo }} {{ bar }}`,
components: { foo }
}).$mount()
expect(vm.$el.textContent).toBe('foo bar')
})
it('should not skip updates when a scoped slot contains parent content', done => {
const inner = {
template: `
`
}
const wrapper = {
template: ``,
components: { inner }
}
const vm = new Vue({
data() {
return {
ok: true
}
},
components: { wrapper },
template: `{{ ok ? 'foo' : 'bar' }}
`
}).$mount()
expect(vm.$el.textContent).toBe('foo')
vm.ok = false
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('bar')
}).then(done)
})
it('should not skip updates for v-slot inside v-for', done => {
const test = {
template: `
`
}
const vm = new Vue({
template: `
`,
components: { test },
data: {
numbers: [1]
}
}).$mount()
expect(vm.$el.textContent).toBe(`1`)
vm.numbers = [2]
waitForUpdate(() => {
expect(vm.$el.textContent).toBe(`2`)
}).then(done)
})
// #9534
it('should detect conditional reuse with different slot content', done => {
const Foo = {
template: `
`
}
const vm = new Vue({
components: { Foo },
data: {
ok: true
},
template: `
`
}).$mount()
expect(vm.$el.textContent.trim()).toBe(`1`)
vm.ok = false
waitForUpdate(() => {
expect(vm.$el.textContent.trim()).toBe(`2`)
}).then(done)
})
// #9644
it('should factor presence of normal slots into scoped slots caching', done => {
const Wrapper = {
template: ``
}
const vm = new Vue({
data: { ok: false },
components: { Wrapper },
template: `
ok
ok
`
}).$mount()
expect(vm.$el.textContent).not.toMatch(`Default:ok`)
expect(vm.$el.textContent).not.toMatch(`Content:ok`)
vm.ok = true
waitForUpdate(() => {
expect(vm.$el.textContent).toMatch(`Default:ok`)
expect(vm.$el.textContent).toMatch(`Content:ok`)
vm.ok = false
})
.then(() => {
expect(vm.$el.textContent).not.toMatch(`Default:ok`)
expect(vm.$el.textContent).not.toMatch(`Content:ok`)
vm.ok = true
})
.then(() => {
expect(vm.$el.textContent).toMatch(`Default:ok`)
expect(vm.$el.textContent).toMatch(`Content:ok`)
})
.then(done)
})
//#9658
it('fallback for scoped slot with single v-if', () => {
const vm = new Vue({
template: `hi`,
components: {
Test: {
template: `fallback
`
}
}
}).$mount()
expect(vm.$el.textContent).toMatch('fallback')
})
// #9699
// Component only has normal slots, but is passing down $scopedSlots directly
// $scopedSlots should not be marked as stable in this case
it('render function passing $scopedSlots w/ normal slots down', done => {
const one = {
template: `
`
}
const two = {
render(h) {
return h(one, {
scopedSlots: this.$scopedSlots
})
}
}
const vm = new Vue({
data: { count: 0 },
render(h) {
return h(two, [h('span', { slot: 'footer' }, this.count)])
}
}).$mount()
expect(vm.$el.textContent).toMatch(`0`)
vm.count++
waitForUpdate(() => {
expect(vm.$el.textContent).toMatch(`1`)
}).then(done)
})
// #11652
it('should update when switching between two components with slot and without slot', done => {
const Child = {
template: `
`
}
const parent = new Vue({
template: `
foo
`,
data: {
flag: true
},
components: { Child }
}).$mount()
expect(parent.$el.textContent).toMatch(`foo`)
parent.flag = false
waitForUpdate(() => {
expect(parent.$el.textContent).toMatch(``)
}).then(done)
})
})