| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406 |
- 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="scope">{{ scope.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)
- })
- // #9422
- // the behavior of the new syntax is slightly different.
- it('scoped slot v-if using slot-scope value', () => {
- const Child = {
- template: '<div><slot value="foo"/></div>'
- }
- const vm = new Vue({
- components: { Child },
- template: `
- <child>
- <template slot-scope="{ value }" v-if="value">
- foo {{ value }}
- </template>
- </child>
- `
- }).$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 ${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('named slots', () => {
- const vm = new Vue({
- template: `
- <foo>
- <template ${toNamed(syntax, 'default')}="foo">
- {{ foo }}
- </template>
- <template ${toNamed(syntax, 'one')}="one">
- {{ one }}
- </template>
- <template ${toNamed(syntax, 'two')}="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 ${toNamed(syntax, 'one')}="one">
- <bar ${syntax}="bar">
- {{ one }} {{ bar }}
- </bar>
- </template>
- <template ${toNamed(syntax, 'two')}="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 v-slot usage on non-component elements', () => {
- new Vue({
- template: `<div ${syntax}="foo"/>`
- }).$mount()
- expect(
- `v-slot can only be used on components or <template>`
- ).toHaveBeenWarned()
- })
- it('should warn mixed usage', () => {
- new Vue({
- template: `<foo><bar slot="one" slot-scope="bar" ${syntax}="bar"></bar></foo>`,
- components: { Foo, Bar }
- }).$mount()
- expect(
- `Unexpected mixed usage of different slot syntaxes`
- ).toHaveBeenWarned()
- })
- it('should warn invalid parameter expression', () => {
- new Vue({
- template: `<foo ${syntax}="1"></foo>`,
- components: { Foo }
- }).$mount()
- expect('invalid function parameter expression').toHaveBeenWarned()
- })
- it('should allow destructuring props with default value', () => {
- new Vue({
- template: `<foo ${syntax}="{ foo = { bar: '1' } }"></foo>`,
- 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>
- <template #default="foo">
- {{ foo }}
- </template>
- <template #one="one">
- {{ one }}
- </template>
- <template #two="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('should warn mixed root-default and named slots', () => {
- new Vue({
- template: `
- <foo #default="foo">
- {{ foo }}
- <template #one="one">
- {{ one }}
- </template>
- </foo>
- `,
- components: { Foo }
- }).$mount()
- expect(`default slot should also use <template>`).toHaveBeenWarned()
- })
- it('shorthand without scope variable', () => {
- const vm = new Vue({
- template: `
- <foo>
- <template #one>one</template>
- <template #two>two</template>
- </foo>
- `,
- components: { Foo }
- }).$mount()
- expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`onetwo`)
- })
- it('shorthand named slots on root', () => {
- const vm = new Vue({
- template: `
- <foo #one="one">
- {{ one }}
- </foo>
- `,
- 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: `
- <foo>
- <template #[a]="one">a {{ one }} </template>
- <template v-slot:[b]="two">b {{ two }} </template>
- </foo>
- `,
- 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: `
- <foo>
- <template v-if="flag" v-slot:one="one">a {{ one }} </template>
- <template v-else v-slot:two="two">b {{ two }} </template>
- </foo>
- `,
- 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 <template>', () => {
- // @ts-ignore unused
- const vm = new Vue({
- template: `
- <foo>
- <template v-if="true">
- <template v-slot:one>foo</template>
- </template>
- </foo>
- `,
- components: { Foo }
- }).$mount()
- expect(
- `<template v-slot> 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: `
- <div>{{ parentCount }}<foo #default>{{ childCount }}</foo></div>
- `,
- data: {
- parentCount: 0,
- childCount: 0
- },
- updated: parentUpdate,
- components: {
- foo: {
- template: `<div><slot/></div>`,
- updated: childUpdate
- }
- }
- }).$mount()
- expect(vm.$el.innerHTML).toMatch(`0<div>0</div>`)
- vm.parentCount++
- waitForUpdate(() => {
- expect(vm.$el.innerHTML).toMatch(`1<div>0</div>`)
- // 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(`1<div>1</div>`)
- // 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: `
- <foo>
- <template #default>
- <bar />
- </template>
- </foo>
- `,
- components: {
- foo: {
- template: `<div>foo<slot/></div>`
- },
- bar: resolve => {
- setTimeout(() => {
- resolve({
- template: `<div>bar</div>`
- })
- 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: `<div></div>`
- }
- const parent = new Vue({
- template: `<div>{{ count }}<child/></div>`,
- 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: `<div><slot/></div>`
- }
- const Inner = {
- props: ['foo'],
- template: `<div>{{ foo }}</div>`
- }
- const Outer = {
- data: () => ({ foo: 1 }),
- template: `<div><slot :foo="foo" /></div>`
- }
- const vm = new Vue({
- components: { Outer, Wrapper, Inner },
- template: `
- <outer ref="outer" v-slot="props">
- <wrapper v-slot>
- <inner :foo="props.foo"/>
- </wrapper>
- </outer>
- `
- }).$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 <slot>', done => {
- const Foo = {
- data() {
- return {
- key: 'msg'
- }
- },
- template: `<div><slot :[key]="'hello'"/></div>`
- }
- const vm = new Vue({
- components: { Foo },
- template: `
- <foo ref="foo" v-slot="props">{{ props }}</foo>
- `
- }).$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: `<div><slot>fallback</slot></div>`
- }
- const wrapper = {
- template: `
- <inner>
- <template #default>
- <slot/>
- </template>
- </inner>
- `,
- components: { inner }
- }
- const vm = new Vue({
- components: { wrapper, inner },
- template: `<wrapper/>`
- }).$mount()
- expect(vm.$el.textContent).toBe(`fallback`)
- })
- it('should expose v-slot without scope on this.$slots', () => {
- const vm = new Vue({
- template: `<foo><template v-slot>hello</template></foo>`,
- 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: `<foo><template slot-scope="foo">hello</template></foo>`,
- 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: `<foo><template v-slot>hello</template></foo>`,
- 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: `<div><slot name="foo" /> <slot name="bar" /></div>`
- }
- const vm = new Vue({
- data: {
- msg: 'foo'
- },
- template: `
- <foo>
- <div slot="foo">{{ msg }}</div>
- <template #bar><div>bar</div></template>
- </foo>
- `,
- 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 v-slot="foo, bar">{{ foo }} {{ bar }}</foo>`,
- components: { foo }
- }).$mount()
- expect(vm.$el.textContent).toBe('foo bar')
- })
- it('should not skip updates when a scoped slot contains parent <slot/> content', done => {
- const inner = {
- template: `<div><slot/></div>`
- }
- const wrapper = {
- template: `<inner v-slot><slot/></inner>`,
- components: { inner }
- }
- const vm = new Vue({
- data() {
- return {
- ok: true
- }
- },
- components: { wrapper },
- template: `<wrapper><div>{{ ok ? 'foo' : 'bar' }}</div></wrapper>`
- }).$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: `<div><slot></slot></div>`
- }
- const vm = new Vue({
- template: `
- <div>
- <div v-for="i in numbers">
- <test v-slot>{{ i }}</test>
- </div>
- </div>
- `,
- 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: `<div><slot :n="1" /></div>`
- }
- const vm = new Vue({
- components: { Foo },
- data: {
- ok: true
- },
- template: `
- <div>
- <div v-if="ok">
- <foo v-slot="{ n }">{{ n }}</foo>
- </div>
- <div v-if="!ok">
- <foo v-slot="{ n }">{{ n + 1 }}</foo>
- </div>
- </div>
- `
- }).$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: `<div>
- <p>Default:<slot/></p>
- <p>Content:<slot name='content'/></p>
- </div>`
- }
- const vm = new Vue({
- data: { ok: false },
- components: { Wrapper },
- template: `<wrapper>
- <p v-if='ok'>ok</p>
- <template #content>
- <p v-if='ok'>ok</p>
- </template>
- </wrapper>`
- }).$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: `<test v-slot><template v-if="false">hi</template></test>`,
- components: {
- Test: {
- template: `<div><slot>fallback</slot></div>`
- }
- }
- }).$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: `<div><slot name="footer"/></div>`
- }
- 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: `<div><slot/></div>`
- }
- const parent = new Vue({
- template: `<div>
- <child v-if="flag"><template #default>foo</template></child>
- <child v-else></child>
- </div>`,
- data: {
- flag: true
- },
- components: { Child }
- }).$mount()
- expect(parent.$el.textContent).toMatch(`foo`)
- parent.flag = false
- waitForUpdate(() => {
- expect(parent.$el.textContent).toMatch(``)
- }).then(done)
- })
- })
|