component-scoped-slot.spec.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. import Vue from 'vue'
  2. describe('Component scoped slot', () => {
  3. it('default slot', done => {
  4. const vm = new Vue({
  5. template: `
  6. <test ref="test">
  7. <template slot-scope="props">
  8. <span>{{ props.msg }}</span>
  9. </template>
  10. </test>
  11. `,
  12. components: {
  13. test: {
  14. data () {
  15. return { msg: 'hello' }
  16. },
  17. template: `
  18. <div>
  19. <slot :msg="msg"></slot>
  20. </div>
  21. `
  22. }
  23. }
  24. }).$mount()
  25. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  26. vm.$refs.test.msg = 'world'
  27. waitForUpdate(() => {
  28. expect(vm.$el.innerHTML).toBe('<span>world</span>')
  29. }).then(done)
  30. })
  31. it('default slot (plain element)', done => {
  32. const vm = new Vue({
  33. template: `
  34. <test ref="test">
  35. <span slot-scope="props">{{ props.msg }}</span>
  36. </test>
  37. `,
  38. components: {
  39. test: {
  40. data () {
  41. return { msg: 'hello' }
  42. },
  43. template: `
  44. <div>
  45. <slot :msg="msg"></slot>
  46. </div>
  47. `
  48. }
  49. }
  50. }).$mount()
  51. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  52. vm.$refs.test.msg = 'world'
  53. waitForUpdate(() => {
  54. expect(vm.$el.innerHTML).toBe('<span>world</span>')
  55. }).then(done)
  56. })
  57. it('with v-bind', done => {
  58. const vm = new Vue({
  59. template: `
  60. <test ref="test">
  61. <template slot-scope="props">
  62. <span>{{ props.msg }} {{ props.msg2 }} {{ props.msg3 }}</span>
  63. </template>
  64. </test>
  65. `,
  66. components: {
  67. test: {
  68. data () {
  69. return {
  70. msg: 'hello',
  71. obj: { msg2: 'world', msg3: '.' }
  72. }
  73. },
  74. template: `
  75. <div>
  76. <slot :msg="msg" v-bind="obj" msg3="!"></slot>
  77. </div>
  78. `
  79. }
  80. }
  81. }).$mount()
  82. expect(vm.$el.innerHTML).toBe('<span>hello world !</span>')
  83. vm.$refs.test.msg = 'bye'
  84. vm.$refs.test.obj.msg2 = 'bye'
  85. waitForUpdate(() => {
  86. expect(vm.$el.innerHTML).toBe('<span>bye bye !</span>')
  87. }).then(done)
  88. })
  89. it('should warn when using v-bind with no object', () => {
  90. new Vue({
  91. template: `
  92. <test ref="test">
  93. <template scope="props">
  94. </template>
  95. </test>
  96. `,
  97. components: {
  98. test: {
  99. data () {
  100. return {
  101. text: 'some text'
  102. }
  103. },
  104. template: `
  105. <div>
  106. <slot v-bind="text"></slot>
  107. </div>
  108. `
  109. }
  110. }
  111. }).$mount()
  112. expect('slot v-bind without argument expects an Object').toHaveBeenWarned()
  113. })
  114. it('should not warn when using v-bind with object', () => {
  115. new Vue({
  116. template: `
  117. <test ref="test">
  118. <template scope="props">
  119. </template>
  120. </test>
  121. `,
  122. components: {
  123. test: {
  124. data () {
  125. return {
  126. foo: {
  127. text: 'some text'
  128. }
  129. }
  130. },
  131. template: `
  132. <div>
  133. <slot v-bind="foo"></slot>
  134. </div>
  135. `
  136. }
  137. }
  138. }).$mount()
  139. expect('slot v-bind without argument expects an Object').not.toHaveBeenWarned()
  140. })
  141. it('named scoped slot', done => {
  142. const vm = new Vue({
  143. template: `
  144. <test ref="test">
  145. <template slot="item" slot-scope="props">
  146. <span>{{ props.foo }}</span><span>{{ props.bar }}</span>
  147. </template>
  148. </test>
  149. `,
  150. components: {
  151. test: {
  152. data () {
  153. return { foo: 'FOO', bar: 'BAR' }
  154. },
  155. template: `
  156. <div>
  157. <slot name="item" :foo="foo" :bar="bar"></slot>
  158. </div>
  159. `
  160. }
  161. }
  162. }).$mount()
  163. expect(vm.$el.innerHTML).toBe('<span>FOO</span><span>BAR</span>')
  164. vm.$refs.test.foo = 'BAZ'
  165. waitForUpdate(() => {
  166. expect(vm.$el.innerHTML).toBe('<span>BAZ</span><span>BAR</span>')
  167. }).then(done)
  168. })
  169. it('named scoped slot (plain element)', done => {
  170. const vm = new Vue({
  171. template: `
  172. <test ref="test">
  173. <span slot="item" slot-scope="props">{{ props.foo }} {{ props.bar }}</span>
  174. </test>
  175. `,
  176. components: {
  177. test: {
  178. data () {
  179. return { foo: 'FOO', bar: 'BAR' }
  180. },
  181. template: `
  182. <div>
  183. <slot name="item" :foo="foo" :bar="bar"></slot>
  184. </div>
  185. `
  186. }
  187. }
  188. }).$mount()
  189. expect(vm.$el.innerHTML).toBe('<span>FOO BAR</span>')
  190. vm.$refs.test.foo = 'BAZ'
  191. waitForUpdate(() => {
  192. expect(vm.$el.innerHTML).toBe('<span>BAZ BAR</span>')
  193. }).then(done)
  194. })
  195. it('fallback content', () => {
  196. const vm = new Vue({
  197. template: `<test></test>`,
  198. components: {
  199. test: {
  200. data () {
  201. return { msg: 'hello' }
  202. },
  203. template: `
  204. <div>
  205. <slot name="item" :text="msg">
  206. <span>{{ msg }} fallback</span>
  207. </slot>
  208. </div>
  209. `
  210. }
  211. }
  212. }).$mount()
  213. expect(vm.$el.innerHTML).toBe('<span>hello fallback</span>')
  214. })
  215. it('slot with v-for', done => {
  216. const vm = new Vue({
  217. template: `
  218. <test ref="test">
  219. <template slot="item" slot-scope="props">
  220. <span>{{ props.text }}</span>
  221. </template>
  222. </test>
  223. `,
  224. components: {
  225. test: {
  226. data () {
  227. return {
  228. items: ['foo', 'bar', 'baz']
  229. }
  230. },
  231. template: `
  232. <div>
  233. <slot v-for="item in items" name="item" :text="item"></slot>
  234. </div>
  235. `
  236. }
  237. }
  238. }).$mount()
  239. function assertOutput () {
  240. expect(vm.$el.innerHTML).toBe(vm.$refs.test.items.map(item => {
  241. return `<span>${item}</span>`
  242. }).join(''))
  243. }
  244. assertOutput()
  245. vm.$refs.test.items.reverse()
  246. waitForUpdate(assertOutput).then(() => {
  247. vm.$refs.test.items.push('qux')
  248. }).then(assertOutput).then(done)
  249. })
  250. it('slot inside v-for', done => {
  251. const vm = new Vue({
  252. template: `
  253. <test ref="test">
  254. <template slot="item" slot-scope="props">
  255. <span>{{ props.text }}</span>
  256. </template>
  257. </test>
  258. `,
  259. components: {
  260. test: {
  261. data () {
  262. return {
  263. items: ['foo', 'bar', 'baz']
  264. }
  265. },
  266. template: `
  267. <ul>
  268. <li v-for="item in items">
  269. <slot name="item" :text="item"></slot>
  270. </li>
  271. </ul>
  272. `
  273. }
  274. }
  275. }).$mount()
  276. function assertOutput () {
  277. expect(vm.$el.innerHTML).toBe(vm.$refs.test.items.map(item => {
  278. return `<li><span>${item}</span></li>`
  279. }).join(''))
  280. }
  281. assertOutput()
  282. vm.$refs.test.items.reverse()
  283. waitForUpdate(assertOutput).then(() => {
  284. vm.$refs.test.items.push('qux')
  285. }).then(assertOutput).then(done)
  286. })
  287. it('scoped slot without scope alias', () => {
  288. const vm = new Vue({
  289. template: `
  290. <test ref="test">
  291. <span slot="item">I am static</span>
  292. </test>
  293. `,
  294. components: {
  295. test: {
  296. data () {
  297. return { msg: 'hello' }
  298. },
  299. template: `
  300. <div>
  301. <slot name="item" :text="msg"></slot>
  302. </div>
  303. `
  304. }
  305. }
  306. }).$mount()
  307. expect(vm.$el.innerHTML).toBe('<span>I am static</span>')
  308. })
  309. it('non-scoped slot with scope alias', () => {
  310. const vm = new Vue({
  311. template: `
  312. <test ref="test">
  313. <template slot="item" slot-scope="props">
  314. <span>{{ props.text || 'meh' }}</span>
  315. </template>
  316. </test>
  317. `,
  318. components: {
  319. test: {
  320. data () {
  321. return { msg: 'hello' }
  322. },
  323. template: `
  324. <div>
  325. <slot name="item"></slot>
  326. </div>
  327. `
  328. }
  329. }
  330. }).$mount()
  331. expect(vm.$el.innerHTML).toBe('<span>meh</span>')
  332. })
  333. it('warn key on slot', () => {
  334. new Vue({
  335. template: `
  336. <test ref="test">
  337. <template slot="item" slot-scope="props">
  338. <span>{{ props.text }}</span>
  339. </template>
  340. </test>
  341. `,
  342. components: {
  343. test: {
  344. data () {
  345. return {
  346. items: ['foo', 'bar', 'baz']
  347. }
  348. },
  349. template: `
  350. <div>
  351. <slot v-for="item in items" name="item" :text="item" :key="item"></slot>
  352. </div>
  353. `
  354. }
  355. }
  356. }).$mount()
  357. expect(`\`key\` does not work on <slot>`).toHaveBeenWarned()
  358. })
  359. it('render function usage (named, via data)', done => {
  360. const vm = new Vue({
  361. render (h) {
  362. return h('test', {
  363. ref: 'test',
  364. scopedSlots: {
  365. item: props => h('span', props.text)
  366. }
  367. })
  368. },
  369. components: {
  370. test: {
  371. data () {
  372. return { msg: 'hello' }
  373. },
  374. render (h) {
  375. return h('div', this.$scopedSlots.item({
  376. text: this.msg
  377. }))
  378. }
  379. }
  380. }
  381. }).$mount()
  382. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  383. vm.$refs.test.msg = 'world'
  384. waitForUpdate(() => {
  385. expect(vm.$el.innerHTML).toBe('<span>world</span>')
  386. }).then(done)
  387. })
  388. it('render function usage (default, as children)', () => {
  389. const vm = new Vue({
  390. render (h) {
  391. return h('test', [
  392. props => h('span', [props.msg])
  393. ])
  394. },
  395. components: {
  396. test: {
  397. data () {
  398. return { msg: 'hello' }
  399. },
  400. render (h) {
  401. return h('div', this.$scopedSlots.default({ msg: this.msg }))
  402. }
  403. }
  404. }
  405. }).$mount()
  406. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  407. })
  408. it('render function usage (default, as root)', () => {
  409. const vm = new Vue({
  410. render (h) {
  411. return h('test', [
  412. props => h('span', [props.msg])
  413. ])
  414. },
  415. components: {
  416. test: {
  417. data () {
  418. return { msg: 'hello' }
  419. },
  420. render (h) {
  421. const res = this.$scopedSlots.default({ msg: this.msg })
  422. // all scoped slots should be normalized into arrays
  423. expect(Array.isArray(res)).toBe(true)
  424. return res
  425. }
  426. }
  427. }
  428. }).$mount()
  429. expect(vm.$el.outerHTML).toBe('<span>hello</span>')
  430. })
  431. // new in 2.6, unifying all slots as functions
  432. it('non-scoped slots should also be available on this.$scopedSlots', () => {
  433. const vm = new Vue({
  434. template: `<foo>before <div slot="bar" slot-scope="scope">{{ scope.msg }}</div> after</foo>`,
  435. components: {
  436. foo: {
  437. render(h) {
  438. return h('div', [
  439. this.$scopedSlots.default(),
  440. this.$scopedSlots.bar({ msg: 'hi' })
  441. ])
  442. }
  443. }
  444. }
  445. }).$mount()
  446. expect(vm.$el.innerHTML).toBe(`before after<div>hi</div>`)
  447. })
  448. // #9421 the other side of unification is also needed
  449. // for library authors
  450. it('scoped slots should also be available on this.$slots', () => {
  451. const Child = {
  452. render: function (h) {
  453. return h(
  454. 'div',
  455. this.$slots.content
  456. )
  457. }
  458. }
  459. const vm = new Vue({
  460. template: `<child><template #content>foo</template></child>`,
  461. components: { Child }
  462. }).$mount()
  463. expect(vm.$el.innerHTML).toBe(`foo`)
  464. })
  465. // #4779
  466. it('should support dynamic slot target', done => {
  467. const Child = {
  468. template: `
  469. <div>
  470. <slot name="a" msg="a" />
  471. <slot name="b" msg="b" />
  472. </div>
  473. `
  474. }
  475. const vm = new Vue({
  476. data: {
  477. a: 'a',
  478. b: 'b'
  479. },
  480. template: `
  481. <child>
  482. <template :slot="a" slot-scope="props">A {{ props.msg }}</template>
  483. <template :slot="b" slot-scope="props">B {{ props.msg }}</template>
  484. </child>
  485. `,
  486. components: { Child }
  487. }).$mount()
  488. expect(vm.$el.textContent.trim()).toBe('A a B b')
  489. // switch slots
  490. vm.a = 'b'
  491. vm.b = 'a'
  492. waitForUpdate(() => {
  493. expect(vm.$el.textContent.trim()).toBe('B a A b')
  494. }).then(done)
  495. })
  496. it('render function usage (JSX)', () => {
  497. const vm = new Vue({
  498. render (h) {
  499. return <test>{
  500. props => <span>{props.msg}</span>
  501. }</test>
  502. },
  503. components: {
  504. test: {
  505. data () {
  506. return { msg: 'hello' }
  507. },
  508. render (h) {
  509. return <div>
  510. {this.$scopedSlots.default({ msg: this.msg })}
  511. </div>
  512. }
  513. }
  514. }
  515. }).$mount()
  516. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  517. })
  518. // #5615
  519. it('scoped slot with v-for', done => {
  520. const vm = new Vue({
  521. data: { names: ['foo', 'bar'] },
  522. template: `
  523. <test ref="test">
  524. <template v-for="n in names" :slot="n" slot-scope="props">
  525. <span>{{ props.msg }}</span>
  526. </template>
  527. <template slot="abc" slot-scope="props">
  528. <span>{{ props.msg }}</span>
  529. </template>
  530. </test>
  531. `,
  532. components: {
  533. test: {
  534. data: () => ({ msg: 'hello' }),
  535. template: `
  536. <div>
  537. <slot name="foo" :msg="msg + ' foo'"></slot>
  538. <slot name="bar" :msg="msg + ' bar'"></slot>
  539. <slot name="abc" :msg="msg + ' abc'"></slot>
  540. </div>
  541. `
  542. }
  543. }
  544. }).$mount()
  545. expect(vm.$el.innerHTML).toBe('<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>')
  546. vm.$refs.test.msg = 'world'
  547. waitForUpdate(() => {
  548. expect(vm.$el.innerHTML).toBe('<span>world foo</span> <span>world bar</span> <span>world abc</span>')
  549. }).then(done)
  550. })
  551. it('scoped slot with v-for (plain elements)', done => {
  552. const vm = new Vue({
  553. data: { names: ['foo', 'bar'] },
  554. template: `
  555. <test ref="test">
  556. <span v-for="n in names" :slot="n" slot-scope="props">{{ props.msg }}</span>
  557. <span slot="abc" slot-scope="props">{{ props.msg }}</span>
  558. </test>
  559. `,
  560. components: {
  561. test: {
  562. data: () => ({ msg: 'hello' }),
  563. template: `
  564. <div>
  565. <slot name="foo" :msg="msg + ' foo'"></slot>
  566. <slot name="bar" :msg="msg + ' bar'"></slot>
  567. <slot name="abc" :msg="msg + ' abc'"></slot>
  568. </div>
  569. `
  570. }
  571. }
  572. }).$mount()
  573. expect(vm.$el.innerHTML).toBe('<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>')
  574. vm.$refs.test.msg = 'world'
  575. waitForUpdate(() => {
  576. expect(vm.$el.innerHTML).toBe('<span>world foo</span> <span>world bar</span> <span>world abc</span>')
  577. }).then(done)
  578. })
  579. // #6725
  580. it('scoped slot with v-if', done => {
  581. const vm = new Vue({
  582. data: {
  583. ok: false
  584. },
  585. template: `
  586. <test>
  587. <template v-if="ok" slot-scope="foo">
  588. <p>{{ foo.text }}</p>
  589. </template>
  590. </test>
  591. `,
  592. components: {
  593. test: {
  594. data () {
  595. return { msg: 'hello' }
  596. },
  597. template: `
  598. <div>
  599. <slot :text="msg">
  600. <span>{{ msg }} fallback</span>
  601. </slot>
  602. </div>
  603. `
  604. }
  605. }
  606. }).$mount()
  607. expect(vm.$el.innerHTML).toBe('<span>hello fallback</span>')
  608. vm.ok = true
  609. waitForUpdate(() => {
  610. expect(vm.$el.innerHTML).toBe('<p>hello</p>')
  611. }).then(done)
  612. })
  613. // #9422
  614. // the behavior of the new syntax is slightly different.
  615. it('scoped slot v-if using slot-scope value', () => {
  616. const Child = {
  617. template: '<div><slot value="foo"/></div>',
  618. }
  619. const vm = new Vue({
  620. components: { Child },
  621. template: `
  622. <child>
  623. <template slot-scope="{ value }" v-if="value">
  624. foo {{ value }}
  625. </template>
  626. </child>
  627. `
  628. }).$mount()
  629. expect(vm.$el.textContent).toMatch(`foo foo`)
  630. })
  631. // 2.6 new slot syntax
  632. describe('v-slot syntax', () => {
  633. const Foo = {
  634. render(h) {
  635. return h('div', [
  636. this.$scopedSlots.default && this.$scopedSlots.default('from foo default'),
  637. this.$scopedSlots.one && this.$scopedSlots.one('from foo one'),
  638. this.$scopedSlots.two && this.$scopedSlots.two('from foo two')
  639. ])
  640. }
  641. }
  642. const Bar = {
  643. render(h) {
  644. return this.$scopedSlots.default && this.$scopedSlots.default('from bar')
  645. }
  646. }
  647. const Baz = {
  648. render(h) {
  649. return this.$scopedSlots.default && this.$scopedSlots.default('from baz')
  650. }
  651. }
  652. const toNamed = (syntax, name) => syntax[0] === '#'
  653. ? `#${name}` // shorthand
  654. : `${syntax}:${name}` // full syntax
  655. function runSuite(syntax) {
  656. it('default slot', () => {
  657. const vm = new Vue({
  658. template: `<foo ${syntax}="foo">{{ foo }}<div>{{ foo }}</div></foo>`,
  659. components: { Foo }
  660. }).$mount()
  661. expect(vm.$el.innerHTML).toBe(`from foo default<div>from foo default</div>`)
  662. })
  663. it('nested default slots', () => {
  664. const vm = new Vue({
  665. template: `
  666. <foo ${syntax}="foo">
  667. <bar ${syntax}="bar">
  668. <baz ${syntax}="baz">
  669. {{ foo }} | {{ bar }} | {{ baz }}
  670. </baz>
  671. </bar>
  672. </foo>
  673. `,
  674. components: { Foo, Bar, Baz }
  675. }).$mount()
  676. expect(vm.$el.innerHTML.trim()).toBe(`from foo default | from bar | from baz`)
  677. })
  678. it('named slots', () => {
  679. const vm = new Vue({
  680. template: `
  681. <foo>
  682. <template ${toNamed(syntax, 'default')}="foo">
  683. {{ foo }}
  684. </template>
  685. <template ${toNamed(syntax, 'one')}="one">
  686. {{ one }}
  687. </template>
  688. <template ${toNamed(syntax, 'two')}="two">
  689. {{ two }}
  690. </template>
  691. </foo>
  692. `,
  693. components: { Foo }
  694. }).$mount()
  695. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo default from foo one from foo two`)
  696. })
  697. it('nested + named + default slots', () => {
  698. const vm = new Vue({
  699. template: `
  700. <foo>
  701. <template ${toNamed(syntax, 'one')}="one">
  702. <bar ${syntax}="bar">
  703. {{ one }} {{ bar }}
  704. </bar>
  705. </template>
  706. <template ${toNamed(syntax, 'two')}="two">
  707. <baz ${syntax}="baz">
  708. {{ two }} {{ baz }}
  709. </baz>
  710. </template>
  711. </foo>
  712. `,
  713. components: { Foo, Bar, Baz }
  714. }).$mount()
  715. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo one from bar from foo two from baz`)
  716. })
  717. it('should warn v-slot usage on non-component elements', () => {
  718. const vm = new Vue({
  719. template: `<div ${syntax}="foo"/>`
  720. }).$mount()
  721. expect(`v-slot can only be used on components or <template>`).toHaveBeenWarned()
  722. })
  723. it('should warn mixed usage', () => {
  724. const vm = new Vue({
  725. template: `<foo><bar slot="one" slot-scope="bar" ${syntax}="bar"></bar></foo>`,
  726. components: { Foo, Bar }
  727. }).$mount()
  728. expect(`Unexpected mixed usage of different slot syntaxes`).toHaveBeenWarned()
  729. })
  730. }
  731. // run tests for both full syntax and shorthand
  732. runSuite('v-slot')
  733. runSuite('#default')
  734. it('shorthand named slots', () => {
  735. const vm = new Vue({
  736. template: `
  737. <foo>
  738. <template #default="foo">
  739. {{ foo }}
  740. </template>
  741. <template #one="one">
  742. {{ one }}
  743. </template>
  744. <template #two="two">
  745. {{ two }}
  746. </template>
  747. </foo>
  748. `,
  749. components: { Foo }
  750. }).$mount()
  751. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo default from foo one from foo two`)
  752. })
  753. it('should warn mixed root-default and named slots', () => {
  754. const vm = new Vue({
  755. template: `
  756. <foo #default="foo">
  757. {{ foo }}
  758. <template #one="one">
  759. {{ one }}
  760. </template>
  761. </foo>
  762. `,
  763. components: { Foo }
  764. }).$mount()
  765. expect(`default slot should also use <template>`).toHaveBeenWarned()
  766. })
  767. it('shorthand without scope variable', () => {
  768. const vm = new Vue({
  769. template: `
  770. <foo>
  771. <template #one>one</template>
  772. <template #two>two</template>
  773. </foo>
  774. `,
  775. components: { Foo }
  776. }).$mount()
  777. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`onetwo`)
  778. })
  779. it('shorthand named slots on root', () => {
  780. const vm = new Vue({
  781. template: `
  782. <foo #one="one">
  783. {{ one }}
  784. </foo>
  785. `,
  786. components: { Foo }
  787. }).$mount()
  788. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo one`)
  789. })
  790. it('dynamic slot name', done => {
  791. const vm = new Vue({
  792. data: {
  793. a: 'one',
  794. b: 'two'
  795. },
  796. template: `
  797. <foo>
  798. <template #[a]="one">a {{ one }} </template>
  799. <template v-slot:[b]="two">b {{ two }} </template>
  800. </foo>
  801. `,
  802. components: { Foo }
  803. }).$mount()
  804. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`a from foo one b from foo two`)
  805. vm.a = 'two'
  806. vm.b = 'one'
  807. waitForUpdate(() => {
  808. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`b from foo one a from foo two `)
  809. }).then(done)
  810. })
  811. it('should work with v-if/v-else', done => {
  812. const vm = new Vue({
  813. data: { flag: true },
  814. template: `
  815. <foo>
  816. <template v-if="flag" v-slot:one="one">a {{ one }} </template>
  817. <template v-else v-slot:two="two">b {{ two }} </template>
  818. </foo>
  819. `,
  820. components: { Foo }
  821. }).$mount()
  822. expect(vm.$el.innerHTML).toBe(`a from foo one `)
  823. vm.flag = false
  824. waitForUpdate(() => {
  825. expect(vm.$el.innerHTML).toBe(`b from foo two `)
  826. }).then(done)
  827. })
  828. it('warn when v-slot used on non-root <template>', () => {
  829. const vm = new Vue({
  830. template: `
  831. <foo>
  832. <template v-if="true">
  833. <template v-slot:one>foo</template>
  834. </template>
  835. </foo>
  836. `,
  837. components: { Foo }
  838. }).$mount()
  839. expect(`<template v-slot> can only appear at the root level`).toHaveBeenWarned()
  840. })
  841. })
  842. // 2.6 scoped slot perf optimization
  843. it('should have accurate tracking for scoped slots', done => {
  844. const parentUpdate = jasmine.createSpy()
  845. const childUpdate = jasmine.createSpy()
  846. const vm = new Vue({
  847. template: `
  848. <div>{{ parentCount }}<foo #default>{{ childCount }}</foo></div>
  849. `,
  850. data: {
  851. parentCount: 0,
  852. childCount: 0
  853. },
  854. updated: parentUpdate,
  855. components: {
  856. foo: {
  857. template: `<div><slot/></div>`,
  858. updated: childUpdate
  859. }
  860. }
  861. }).$mount()
  862. expect(vm.$el.innerHTML).toMatch(`0<div>0</div>`)
  863. vm.parentCount++
  864. waitForUpdate(() => {
  865. expect(vm.$el.innerHTML).toMatch(`1<div>0</div>`)
  866. // should only trigger parent update
  867. expect(parentUpdate.calls.count()).toBe(1)
  868. expect(childUpdate.calls.count()).toBe(0)
  869. vm.childCount++
  870. }).then(() => {
  871. expect(vm.$el.innerHTML).toMatch(`1<div>1</div>`)
  872. // should only trigger child update
  873. expect(parentUpdate.calls.count()).toBe(1)
  874. expect(childUpdate.calls.count()).toBe(1)
  875. }).then(done)
  876. })
  877. // #9432: async components inside a scoped slot should trigger update of the
  878. // component that invoked the scoped slot, not the lexical context component.
  879. it('async component inside scoped slot', done => {
  880. let p
  881. const vm = new Vue({
  882. template: `
  883. <foo>
  884. <template #default>
  885. <bar />
  886. </template>
  887. </foo>
  888. `,
  889. components: {
  890. foo: {
  891. template: `<div>foo<slot/></div>`
  892. },
  893. bar: resolve => {
  894. setTimeout(() => {
  895. resolve({
  896. template: `<div>bar</div>`
  897. })
  898. next()
  899. }, 0)
  900. }
  901. }
  902. }).$mount()
  903. function next () {
  904. waitForUpdate(() => {
  905. expect(vm.$el.textContent).toBe(`foobar`)
  906. }).then(done)
  907. }
  908. })
  909. // regression #9396
  910. it('should not force update child with no slot content', done => {
  911. const Child = {
  912. updated: jasmine.createSpy(),
  913. template: `<div></div>`
  914. }
  915. const parent = new Vue({
  916. template: `<div>{{ count }}<child/></div>`,
  917. data: {
  918. count: 0
  919. },
  920. components: { Child }
  921. }).$mount()
  922. expect(parent.$el.textContent).toBe(`0`)
  923. parent.count++
  924. waitForUpdate(() => {
  925. expect(parent.$el.textContent).toBe(`1`)
  926. expect(Child.updated).not.toHaveBeenCalled()
  927. }).then(done)
  928. })
  929. })