| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735 |
- import Vue from 'vue'
- describe('Component scoped slot', () => {
- it('default slot', done => {
- const vm = new Vue({
- template: `
- <test ref="test">
- <template slot-scope="props">
- <span>{{ props.msg }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return { msg: 'hello' }
- },
- template: `
- <div>
- <slot :msg="msg"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello</span>')
- vm.$refs.test.msg = 'world'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>world</span>')
- }).then(done)
- })
- it('default slot (plain element)', done => {
- const vm = new Vue({
- template: `
- <test ref="test">
- <span slot-scope="props">{{ props.msg }}</span>
- </test>
- `,
- components: {
- test: {
- data () {
- return { msg: 'hello' }
- },
- template: `
- <div>
- <slot :msg="msg"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello</span>')
- vm.$refs.test.msg = 'world'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>world</span>')
- }).then(done)
- })
- it('with v-bind', done => {
- const vm = new Vue({
- template: `
- <test ref="test">
- <template slot-scope="props">
- <span>{{ props.msg }} {{ props.msg2 }} {{ props.msg3 }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return {
- msg: 'hello',
- obj: { msg2: 'world', msg3: '.' }
- }
- },
- template: `
- <div>
- <slot :msg="msg" v-bind="obj" msg3="!"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello world !</span>')
- vm.$refs.test.msg = 'bye'
- vm.$refs.test.obj.msg2 = 'bye'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>bye bye !</span>')
- }).then(done)
- })
- it('should warn when using v-bind with no object', () => {
- new Vue({
- template: `
- <test ref="test">
- <template scope="props">
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return {
- text: 'some text'
- }
- },
- template: `
- <div>
- <slot v-bind="text"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect('slot v-bind without argument expects an Object').toHaveBeenWarned()
- })
- it('should not warn when using v-bind with object', () => {
- new Vue({
- template: `
- <test ref="test">
- <template scope="props">
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return {
- foo: {
- text: 'some text'
- }
- }
- },
- template: `
- <div>
- <slot v-bind="foo"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect('slot v-bind without argument expects an Object').not.toHaveBeenWarned()
- })
- it('named scoped slot', done => {
- const vm = new Vue({
- template: `
- <test ref="test">
- <template slot="item" slot-scope="props">
- <span>{{ props.foo }}</span><span>{{ props.bar }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return { foo: 'FOO', bar: 'BAR' }
- },
- template: `
- <div>
- <slot name="item" :foo="foo" :bar="bar"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>FOO</span><span>BAR</span>')
- vm.$refs.test.foo = 'BAZ'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>BAZ</span><span>BAR</span>')
- }).then(done)
- })
- it('named scoped slot (plain element)', done => {
- const vm = new Vue({
- template: `
- <test ref="test">
- <span slot="item" slot-scope="props">{{ props.foo }} {{ props.bar }}</span>
- </test>
- `,
- components: {
- test: {
- data () {
- return { foo: 'FOO', bar: 'BAR' }
- },
- template: `
- <div>
- <slot name="item" :foo="foo" :bar="bar"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>FOO BAR</span>')
- vm.$refs.test.foo = 'BAZ'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>BAZ BAR</span>')
- }).then(done)
- })
- it('fallback content', () => {
- const vm = new Vue({
- template: `<test></test>`,
- components: {
- test: {
- data () {
- return { msg: 'hello' }
- },
- template: `
- <div>
- <slot name="item" :text="msg">
- <span>{{ msg }} fallback</span>
- </slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello fallback</span>')
- })
- it('slot with v-for', done => {
- const vm = new Vue({
- template: `
- <test ref="test">
- <template slot="item" slot-scope="props">
- <span>{{ props.text }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return {
- items: ['foo', 'bar', 'baz']
- }
- },
- template: `
- <div>
- <slot v-for="item in items" name="item" :text="item"></slot>
- </div>
- `
- }
- }
- }).$mount()
- function assertOutput () {
- expect(vm.$el.innerHTML).toBe(vm.$refs.test.items.map(item => {
- return `<span>${item}</span>`
- }).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: `
- <test ref="test">
- <template slot="item" slot-scope="props">
- <span>{{ props.text }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return {
- items: ['foo', 'bar', 'baz']
- }
- },
- template: `
- <ul>
- <li v-for="item in items">
- <slot name="item" :text="item"></slot>
- </li>
- </ul>
- `
- }
- }
- }).$mount()
- function assertOutput () {
- expect(vm.$el.innerHTML).toBe(vm.$refs.test.items.map(item => {
- return `<li><span>${item}</span></li>`
- }).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: `
- <test ref="test">
- <span slot="item">I am static</span>
- </test>
- `,
- components: {
- test: {
- data () {
- return { msg: 'hello' }
- },
- template: `
- <div>
- <slot name="item" :text="msg"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>I am static</span>')
- })
- it('non-scoped slot with scope alias', () => {
- const vm = new Vue({
- template: `
- <test ref="test">
- <template slot="item" slot-scope="props">
- <span>{{ props.text || 'meh' }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return { msg: 'hello' }
- },
- template: `
- <div>
- <slot name="item"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>meh</span>')
- })
- it('warn key on slot', () => {
- new Vue({
- template: `
- <test ref="test">
- <template slot="item" slot-scope="props">
- <span>{{ props.text }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return {
- items: ['foo', 'bar', 'baz']
- }
- },
- template: `
- <div>
- <slot v-for="item in items" name="item" :text="item" :key="item"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(`\`key\` does not work on <slot>`).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('<span>hello</span>')
- vm.$refs.test.msg = 'world'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>world</span>')
- }).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('<span>hello</span>')
- })
- 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('<span>hello</span>')
- })
- // new in 2.6, unifying all slots as functions
- it('non-scoped slots should also be available on $scopedSlots', () => {
- const vm = new Vue({
- template: `<foo>before <div slot="bar" slot-scope="{ msg }">{{ msg }}</div> after</foo>`,
- components: {
- foo: {
- render(h) {
- return h('div', [
- this.$scopedSlots.default(),
- this.$scopedSlots.bar({ msg: 'hi' })
- ])
- }
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe(`before after<div>hi</div>`)
- })
- // #4779
- it('should support dynamic slot target', done => {
- const Child = {
- template: `
- <div>
- <slot name="a" msg="a" />
- <slot name="b" msg="b" />
- </div>
- `
- }
- const vm = new Vue({
- data: {
- a: 'a',
- b: 'b'
- },
- template: `
- <child>
- <template :slot="a" slot-scope="props">A {{ props.msg }}</template>
- <template :slot="b" slot-scope="props">B {{ props.msg }}</template>
- </child>
- `,
- 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 <test>{
- props => <span>{props.msg}</span>
- }</test>
- },
- components: {
- test: {
- data () {
- return { msg: 'hello' }
- },
- render (h) {
- return <div>
- {this.$scopedSlots.default({ msg: this.msg })}
- </div>
- }
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello</span>')
- })
- // #5615
- it('scoped slot with v-for', done => {
- const vm = new Vue({
- data: { names: ['foo', 'bar'] },
- template: `
- <test ref="test">
- <template v-for="n in names" :slot="n" slot-scope="props">
- <span>{{ props.msg }}</span>
- </template>
- <template slot="abc" slot-scope="props">
- <span>{{ props.msg }}</span>
- </template>
- </test>
- `,
- components: {
- test: {
- data: () => ({ msg: 'hello' }),
- template: `
- <div>
- <slot name="foo" :msg="msg + ' foo'"></slot>
- <slot name="bar" :msg="msg + ' bar'"></slot>
- <slot name="abc" :msg="msg + ' abc'"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>')
- vm.$refs.test.msg = 'world'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>world foo</span> <span>world bar</span> <span>world abc</span>')
- }).then(done)
- })
- it('scoped slot with v-for (plain elements)', done => {
- const vm = new Vue({
- data: { names: ['foo', 'bar'] },
- template: `
- <test ref="test">
- <span v-for="n in names" :slot="n" slot-scope="props">{{ props.msg }}</span>
- <span slot="abc" slot-scope="props">{{ props.msg }}</span>
- </test>
- `,
- components: {
- test: {
- data: () => ({ msg: 'hello' }),
- template: `
- <div>
- <slot name="foo" :msg="msg + ' foo'"></slot>
- <slot name="bar" :msg="msg + ' bar'"></slot>
- <slot name="abc" :msg="msg + ' abc'"></slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>')
- vm.$refs.test.msg = 'world'
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<span>world foo</span> <span>world bar</span> <span>world abc</span>')
- }).then(done)
- })
- // #6725
- it('scoped slot with v-if', done => {
- const vm = new Vue({
- data: {
- ok: false
- },
- template: `
- <test>
- <template v-if="ok" slot-scope="foo">
- <p>{{ foo.text }}</p>
- </template>
- </test>
- `,
- components: {
- test: {
- data () {
- return { msg: 'hello' }
- },
- template: `
- <div>
- <slot :text="msg">
- <span>{{ msg }} fallback</span>
- </slot>
- </div>
- `
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toBe('<span>hello fallback</span>')
- vm.ok = true
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toBe('<p>hello</p>')
- }).then(done)
- })
- // new in 2.6
- describe('slot-props 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')[0]
- }
- }
- const Baz = {
- render(h) {
- return this.$scopedSlots.default && this.$scopedSlots.default('from baz')[0]
- }
- }
- function runSuite(syntax) {
- it('default slot', () => {
- const vm = new Vue({
- template: `<foo ${syntax}="foo">{{ foo }}<div>{{ foo }}</div></foo>`,
- components: { Foo }
- }).$mount()
- expect(vm.$el.innerHTML).toBe(`from foo default<div>from foo default</div>`)
- })
- it('nested default slots', () => {
- const vm = new Vue({
- template: `
- <foo ${syntax}="foo">
- <bar ${syntax}="bar">
- <baz ${syntax}="baz">
- {{ foo }} | {{ bar }} | {{ baz }}
- </baz>
- </bar>
- </foo>
- `,
- components: { Foo, Bar, Baz }
- }).$mount()
- expect(vm.$el.innerHTML.trim()).toBe(`from foo default | from bar | from baz`)
- })
- it('default + named slots', () => {
- const vm = new Vue({
- template: `
- <foo ()="foo">
- {{ foo }}
- <template slot="one" ${syntax}="one">
- {{ one }}
- </template>
- <template slot="two" ${syntax}="two">
- {{ two }}
- </template>
- </foo>
- `,
- 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: `
- <foo>
- <template slot="one" ${syntax}="one">
- <bar ${syntax}="bar">
- {{ one }} {{ bar }}
- </bar>
- </template>
- <template slot="two" ${syntax}="two">
- <baz ${syntax}="baz">
- {{ two }} {{ baz }}
- </baz>
- </template>
- </foo>
- `,
- 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 slot-props usage on non-component elements', () => {
- const vm = new Vue({
- template: `<div ${syntax}="foo"/>`
- }).$mount()
- expect(`slot-props cannot be used on non-component elements`).toHaveBeenWarned()
- })
- }
- // run tests for both full syntax and shorthand
- runSuite('slot-props')
- runSuite('()')
- })
- })
|