component-scoped-slot.spec.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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 $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. // #4779
  449. it('should support dynamic slot target', done => {
  450. const Child = {
  451. template: `
  452. <div>
  453. <slot name="a" msg="a" />
  454. <slot name="b" msg="b" />
  455. </div>
  456. `
  457. }
  458. const vm = new Vue({
  459. data: {
  460. a: 'a',
  461. b: 'b'
  462. },
  463. template: `
  464. <child>
  465. <template :slot="a" slot-scope="props">A {{ props.msg }}</template>
  466. <template :slot="b" slot-scope="props">B {{ props.msg }}</template>
  467. </child>
  468. `,
  469. components: { Child }
  470. }).$mount()
  471. expect(vm.$el.textContent.trim()).toBe('A a B b')
  472. // switch slots
  473. vm.a = 'b'
  474. vm.b = 'a'
  475. waitForUpdate(() => {
  476. expect(vm.$el.textContent.trim()).toBe('B a A b')
  477. }).then(done)
  478. })
  479. it('render function usage (JSX)', () => {
  480. const vm = new Vue({
  481. render (h) {
  482. return <test>{
  483. props => <span>{props.msg}</span>
  484. }</test>
  485. },
  486. components: {
  487. test: {
  488. data () {
  489. return { msg: 'hello' }
  490. },
  491. render (h) {
  492. return <div>
  493. {this.$scopedSlots.default({ msg: this.msg })}
  494. </div>
  495. }
  496. }
  497. }
  498. }).$mount()
  499. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  500. })
  501. // #5615
  502. it('scoped slot with v-for', done => {
  503. const vm = new Vue({
  504. data: { names: ['foo', 'bar'] },
  505. template: `
  506. <test ref="test">
  507. <template v-for="n in names" :slot="n" slot-scope="props">
  508. <span>{{ props.msg }}</span>
  509. </template>
  510. <template slot="abc" slot-scope="props">
  511. <span>{{ props.msg }}</span>
  512. </template>
  513. </test>
  514. `,
  515. components: {
  516. test: {
  517. data: () => ({ msg: 'hello' }),
  518. template: `
  519. <div>
  520. <slot name="foo" :msg="msg + ' foo'"></slot>
  521. <slot name="bar" :msg="msg + ' bar'"></slot>
  522. <slot name="abc" :msg="msg + ' abc'"></slot>
  523. </div>
  524. `
  525. }
  526. }
  527. }).$mount()
  528. expect(vm.$el.innerHTML).toBe('<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>')
  529. vm.$refs.test.msg = 'world'
  530. waitForUpdate(() => {
  531. expect(vm.$el.innerHTML).toBe('<span>world foo</span> <span>world bar</span> <span>world abc</span>')
  532. }).then(done)
  533. })
  534. it('scoped slot with v-for (plain elements)', done => {
  535. const vm = new Vue({
  536. data: { names: ['foo', 'bar'] },
  537. template: `
  538. <test ref="test">
  539. <span v-for="n in names" :slot="n" slot-scope="props">{{ props.msg }}</span>
  540. <span slot="abc" slot-scope="props">{{ props.msg }}</span>
  541. </test>
  542. `,
  543. components: {
  544. test: {
  545. data: () => ({ msg: 'hello' }),
  546. template: `
  547. <div>
  548. <slot name="foo" :msg="msg + ' foo'"></slot>
  549. <slot name="bar" :msg="msg + ' bar'"></slot>
  550. <slot name="abc" :msg="msg + ' abc'"></slot>
  551. </div>
  552. `
  553. }
  554. }
  555. }).$mount()
  556. expect(vm.$el.innerHTML).toBe('<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>')
  557. vm.$refs.test.msg = 'world'
  558. waitForUpdate(() => {
  559. expect(vm.$el.innerHTML).toBe('<span>world foo</span> <span>world bar</span> <span>world abc</span>')
  560. }).then(done)
  561. })
  562. // #6725
  563. it('scoped slot with v-if', done => {
  564. const vm = new Vue({
  565. data: {
  566. ok: false
  567. },
  568. template: `
  569. <test>
  570. <template v-if="ok" slot-scope="foo">
  571. <p>{{ foo.text }}</p>
  572. </template>
  573. </test>
  574. `,
  575. components: {
  576. test: {
  577. data () {
  578. return { msg: 'hello' }
  579. },
  580. template: `
  581. <div>
  582. <slot :text="msg">
  583. <span>{{ msg }} fallback</span>
  584. </slot>
  585. </div>
  586. `
  587. }
  588. }
  589. }).$mount()
  590. expect(vm.$el.innerHTML).toBe('<span>hello fallback</span>')
  591. vm.ok = true
  592. waitForUpdate(() => {
  593. expect(vm.$el.innerHTML).toBe('<p>hello</p>')
  594. }).then(done)
  595. })
  596. // #9422
  597. // the behavior of the new syntax is slightly different.
  598. it('scoped slot v-if using slot-scope value', () => {
  599. const Child = {
  600. template: '<div><slot value="foo"/></div>',
  601. }
  602. const vm = new Vue({
  603. components: { Child },
  604. template: `
  605. <child>
  606. <template slot-scope="{ value }" v-if="value">
  607. foo {{ value }}
  608. </template>
  609. </child>
  610. `
  611. }).$mount()
  612. expect(vm.$el.textContent).toMatch(`foo foo`)
  613. })
  614. // 2.6 new slot syntax
  615. describe('v-slot syntax', () => {
  616. const Foo = {
  617. render(h) {
  618. return h('div', [
  619. this.$scopedSlots.default && this.$scopedSlots.default('from foo default'),
  620. this.$scopedSlots.one && this.$scopedSlots.one('from foo one'),
  621. this.$scopedSlots.two && this.$scopedSlots.two('from foo two')
  622. ])
  623. }
  624. }
  625. const Bar = {
  626. render(h) {
  627. return this.$scopedSlots.default && this.$scopedSlots.default('from bar')
  628. }
  629. }
  630. const Baz = {
  631. render(h) {
  632. return this.$scopedSlots.default && this.$scopedSlots.default('from baz')
  633. }
  634. }
  635. const toNamed = (syntax, name) => syntax[0] === '#'
  636. ? `#${name}` // shorthand
  637. : `${syntax}:${name}` // full syntax
  638. function runSuite(syntax) {
  639. it('default slot', () => {
  640. const vm = new Vue({
  641. template: `<foo ${syntax}="foo">{{ foo }}<div>{{ foo }}</div></foo>`,
  642. components: { Foo }
  643. }).$mount()
  644. expect(vm.$el.innerHTML).toBe(`from foo default<div>from foo default</div>`)
  645. })
  646. it('nested default slots', () => {
  647. const vm = new Vue({
  648. template: `
  649. <foo ${syntax}="foo">
  650. <bar ${syntax}="bar">
  651. <baz ${syntax}="baz">
  652. {{ foo }} | {{ bar }} | {{ baz }}
  653. </baz>
  654. </bar>
  655. </foo>
  656. `,
  657. components: { Foo, Bar, Baz }
  658. }).$mount()
  659. expect(vm.$el.innerHTML.trim()).toBe(`from foo default | from bar | from baz`)
  660. })
  661. it('named slots', () => {
  662. const vm = new Vue({
  663. template: `
  664. <foo>
  665. <template ${toNamed(syntax, 'default')}="foo">
  666. {{ foo }}
  667. </template>
  668. <template ${toNamed(syntax, 'one')}="one">
  669. {{ one }}
  670. </template>
  671. <template ${toNamed(syntax, 'two')}="two">
  672. {{ two }}
  673. </template>
  674. </foo>
  675. `,
  676. components: { Foo }
  677. }).$mount()
  678. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo default from foo one from foo two`)
  679. })
  680. it('nested + named + default slots', () => {
  681. const vm = new Vue({
  682. template: `
  683. <foo>
  684. <template ${toNamed(syntax, 'one')}="one">
  685. <bar ${syntax}="bar">
  686. {{ one }} {{ bar }}
  687. </bar>
  688. </template>
  689. <template ${toNamed(syntax, 'two')}="two">
  690. <baz ${syntax}="baz">
  691. {{ two }} {{ baz }}
  692. </baz>
  693. </template>
  694. </foo>
  695. `,
  696. components: { Foo, Bar, Baz }
  697. }).$mount()
  698. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo one from bar from foo two from baz`)
  699. })
  700. it('should warn v-slot usage on non-component elements', () => {
  701. const vm = new Vue({
  702. template: `<div ${syntax}="foo"/>`
  703. }).$mount()
  704. expect(`v-slot can only be used on components or <template>`).toHaveBeenWarned()
  705. })
  706. it('should warn mixed usage', () => {
  707. const vm = new Vue({
  708. template: `<foo><bar slot="one" slot-scope="bar" ${syntax}="bar"></bar></foo>`,
  709. components: { Foo, Bar }
  710. }).$mount()
  711. expect(`Unexpected mixed usage of different slot syntaxes`).toHaveBeenWarned()
  712. })
  713. it('should warn invalid parameter expression', () => {
  714. new Vue({
  715. template: `<foo ${syntax}="1"></foo>`,
  716. components: { Foo }
  717. }).$mount();
  718. expect('invalid function parameter expression').toHaveBeenWarned()
  719. })
  720. it('should allow destructuring props with default value', () => {
  721. new Vue({
  722. template: `<foo ${syntax}="{ foo = { bar: '1' } }"></foo>`,
  723. components: { Foo }
  724. }).$mount();
  725. expect('invalid function parameter expression').not.toHaveBeenWarned()
  726. })
  727. }
  728. // run tests for both full syntax and shorthand
  729. runSuite('v-slot')
  730. runSuite('#default')
  731. it('shorthand named slots', () => {
  732. const vm = new Vue({
  733. template: `
  734. <foo>
  735. <template #default="foo">
  736. {{ foo }}
  737. </template>
  738. <template #one="one">
  739. {{ one }}
  740. </template>
  741. <template #two="two">
  742. {{ two }}
  743. </template>
  744. </foo>
  745. `,
  746. components: { Foo }
  747. }).$mount()
  748. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo default from foo one from foo two`)
  749. })
  750. it('should warn mixed root-default and named slots', () => {
  751. const vm = new Vue({
  752. template: `
  753. <foo #default="foo">
  754. {{ foo }}
  755. <template #one="one">
  756. {{ one }}
  757. </template>
  758. </foo>
  759. `,
  760. components: { Foo }
  761. }).$mount()
  762. expect(`default slot should also use <template>`).toHaveBeenWarned()
  763. })
  764. it('shorthand without scope variable', () => {
  765. const vm = new Vue({
  766. template: `
  767. <foo>
  768. <template #one>one</template>
  769. <template #two>two</template>
  770. </foo>
  771. `,
  772. components: { Foo }
  773. }).$mount()
  774. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`onetwo`)
  775. })
  776. it('shorthand named slots on root', () => {
  777. const vm = new Vue({
  778. template: `
  779. <foo #one="one">
  780. {{ one }}
  781. </foo>
  782. `,
  783. components: { Foo }
  784. }).$mount()
  785. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo one`)
  786. })
  787. it('dynamic slot name', done => {
  788. const vm = new Vue({
  789. data: {
  790. a: 'one',
  791. b: 'two'
  792. },
  793. template: `
  794. <foo>
  795. <template #[a]="one">a {{ one }} </template>
  796. <template v-slot:[b]="two">b {{ two }} </template>
  797. </foo>
  798. `,
  799. components: { Foo }
  800. }).$mount()
  801. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`a from foo one b from foo two`)
  802. vm.a = 'two'
  803. vm.b = 'one'
  804. waitForUpdate(() => {
  805. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`b from foo one a from foo two `)
  806. }).then(done)
  807. })
  808. it('should work with v-if/v-else', done => {
  809. const vm = new Vue({
  810. data: { flag: true },
  811. template: `
  812. <foo>
  813. <template v-if="flag" v-slot:one="one">a {{ one }} </template>
  814. <template v-else v-slot:two="two">b {{ two }} </template>
  815. </foo>
  816. `,
  817. components: { Foo }
  818. }).$mount()
  819. expect(vm.$el.innerHTML).toBe(`a from foo one `)
  820. vm.flag = false
  821. waitForUpdate(() => {
  822. expect(vm.$el.innerHTML).toBe(`b from foo two `)
  823. }).then(done)
  824. })
  825. it('warn when v-slot used on non-root <template>', () => {
  826. const vm = new Vue({
  827. template: `
  828. <foo>
  829. <template v-if="true">
  830. <template v-slot:one>foo</template>
  831. </template>
  832. </foo>
  833. `,
  834. components: { Foo }
  835. }).$mount()
  836. expect(`<template v-slot> can only appear at the root level`).toHaveBeenWarned()
  837. })
  838. })
  839. // 2.6 scoped slot perf optimization
  840. it('should have accurate tracking for scoped slots', done => {
  841. const parentUpdate = jasmine.createSpy()
  842. const childUpdate = jasmine.createSpy()
  843. const vm = new Vue({
  844. template: `
  845. <div>{{ parentCount }}<foo #default>{{ childCount }}</foo></div>
  846. `,
  847. data: {
  848. parentCount: 0,
  849. childCount: 0
  850. },
  851. updated: parentUpdate,
  852. components: {
  853. foo: {
  854. template: `<div><slot/></div>`,
  855. updated: childUpdate
  856. }
  857. }
  858. }).$mount()
  859. expect(vm.$el.innerHTML).toMatch(`0<div>0</div>`)
  860. vm.parentCount++
  861. waitForUpdate(() => {
  862. expect(vm.$el.innerHTML).toMatch(`1<div>0</div>`)
  863. // should only trigger parent update
  864. expect(parentUpdate.calls.count()).toBe(1)
  865. expect(childUpdate.calls.count()).toBe(0)
  866. vm.childCount++
  867. }).then(() => {
  868. expect(vm.$el.innerHTML).toMatch(`1<div>1</div>`)
  869. // should only trigger child update
  870. expect(parentUpdate.calls.count()).toBe(1)
  871. expect(childUpdate.calls.count()).toBe(1)
  872. }).then(done)
  873. })
  874. // #9432: async components inside a scoped slot should trigger update of the
  875. // component that invoked the scoped slot, not the lexical context component.
  876. it('async component inside scoped slot', done => {
  877. let p
  878. const vm = new Vue({
  879. template: `
  880. <foo>
  881. <template #default>
  882. <bar />
  883. </template>
  884. </foo>
  885. `,
  886. components: {
  887. foo: {
  888. template: `<div>foo<slot/></div>`
  889. },
  890. bar: resolve => {
  891. setTimeout(() => {
  892. resolve({
  893. template: `<div>bar</div>`
  894. })
  895. next()
  896. }, 0)
  897. }
  898. }
  899. }).$mount()
  900. function next () {
  901. waitForUpdate(() => {
  902. expect(vm.$el.textContent).toBe(`foobar`)
  903. }).then(done)
  904. }
  905. })
  906. // regression #9396
  907. it('should not force update child with no slot content', done => {
  908. const Child = {
  909. updated: jasmine.createSpy(),
  910. template: `<div></div>`
  911. }
  912. const parent = new Vue({
  913. template: `<div>{{ count }}<child/></div>`,
  914. data: {
  915. count: 0
  916. },
  917. components: { Child }
  918. }).$mount()
  919. expect(parent.$el.textContent).toBe(`0`)
  920. parent.count++
  921. waitForUpdate(() => {
  922. expect(parent.$el.textContent).toBe(`1`)
  923. expect(Child.updated).not.toHaveBeenCalled()
  924. }).then(done)
  925. })
  926. // regression #9438
  927. it('nested scoped slots update', done => {
  928. const Wrapper = {
  929. template: `<div><slot/></div>`
  930. }
  931. const Inner = {
  932. props: ['foo'],
  933. template: `<div>{{ foo }}</div>`
  934. }
  935. const Outer = {
  936. data: () => ({ foo: 1 }),
  937. template: `<div><slot :foo="foo" /></div>`
  938. }
  939. const vm = new Vue({
  940. components: { Outer, Wrapper, Inner },
  941. template: `
  942. <outer ref="outer" v-slot="props">
  943. <wrapper v-slot>
  944. <inner :foo="props.foo"/>
  945. </wrapper>
  946. </outer>
  947. `
  948. }).$mount()
  949. expect(vm.$el.textContent).toBe(`1`)
  950. vm.$refs.outer.foo++
  951. waitForUpdate(() => {
  952. expect(vm.$el.textContent).toBe(`2`)
  953. }).then(done)
  954. })
  955. it('dynamic v-bind arguments on <slot>', done => {
  956. const Foo = {
  957. data() {
  958. return {
  959. key: 'msg'
  960. }
  961. },
  962. template: `<div><slot :[key]="'hello'"/></div>`
  963. }
  964. const vm = new Vue({
  965. components: { Foo },
  966. template: `
  967. <foo ref="foo" v-slot="props">{{ props }}</foo>
  968. `
  969. }).$mount()
  970. expect(vm.$el.textContent).toBe(JSON.stringify({ msg: 'hello' }, null, 2))
  971. vm.$refs.foo.key = 'changed'
  972. waitForUpdate(() => {
  973. expect(vm.$el.textContent).toBe(JSON.stringify({ changed: 'hello' }, null, 2))
  974. }).then(done)
  975. })
  976. // #9452
  977. it('fallback for scoped slots passed multiple levels down', () => {
  978. const inner = {
  979. template: `<div><slot>fallback</slot></div>`
  980. }
  981. const wrapper = {
  982. template: `
  983. <inner>
  984. <template #default>
  985. <slot/>
  986. </template>
  987. </inner>
  988. `,
  989. components: { inner }
  990. }
  991. const vm = new Vue({
  992. components: { wrapper, inner },
  993. template: `<wrapper/>`
  994. }).$mount()
  995. expect(vm.$el.textContent).toBe(`fallback`)
  996. })
  997. it('should expose v-slot without scope on this.$slots', () => {
  998. const vm = new Vue({
  999. template: `<foo><template v-slot>hello</template></foo>`,
  1000. components: {
  1001. foo: {
  1002. render(h) {
  1003. return h('div', this.$slots.default)
  1004. }
  1005. }
  1006. }
  1007. }).$mount()
  1008. expect(vm.$el.textContent).toBe('hello')
  1009. })
  1010. it('should not expose legacy syntax scoped slot on this.$slots', () => {
  1011. const vm = new Vue({
  1012. template: `<foo><template slot-scope="foo">hello</template></foo>`,
  1013. components: {
  1014. foo: {
  1015. render(h) {
  1016. expect(this.$slots.default).toBeUndefined()
  1017. return h('div', this.$slots.default)
  1018. }
  1019. }
  1020. }
  1021. }).$mount()
  1022. expect(vm.$el.textContent).toBe('')
  1023. })
  1024. it('should expose v-slot without scope on ctx.slots() in functional', () => {
  1025. const vm = new Vue({
  1026. template: `<foo><template v-slot>hello</template></foo>`,
  1027. components: {
  1028. foo: {
  1029. functional: true,
  1030. render(h, ctx) {
  1031. return h('div', ctx.slots().default)
  1032. }
  1033. }
  1034. }
  1035. }).$mount()
  1036. expect(vm.$el.textContent).toBe('hello')
  1037. })
  1038. it('should not cache scoped slot normalization when there are a mix of normal and scoped slots', done => {
  1039. const foo = {
  1040. template: `<div><slot name="foo" /> <slot name="bar" /></div>`
  1041. }
  1042. const vm = new Vue({
  1043. data: {
  1044. msg: 'foo'
  1045. },
  1046. template: `
  1047. <foo>
  1048. <div slot="foo">{{ msg }}</div>
  1049. <template #bar><div>bar</div></template>
  1050. </foo>
  1051. `,
  1052. components: { foo }
  1053. }).$mount()
  1054. expect(vm.$el.textContent).toBe(`foo bar`)
  1055. vm.msg = 'baz'
  1056. waitForUpdate(() => {
  1057. expect(vm.$el.textContent).toBe(`baz bar`)
  1058. }).then(done)
  1059. })
  1060. // #9468
  1061. it('should support passing multiple args to scoped slot function', () => {
  1062. const foo = {
  1063. render() {
  1064. return this.$scopedSlots.default('foo', 'bar')
  1065. }
  1066. }
  1067. const vm = new Vue({
  1068. template: `<foo v-slot="foo, bar">{{ foo }} {{ bar }}</foo>`,
  1069. components: { foo }
  1070. }).$mount()
  1071. expect(vm.$el.textContent).toBe('foo bar')
  1072. })
  1073. it('should not skip updates when a scoped slot contains parent <slot/> content', done => {
  1074. const inner = {
  1075. template: `<div><slot/></div>`
  1076. }
  1077. const wrapper = {
  1078. template: `<inner v-slot><slot/></inner>`,
  1079. components: { inner }
  1080. }
  1081. const vm = new Vue({
  1082. data() {
  1083. return {
  1084. ok: true
  1085. }
  1086. },
  1087. components: { wrapper },
  1088. template: `<wrapper><div>{{ ok ? 'foo' : 'bar' }}</div></wrapper>`
  1089. }).$mount()
  1090. expect(vm.$el.textContent).toBe('foo')
  1091. vm.ok = false
  1092. waitForUpdate(() => {
  1093. expect(vm.$el.textContent).toBe('bar')
  1094. }).then(done)
  1095. })
  1096. it('should not skip updates for v-slot inside v-for', done => {
  1097. const test = {
  1098. template: `<div><slot></slot></div>`
  1099. }
  1100. const vm = new Vue({
  1101. template: `
  1102. <div>
  1103. <div v-for="i in numbers">
  1104. <test v-slot>{{ i }}</test>
  1105. </div>
  1106. </div>
  1107. `,
  1108. components: { test },
  1109. data: {
  1110. numbers: [1]
  1111. }
  1112. }).$mount()
  1113. expect(vm.$el.textContent).toBe(`1`)
  1114. vm.numbers = [2]
  1115. waitForUpdate(() => {
  1116. expect(vm.$el.textContent).toBe(`2`)
  1117. }).then(done)
  1118. })
  1119. // #9534
  1120. it('should detect conditional reuse with different slot content', done => {
  1121. const Foo = {
  1122. template: `<div><slot :n="1" /></div>`
  1123. }
  1124. const vm = new Vue({
  1125. components: { Foo },
  1126. data: {
  1127. ok: true
  1128. },
  1129. template: `
  1130. <div>
  1131. <div v-if="ok">
  1132. <foo v-slot="{ n }">{{ n }}</foo>
  1133. </div>
  1134. <div v-if="!ok">
  1135. <foo v-slot="{ n }">{{ n + 1 }}</foo>
  1136. </div>
  1137. </div>
  1138. `
  1139. }).$mount()
  1140. expect(vm.$el.textContent.trim()).toBe(`1`)
  1141. vm.ok = false
  1142. waitForUpdate(() => {
  1143. expect(vm.$el.textContent.trim()).toBe(`2`)
  1144. }).then(done)
  1145. })
  1146. // #9644
  1147. it('should factor presence of normal slots into scoped slots caching', done => {
  1148. const Wrapper = {
  1149. template: `<div>
  1150. <p>Default:<slot/></p>
  1151. <p>Content:<slot name='content'/></p>
  1152. </div>`
  1153. }
  1154. const vm = new Vue({
  1155. data: { ok: false },
  1156. components: { Wrapper },
  1157. template: `<wrapper>
  1158. <p v-if='ok'>ok</p>
  1159. <template #content>
  1160. <p v-if='ok'>ok</p>
  1161. </template>
  1162. </wrapper>`
  1163. }).$mount()
  1164. expect(vm.$el.textContent).not.toMatch(`Default:ok`)
  1165. expect(vm.$el.textContent).not.toMatch(`Content:ok`)
  1166. vm.ok = true
  1167. waitForUpdate(() => {
  1168. expect(vm.$el.textContent).toMatch(`Default:ok`)
  1169. expect(vm.$el.textContent).toMatch(`Content:ok`)
  1170. vm.ok = false
  1171. }).then(() => {
  1172. expect(vm.$el.textContent).not.toMatch(`Default:ok`)
  1173. expect(vm.$el.textContent).not.toMatch(`Content:ok`)
  1174. vm.ok = true
  1175. }).then(() => {
  1176. expect(vm.$el.textContent).toMatch(`Default:ok`)
  1177. expect(vm.$el.textContent).toMatch(`Content:ok`)
  1178. }).then(done)
  1179. })
  1180. //#9658
  1181. it('fallback for scoped slot with single v-if', () => {
  1182. const vm = new Vue({
  1183. template: `<test v-slot><template v-if="false">hi</template></test>`,
  1184. components: {
  1185. Test: {
  1186. template: `<div><slot>fallback</slot></div>`
  1187. }
  1188. }
  1189. }).$mount()
  1190. expect(vm.$el.textContent).toMatch('fallback')
  1191. })
  1192. // #9699
  1193. // Component only has normal slots, but is passing down $scopedSlots directly
  1194. // $scopedSlots should not be marked as stable in this case
  1195. it('render function passing $scopedSlots w/ normal slots down', done => {
  1196. const one = {
  1197. template: `<div><slot name="footer"/></div>`
  1198. }
  1199. const two = {
  1200. render(h) {
  1201. return h(one, {
  1202. scopedSlots: this.$scopedSlots
  1203. })
  1204. }
  1205. }
  1206. const vm = new Vue({
  1207. data: { count: 0 },
  1208. render(h) {
  1209. return h(two, [
  1210. h('span', { slot: 'footer' }, this.count)
  1211. ])
  1212. }
  1213. }).$mount()
  1214. expect(vm.$el.textContent).toMatch(`0`)
  1215. vm.count++
  1216. waitForUpdate(() => {
  1217. expect(vm.$el.textContent).toMatch(`1`)
  1218. }).then(done)
  1219. })
  1220. // #11652
  1221. it('should update when switching between two components with slot and without slot', done => {
  1222. const Child = {
  1223. template: `<div><slot/></div>`
  1224. }
  1225. const parent = new Vue({
  1226. template: `<div>
  1227. <child v-if="flag"><template #default>foo</template></child>
  1228. <child v-else></child>
  1229. </div>`,
  1230. data: {
  1231. flag: true
  1232. },
  1233. components: { Child }
  1234. }).$mount()
  1235. expect(parent.$el.textContent).toMatch(`foo`)
  1236. parent.flag=false
  1237. waitForUpdate(()=>{
  1238. expect(parent.$el.textContent).toMatch(``)
  1239. }).then(done)
  1240. })
  1241. })