component-scoped-slot.spec.ts 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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(
  140. 'slot v-bind without argument expects an Object'
  141. ).not.toHaveBeenWarned()
  142. })
  143. it('named scoped slot', done => {
  144. const vm = new Vue({
  145. template: `
  146. <test ref="test">
  147. <template slot="item" slot-scope="props">
  148. <span>{{ props.foo }}</span><span>{{ props.bar }}</span>
  149. </template>
  150. </test>
  151. `,
  152. components: {
  153. test: {
  154. data() {
  155. return { foo: 'FOO', bar: 'BAR' }
  156. },
  157. template: `
  158. <div>
  159. <slot name="item" :foo="foo" :bar="bar"></slot>
  160. </div>
  161. `
  162. }
  163. }
  164. }).$mount()
  165. expect(vm.$el.innerHTML).toBe('<span>FOO</span><span>BAR</span>')
  166. vm.$refs.test.foo = 'BAZ'
  167. waitForUpdate(() => {
  168. expect(vm.$el.innerHTML).toBe('<span>BAZ</span><span>BAR</span>')
  169. }).then(done)
  170. })
  171. it('named scoped slot (plain element)', done => {
  172. const vm = new Vue({
  173. template: `
  174. <test ref="test">
  175. <span slot="item" slot-scope="props">{{ props.foo }} {{ props.bar }}</span>
  176. </test>
  177. `,
  178. components: {
  179. test: {
  180. data() {
  181. return { foo: 'FOO', bar: 'BAR' }
  182. },
  183. template: `
  184. <div>
  185. <slot name="item" :foo="foo" :bar="bar"></slot>
  186. </div>
  187. `
  188. }
  189. }
  190. }).$mount()
  191. expect(vm.$el.innerHTML).toBe('<span>FOO BAR</span>')
  192. vm.$refs.test.foo = 'BAZ'
  193. waitForUpdate(() => {
  194. expect(vm.$el.innerHTML).toBe('<span>BAZ BAR</span>')
  195. }).then(done)
  196. })
  197. it('fallback content', () => {
  198. const vm = new Vue({
  199. template: `<test></test>`,
  200. components: {
  201. test: {
  202. data() {
  203. return { msg: 'hello' }
  204. },
  205. template: `
  206. <div>
  207. <slot name="item" :text="msg">
  208. <span>{{ msg }} fallback</span>
  209. </slot>
  210. </div>
  211. `
  212. }
  213. }
  214. }).$mount()
  215. expect(vm.$el.innerHTML).toBe('<span>hello fallback</span>')
  216. })
  217. it('slot with v-for', done => {
  218. const vm = new Vue({
  219. template: `
  220. <test ref="test">
  221. <template slot="item" slot-scope="props">
  222. <span>{{ props.text }}</span>
  223. </template>
  224. </test>
  225. `,
  226. components: {
  227. test: {
  228. data() {
  229. return {
  230. items: ['foo', 'bar', 'baz']
  231. }
  232. },
  233. template: `
  234. <div>
  235. <slot v-for="item in items" name="item" :text="item"></slot>
  236. </div>
  237. `
  238. }
  239. }
  240. }).$mount()
  241. function assertOutput() {
  242. expect(vm.$el.innerHTML).toBe(
  243. vm.$refs.test.items
  244. .map(item => {
  245. return `<span>${item}</span>`
  246. })
  247. .join('')
  248. )
  249. }
  250. assertOutput()
  251. vm.$refs.test.items.reverse()
  252. waitForUpdate(assertOutput)
  253. .then(() => {
  254. vm.$refs.test.items.push('qux')
  255. })
  256. .then(assertOutput)
  257. .then(done)
  258. })
  259. it('slot inside v-for', done => {
  260. const vm = new Vue({
  261. template: `
  262. <test ref="test">
  263. <template slot="item" slot-scope="props">
  264. <span>{{ props.text }}</span>
  265. </template>
  266. </test>
  267. `,
  268. components: {
  269. test: {
  270. data() {
  271. return {
  272. items: ['foo', 'bar', 'baz']
  273. }
  274. },
  275. template: `
  276. <ul>
  277. <li v-for="item in items">
  278. <slot name="item" :text="item"></slot>
  279. </li>
  280. </ul>
  281. `
  282. }
  283. }
  284. }).$mount()
  285. function assertOutput() {
  286. expect(vm.$el.innerHTML).toBe(
  287. vm.$refs.test.items
  288. .map(item => {
  289. return `<li><span>${item}</span></li>`
  290. })
  291. .join('')
  292. )
  293. }
  294. assertOutput()
  295. vm.$refs.test.items.reverse()
  296. waitForUpdate(assertOutput)
  297. .then(() => {
  298. vm.$refs.test.items.push('qux')
  299. })
  300. .then(assertOutput)
  301. .then(done)
  302. })
  303. it('scoped slot without scope alias', () => {
  304. const vm = new Vue({
  305. template: `
  306. <test ref="test">
  307. <span slot="item">I am static</span>
  308. </test>
  309. `,
  310. components: {
  311. test: {
  312. data() {
  313. return { msg: 'hello' }
  314. },
  315. template: `
  316. <div>
  317. <slot name="item" :text="msg"></slot>
  318. </div>
  319. `
  320. }
  321. }
  322. }).$mount()
  323. expect(vm.$el.innerHTML).toBe('<span>I am static</span>')
  324. })
  325. it('non-scoped slot with scope alias', () => {
  326. const vm = new Vue({
  327. template: `
  328. <test ref="test">
  329. <template slot="item" slot-scope="props">
  330. <span>{{ props.text || 'meh' }}</span>
  331. </template>
  332. </test>
  333. `,
  334. components: {
  335. test: {
  336. data() {
  337. return { msg: 'hello' }
  338. },
  339. template: `
  340. <div>
  341. <slot name="item"></slot>
  342. </div>
  343. `
  344. }
  345. }
  346. }).$mount()
  347. expect(vm.$el.innerHTML).toBe('<span>meh</span>')
  348. })
  349. it('warn key on slot', () => {
  350. new Vue({
  351. template: `
  352. <test ref="test">
  353. <template slot="item" slot-scope="props">
  354. <span>{{ props.text }}</span>
  355. </template>
  356. </test>
  357. `,
  358. components: {
  359. test: {
  360. data() {
  361. return {
  362. items: ['foo', 'bar', 'baz']
  363. }
  364. },
  365. template: `
  366. <div>
  367. <slot v-for="item in items" name="item" :text="item" :key="item"></slot>
  368. </div>
  369. `
  370. }
  371. }
  372. }).$mount()
  373. expect(`\`key\` does not work on <slot>`).toHaveBeenWarned()
  374. })
  375. it('render function usage (named, via data)', done => {
  376. const vm = new Vue({
  377. render(h) {
  378. return h('test', {
  379. ref: 'test',
  380. scopedSlots: {
  381. item: props => h('span', props.text)
  382. }
  383. })
  384. },
  385. components: {
  386. test: {
  387. data() {
  388. return { msg: 'hello' }
  389. },
  390. render(h) {
  391. return h(
  392. 'div',
  393. this.$scopedSlots.item({
  394. text: this.msg
  395. })
  396. )
  397. }
  398. }
  399. }
  400. }).$mount()
  401. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  402. vm.$refs.test.msg = 'world'
  403. waitForUpdate(() => {
  404. expect(vm.$el.innerHTML).toBe('<span>world</span>')
  405. }).then(done)
  406. })
  407. it('render function usage (default, as children)', () => {
  408. const vm = new Vue({
  409. render(h) {
  410. return h('test', [props => h('span', [props.msg])])
  411. },
  412. components: {
  413. test: {
  414. data() {
  415. return { msg: 'hello' }
  416. },
  417. render(h) {
  418. return h('div', this.$scopedSlots.default({ msg: this.msg }))
  419. }
  420. }
  421. }
  422. }).$mount()
  423. expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  424. })
  425. it('render function usage (default, as root)', () => {
  426. const vm = new Vue({
  427. render(h) {
  428. return h('test', [props => h('span', [props.msg])])
  429. },
  430. components: {
  431. test: {
  432. data() {
  433. return { msg: 'hello' }
  434. },
  435. render(h) {
  436. const res = this.$scopedSlots.default({ msg: this.msg })
  437. // all scoped slots should be normalized into arrays
  438. expect(Array.isArray(res)).toBe(true)
  439. return res
  440. }
  441. }
  442. }
  443. }).$mount()
  444. expect(vm.$el.outerHTML).toBe('<span>hello</span>')
  445. })
  446. // new in 2.6, unifying all slots as functions
  447. it('non-scoped slots should also be available on $scopedSlots', () => {
  448. const vm = new Vue({
  449. template: `<foo>before <div slot="bar" slot-scope="scope">{{ scope.msg }}</div> after</foo>`,
  450. components: {
  451. foo: {
  452. render(h) {
  453. return h('div', [
  454. this.$scopedSlots.default(),
  455. this.$scopedSlots.bar({ msg: 'hi' })
  456. ])
  457. }
  458. }
  459. }
  460. }).$mount()
  461. expect(vm.$el.innerHTML).toBe(`before after<div>hi</div>`)
  462. })
  463. // #4779
  464. it('should support dynamic slot target', done => {
  465. const Child = {
  466. template: `
  467. <div>
  468. <slot name="a" msg="a" />
  469. <slot name="b" msg="b" />
  470. </div>
  471. `
  472. }
  473. const vm = new Vue({
  474. data: {
  475. a: 'a',
  476. b: 'b'
  477. },
  478. template: `
  479. <child>
  480. <template :slot="a" slot-scope="props">A {{ props.msg }}</template>
  481. <template :slot="b" slot-scope="props">B {{ props.msg }}</template>
  482. </child>
  483. `,
  484. components: { Child }
  485. }).$mount()
  486. expect(vm.$el.textContent.trim()).toBe('A a B b')
  487. // switch slots
  488. vm.a = 'b'
  489. vm.b = 'a'
  490. waitForUpdate(() => {
  491. expect(vm.$el.textContent.trim()).toBe('B a A b')
  492. }).then(done)
  493. })
  494. // it('render function usage (JSX)', () => {
  495. // const vm = new Vue({
  496. // render (h) {
  497. // return (<test>{
  498. // props => <span>{props.msg}</span>
  499. // }</test>)
  500. // },
  501. // components: {
  502. // test: {
  503. // data () {
  504. // return { msg: 'hello' }
  505. // },
  506. // render (h) {
  507. // return <div>
  508. // {this.$scopedSlots.default({ msg: this.msg })}
  509. // </div>
  510. // }
  511. // }
  512. // }
  513. // }).$mount()
  514. // expect(vm.$el.innerHTML).toBe('<span>hello</span>')
  515. // })
  516. // #5615
  517. it('scoped slot with v-for', done => {
  518. const vm = new Vue({
  519. data: { names: ['foo', 'bar'] },
  520. template: `
  521. <test ref="test">
  522. <template v-for="n in names" :slot="n" slot-scope="props">
  523. <span>{{ props.msg }}</span>
  524. </template>
  525. <template slot="abc" slot-scope="props">
  526. <span>{{ props.msg }}</span>
  527. </template>
  528. </test>
  529. `,
  530. components: {
  531. test: {
  532. data: () => ({ msg: 'hello' }),
  533. template: `
  534. <div>
  535. <slot name="foo" :msg="msg + ' foo'"></slot>
  536. <slot name="bar" :msg="msg + ' bar'"></slot>
  537. <slot name="abc" :msg="msg + ' abc'"></slot>
  538. </div>
  539. `
  540. }
  541. }
  542. }).$mount()
  543. expect(vm.$el.innerHTML).toBe(
  544. '<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>'
  545. )
  546. vm.$refs.test.msg = 'world'
  547. waitForUpdate(() => {
  548. expect(vm.$el.innerHTML).toBe(
  549. '<span>world foo</span> <span>world bar</span> <span>world abc</span>'
  550. )
  551. }).then(done)
  552. })
  553. it('scoped slot with v-for (plain elements)', done => {
  554. const vm = new Vue({
  555. data: { names: ['foo', 'bar'] },
  556. template: `
  557. <test ref="test">
  558. <span v-for="n in names" :slot="n" slot-scope="props">{{ props.msg }}</span>
  559. <span slot="abc" slot-scope="props">{{ props.msg }}</span>
  560. </test>
  561. `,
  562. components: {
  563. test: {
  564. data: () => ({ msg: 'hello' }),
  565. template: `
  566. <div>
  567. <slot name="foo" :msg="msg + ' foo'"></slot>
  568. <slot name="bar" :msg="msg + ' bar'"></slot>
  569. <slot name="abc" :msg="msg + ' abc'"></slot>
  570. </div>
  571. `
  572. }
  573. }
  574. }).$mount()
  575. expect(vm.$el.innerHTML).toBe(
  576. '<span>hello foo</span> <span>hello bar</span> <span>hello abc</span>'
  577. )
  578. vm.$refs.test.msg = 'world'
  579. waitForUpdate(() => {
  580. expect(vm.$el.innerHTML).toBe(
  581. '<span>world foo</span> <span>world bar</span> <span>world abc</span>'
  582. )
  583. }).then(done)
  584. })
  585. // #6725
  586. it('scoped slot with v-if', done => {
  587. const vm = new Vue({
  588. data: {
  589. ok: false
  590. },
  591. template: `
  592. <test>
  593. <template v-if="ok" slot-scope="foo">
  594. <p>{{ foo.text }}</p>
  595. </template>
  596. </test>
  597. `,
  598. components: {
  599. test: {
  600. data() {
  601. return { msg: 'hello' }
  602. },
  603. template: `
  604. <div>
  605. <slot :text="msg">
  606. <span>{{ msg }} fallback</span>
  607. </slot>
  608. </div>
  609. `
  610. }
  611. }
  612. }).$mount()
  613. expect(vm.$el.innerHTML).toBe('<span>hello fallback</span>')
  614. vm.ok = true
  615. waitForUpdate(() => {
  616. expect(vm.$el.innerHTML).toBe('<p>hello</p>')
  617. }).then(done)
  618. })
  619. // #9422
  620. // the behavior of the new syntax is slightly different.
  621. it('scoped slot v-if using slot-scope value', () => {
  622. const Child = {
  623. template: '<div><slot value="foo"/></div>'
  624. }
  625. const vm = new Vue({
  626. components: { Child },
  627. template: `
  628. <child>
  629. <template slot-scope="{ value }" v-if="value">
  630. foo {{ value }}
  631. </template>
  632. </child>
  633. `
  634. }).$mount()
  635. expect(vm.$el.textContent).toMatch(`foo foo`)
  636. })
  637. // 2.6 new slot syntax
  638. describe('v-slot syntax', () => {
  639. const Foo = {
  640. render(h) {
  641. return h('div', [
  642. this.$scopedSlots.default &&
  643. this.$scopedSlots.default('from foo default'),
  644. this.$scopedSlots.one && this.$scopedSlots.one('from foo one'),
  645. this.$scopedSlots.two && this.$scopedSlots.two('from foo two')
  646. ])
  647. }
  648. }
  649. const Bar = {
  650. render(h) {
  651. return (
  652. this.$scopedSlots.default && this.$scopedSlots.default('from bar')
  653. )
  654. }
  655. }
  656. const Baz = {
  657. render(h) {
  658. return (
  659. this.$scopedSlots.default && this.$scopedSlots.default('from baz')
  660. )
  661. }
  662. }
  663. const toNamed = (syntax, name) =>
  664. syntax[0] === '#'
  665. ? `#${name}` // shorthand
  666. : `${syntax}:${name}` // full syntax
  667. function runSuite(syntax) {
  668. it('default slot', () => {
  669. const vm = new Vue({
  670. template: `<foo ${syntax}="foo">{{ foo }}<div>{{ foo }}</div></foo>`,
  671. components: { Foo }
  672. }).$mount()
  673. expect(vm.$el.innerHTML).toBe(
  674. `from foo default<div>from foo default</div>`
  675. )
  676. })
  677. it('nested default slots', () => {
  678. const vm = new Vue({
  679. template: `
  680. <foo ${syntax}="foo">
  681. <bar ${syntax}="bar">
  682. <baz ${syntax}="baz">
  683. {{ foo }} | {{ bar }} | {{ baz }}
  684. </baz>
  685. </bar>
  686. </foo>
  687. `,
  688. components: { Foo, Bar, Baz }
  689. }).$mount()
  690. expect(vm.$el.innerHTML.trim()).toBe(
  691. `from foo default | from bar | from baz`
  692. )
  693. })
  694. it('named slots', () => {
  695. const vm = new Vue({
  696. template: `
  697. <foo>
  698. <template ${toNamed(syntax, 'default')}="foo">
  699. {{ foo }}
  700. </template>
  701. <template ${toNamed(syntax, 'one')}="one">
  702. {{ one }}
  703. </template>
  704. <template ${toNamed(syntax, 'two')}="two">
  705. {{ two }}
  706. </template>
  707. </foo>
  708. `,
  709. components: { Foo }
  710. }).$mount()
  711. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
  712. `from foo default from foo one from foo two`
  713. )
  714. })
  715. it('nested + named + default slots', () => {
  716. const vm = new Vue({
  717. template: `
  718. <foo>
  719. <template ${toNamed(syntax, 'one')}="one">
  720. <bar ${syntax}="bar">
  721. {{ one }} {{ bar }}
  722. </bar>
  723. </template>
  724. <template ${toNamed(syntax, 'two')}="two">
  725. <baz ${syntax}="baz">
  726. {{ two }} {{ baz }}
  727. </baz>
  728. </template>
  729. </foo>
  730. `,
  731. components: { Foo, Bar, Baz }
  732. }).$mount()
  733. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
  734. `from foo one from bar from foo two from baz`
  735. )
  736. })
  737. it('should warn v-slot usage on non-component elements', () => {
  738. new Vue({
  739. template: `<div ${syntax}="foo"/>`
  740. }).$mount()
  741. expect(
  742. `v-slot can only be used on components or <template>`
  743. ).toHaveBeenWarned()
  744. })
  745. it('should warn mixed usage', () => {
  746. new Vue({
  747. template: `<foo><bar slot="one" slot-scope="bar" ${syntax}="bar"></bar></foo>`,
  748. components: { Foo, Bar }
  749. }).$mount()
  750. expect(
  751. `Unexpected mixed usage of different slot syntaxes`
  752. ).toHaveBeenWarned()
  753. })
  754. it('should warn invalid parameter expression', () => {
  755. new Vue({
  756. template: `<foo ${syntax}="1"></foo>`,
  757. components: { Foo }
  758. }).$mount()
  759. expect('invalid function parameter expression').toHaveBeenWarned()
  760. })
  761. it('should allow destructuring props with default value', () => {
  762. new Vue({
  763. template: `<foo ${syntax}="{ foo = { bar: '1' } }"></foo>`,
  764. components: { Foo }
  765. }).$mount()
  766. expect('invalid function parameter expression').not.toHaveBeenWarned()
  767. })
  768. }
  769. // run tests for both full syntax and shorthand
  770. runSuite('v-slot')
  771. runSuite('#default')
  772. it('shorthand named slots', () => {
  773. const vm = new Vue({
  774. template: `
  775. <foo>
  776. <template #default="foo">
  777. {{ foo }}
  778. </template>
  779. <template #one="one">
  780. {{ one }}
  781. </template>
  782. <template #two="two">
  783. {{ two }}
  784. </template>
  785. </foo>
  786. `,
  787. components: { Foo }
  788. }).$mount()
  789. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
  790. `from foo default from foo one from foo two`
  791. )
  792. })
  793. it('should warn mixed root-default and named slots', () => {
  794. new Vue({
  795. template: `
  796. <foo #default="foo">
  797. {{ foo }}
  798. <template #one="one">
  799. {{ one }}
  800. </template>
  801. </foo>
  802. `,
  803. components: { Foo }
  804. }).$mount()
  805. expect(`default slot should also use <template>`).toHaveBeenWarned()
  806. })
  807. it('shorthand without scope variable', () => {
  808. const vm = new Vue({
  809. template: `
  810. <foo>
  811. <template #one>one</template>
  812. <template #two>two</template>
  813. </foo>
  814. `,
  815. components: { Foo }
  816. }).$mount()
  817. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`onetwo`)
  818. })
  819. it('shorthand named slots on root', () => {
  820. const vm = new Vue({
  821. template: `
  822. <foo #one="one">
  823. {{ one }}
  824. </foo>
  825. `,
  826. components: { Foo }
  827. }).$mount()
  828. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(`from foo one`)
  829. })
  830. it('dynamic slot name', done => {
  831. const vm = new Vue({
  832. data: {
  833. a: 'one',
  834. b: 'two'
  835. },
  836. template: `
  837. <foo>
  838. <template #[a]="one">a {{ one }} </template>
  839. <template v-slot:[b]="two">b {{ two }} </template>
  840. </foo>
  841. `,
  842. components: { Foo }
  843. }).$mount()
  844. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
  845. `a from foo one b from foo two`
  846. )
  847. vm.a = 'two'
  848. vm.b = 'one'
  849. waitForUpdate(() => {
  850. expect(vm.$el.innerHTML.replace(/\s+/g, ' ')).toMatch(
  851. `b from foo one a from foo two `
  852. )
  853. }).then(done)
  854. })
  855. it('should work with v-if/v-else', done => {
  856. const vm = new Vue({
  857. data: { flag: true },
  858. template: `
  859. <foo>
  860. <template v-if="flag" v-slot:one="one">a {{ one }} </template>
  861. <template v-else v-slot:two="two">b {{ two }} </template>
  862. </foo>
  863. `,
  864. components: { Foo }
  865. }).$mount()
  866. expect(vm.$el.innerHTML).toBe(`a from foo one `)
  867. vm.flag = false
  868. waitForUpdate(() => {
  869. expect(vm.$el.innerHTML).toBe(`b from foo two `)
  870. }).then(done)
  871. })
  872. it('warn when v-slot used on non-root <template>', () => {
  873. // @ts-ignore unused
  874. const vm = new Vue({
  875. template: `
  876. <foo>
  877. <template v-if="true">
  878. <template v-slot:one>foo</template>
  879. </template>
  880. </foo>
  881. `,
  882. components: { Foo }
  883. }).$mount()
  884. expect(
  885. `<template v-slot> can only appear at the root level`
  886. ).toHaveBeenWarned()
  887. })
  888. })
  889. // 2.6 scoped slot perf optimization
  890. it('should have accurate tracking for scoped slots', done => {
  891. const parentUpdate = vi.fn()
  892. const childUpdate = vi.fn()
  893. const vm = new Vue({
  894. template: `
  895. <div>{{ parentCount }}<foo #default>{{ childCount }}</foo></div>
  896. `,
  897. data: {
  898. parentCount: 0,
  899. childCount: 0
  900. },
  901. updated: parentUpdate,
  902. components: {
  903. foo: {
  904. template: `<div><slot/></div>`,
  905. updated: childUpdate
  906. }
  907. }
  908. }).$mount()
  909. expect(vm.$el.innerHTML).toMatch(`0<div>0</div>`)
  910. vm.parentCount++
  911. waitForUpdate(() => {
  912. expect(vm.$el.innerHTML).toMatch(`1<div>0</div>`)
  913. // should only trigger parent update
  914. expect(parentUpdate.mock.calls.length).toBe(1)
  915. expect(childUpdate.mock.calls.length).toBe(0)
  916. vm.childCount++
  917. })
  918. .then(() => {
  919. expect(vm.$el.innerHTML).toMatch(`1<div>1</div>`)
  920. // should only trigger child update
  921. expect(parentUpdate.mock.calls.length).toBe(1)
  922. expect(childUpdate.mock.calls.length).toBe(1)
  923. })
  924. .then(done)
  925. })
  926. // #9432: async components inside a scoped slot should trigger update of the
  927. // component that invoked the scoped slot, not the lexical context component.
  928. it('async component inside scoped slot', done => {
  929. const vm = new Vue({
  930. template: `
  931. <foo>
  932. <template #default>
  933. <bar />
  934. </template>
  935. </foo>
  936. `,
  937. components: {
  938. foo: {
  939. template: `<div>foo<slot/></div>`
  940. },
  941. bar: resolve => {
  942. setTimeout(() => {
  943. resolve({
  944. template: `<div>bar</div>`
  945. })
  946. next()
  947. }, 0)
  948. }
  949. }
  950. }).$mount()
  951. function next() {
  952. waitForUpdate(() => {
  953. expect(vm.$el.textContent).toBe(`foobar`)
  954. }).then(done)
  955. }
  956. })
  957. // regression #9396
  958. it('should not force update child with no slot content', done => {
  959. const Child = {
  960. updated: vi.fn(),
  961. template: `<div></div>`
  962. }
  963. const parent = new Vue({
  964. template: `<div>{{ count }}<child/></div>`,
  965. data: {
  966. count: 0
  967. },
  968. components: { Child }
  969. }).$mount()
  970. expect(parent.$el.textContent).toBe(`0`)
  971. parent.count++
  972. waitForUpdate(() => {
  973. expect(parent.$el.textContent).toBe(`1`)
  974. expect(Child.updated).not.toHaveBeenCalled()
  975. }).then(done)
  976. })
  977. // regression #9438
  978. it('nested scoped slots update', done => {
  979. const Wrapper = {
  980. template: `<div><slot/></div>`
  981. }
  982. const Inner = {
  983. props: ['foo'],
  984. template: `<div>{{ foo }}</div>`
  985. }
  986. const Outer = {
  987. data: () => ({ foo: 1 }),
  988. template: `<div><slot :foo="foo" /></div>`
  989. }
  990. const vm = new Vue({
  991. components: { Outer, Wrapper, Inner },
  992. template: `
  993. <outer ref="outer" v-slot="props">
  994. <wrapper v-slot>
  995. <inner :foo="props.foo"/>
  996. </wrapper>
  997. </outer>
  998. `
  999. }).$mount()
  1000. expect(vm.$el.textContent).toBe(`1`)
  1001. vm.$refs.outer.foo++
  1002. waitForUpdate(() => {
  1003. expect(vm.$el.textContent).toBe(`2`)
  1004. }).then(done)
  1005. })
  1006. it('dynamic v-bind arguments on <slot>', done => {
  1007. const Foo = {
  1008. data() {
  1009. return {
  1010. key: 'msg'
  1011. }
  1012. },
  1013. template: `<div><slot :[key]="'hello'"/></div>`
  1014. }
  1015. const vm = new Vue({
  1016. components: { Foo },
  1017. template: `
  1018. <foo ref="foo" v-slot="props">{{ props }}</foo>
  1019. `
  1020. }).$mount()
  1021. expect(vm.$el.textContent).toBe(JSON.stringify({ msg: 'hello' }, null, 2))
  1022. vm.$refs.foo.key = 'changed'
  1023. waitForUpdate(() => {
  1024. expect(vm.$el.textContent).toBe(
  1025. JSON.stringify({ changed: 'hello' }, null, 2)
  1026. )
  1027. }).then(done)
  1028. })
  1029. // #9452
  1030. it('fallback for scoped slots passed multiple levels down', () => {
  1031. const inner = {
  1032. template: `<div><slot>fallback</slot></div>`
  1033. }
  1034. const wrapper = {
  1035. template: `
  1036. <inner>
  1037. <template #default>
  1038. <slot/>
  1039. </template>
  1040. </inner>
  1041. `,
  1042. components: { inner }
  1043. }
  1044. const vm = new Vue({
  1045. components: { wrapper, inner },
  1046. template: `<wrapper/>`
  1047. }).$mount()
  1048. expect(vm.$el.textContent).toBe(`fallback`)
  1049. })
  1050. it('should expose v-slot without scope on this.$slots', () => {
  1051. const vm = new Vue({
  1052. template: `<foo><template v-slot>hello</template></foo>`,
  1053. components: {
  1054. foo: {
  1055. render(h) {
  1056. return h('div', this.$slots.default)
  1057. }
  1058. }
  1059. }
  1060. }).$mount()
  1061. expect(vm.$el.textContent).toBe('hello')
  1062. })
  1063. it('should not expose legacy syntax scoped slot on this.$slots', () => {
  1064. const vm = new Vue({
  1065. template: `<foo><template slot-scope="foo">hello</template></foo>`,
  1066. components: {
  1067. foo: {
  1068. render(h) {
  1069. expect(this.$slots.default).toBeUndefined()
  1070. return h('div', this.$slots.default)
  1071. }
  1072. }
  1073. }
  1074. }).$mount()
  1075. expect(vm.$el.textContent).toBe('')
  1076. })
  1077. it('should expose v-slot without scope on ctx.slots() in functional', () => {
  1078. const vm = new Vue({
  1079. template: `<foo><template v-slot>hello</template></foo>`,
  1080. components: {
  1081. foo: {
  1082. functional: true,
  1083. render(h, ctx) {
  1084. return h('div', ctx.slots().default)
  1085. }
  1086. }
  1087. }
  1088. }).$mount()
  1089. expect(vm.$el.textContent).toBe('hello')
  1090. })
  1091. it('should not cache scoped slot normalization when there are a mix of normal and scoped slots', done => {
  1092. const foo = {
  1093. template: `<div><slot name="foo" /> <slot name="bar" /></div>`
  1094. }
  1095. const vm = new Vue({
  1096. data: {
  1097. msg: 'foo'
  1098. },
  1099. template: `
  1100. <foo>
  1101. <div slot="foo">{{ msg }}</div>
  1102. <template #bar><div>bar</div></template>
  1103. </foo>
  1104. `,
  1105. components: { foo }
  1106. }).$mount()
  1107. expect(vm.$el.textContent).toBe(`foo bar`)
  1108. vm.msg = 'baz'
  1109. waitForUpdate(() => {
  1110. expect(vm.$el.textContent).toBe(`baz bar`)
  1111. }).then(done)
  1112. })
  1113. // #9468
  1114. it('should support passing multiple args to scoped slot function', () => {
  1115. const foo = {
  1116. render() {
  1117. return this.$scopedSlots.default('foo', 'bar')
  1118. }
  1119. }
  1120. const vm = new Vue({
  1121. template: `<foo v-slot="foo, bar">{{ foo }} {{ bar }}</foo>`,
  1122. components: { foo }
  1123. }).$mount()
  1124. expect(vm.$el.textContent).toBe('foo bar')
  1125. })
  1126. it('should not skip updates when a scoped slot contains parent <slot/> content', done => {
  1127. const inner = {
  1128. template: `<div><slot/></div>`
  1129. }
  1130. const wrapper = {
  1131. template: `<inner v-slot><slot/></inner>`,
  1132. components: { inner }
  1133. }
  1134. const vm = new Vue({
  1135. data() {
  1136. return {
  1137. ok: true
  1138. }
  1139. },
  1140. components: { wrapper },
  1141. template: `<wrapper><div>{{ ok ? 'foo' : 'bar' }}</div></wrapper>`
  1142. }).$mount()
  1143. expect(vm.$el.textContent).toBe('foo')
  1144. vm.ok = false
  1145. waitForUpdate(() => {
  1146. expect(vm.$el.textContent).toBe('bar')
  1147. }).then(done)
  1148. })
  1149. it('should not skip updates for v-slot inside v-for', done => {
  1150. const test = {
  1151. template: `<div><slot></slot></div>`
  1152. }
  1153. const vm = new Vue({
  1154. template: `
  1155. <div>
  1156. <div v-for="i in numbers">
  1157. <test v-slot>{{ i }}</test>
  1158. </div>
  1159. </div>
  1160. `,
  1161. components: { test },
  1162. data: {
  1163. numbers: [1]
  1164. }
  1165. }).$mount()
  1166. expect(vm.$el.textContent).toBe(`1`)
  1167. vm.numbers = [2]
  1168. waitForUpdate(() => {
  1169. expect(vm.$el.textContent).toBe(`2`)
  1170. }).then(done)
  1171. })
  1172. // #9534
  1173. it('should detect conditional reuse with different slot content', done => {
  1174. const Foo = {
  1175. template: `<div><slot :n="1" /></div>`
  1176. }
  1177. const vm = new Vue({
  1178. components: { Foo },
  1179. data: {
  1180. ok: true
  1181. },
  1182. template: `
  1183. <div>
  1184. <div v-if="ok">
  1185. <foo v-slot="{ n }">{{ n }}</foo>
  1186. </div>
  1187. <div v-if="!ok">
  1188. <foo v-slot="{ n }">{{ n + 1 }}</foo>
  1189. </div>
  1190. </div>
  1191. `
  1192. }).$mount()
  1193. expect(vm.$el.textContent.trim()).toBe(`1`)
  1194. vm.ok = false
  1195. waitForUpdate(() => {
  1196. expect(vm.$el.textContent.trim()).toBe(`2`)
  1197. }).then(done)
  1198. })
  1199. // #9644
  1200. it('should factor presence of normal slots into scoped slots caching', done => {
  1201. const Wrapper = {
  1202. template: `<div>
  1203. <p>Default:<slot/></p>
  1204. <p>Content:<slot name='content'/></p>
  1205. </div>`
  1206. }
  1207. const vm = new Vue({
  1208. data: { ok: false },
  1209. components: { Wrapper },
  1210. template: `<wrapper>
  1211. <p v-if='ok'>ok</p>
  1212. <template #content>
  1213. <p v-if='ok'>ok</p>
  1214. </template>
  1215. </wrapper>`
  1216. }).$mount()
  1217. expect(vm.$el.textContent).not.toMatch(`Default:ok`)
  1218. expect(vm.$el.textContent).not.toMatch(`Content:ok`)
  1219. vm.ok = true
  1220. waitForUpdate(() => {
  1221. expect(vm.$el.textContent).toMatch(`Default:ok`)
  1222. expect(vm.$el.textContent).toMatch(`Content:ok`)
  1223. vm.ok = false
  1224. })
  1225. .then(() => {
  1226. expect(vm.$el.textContent).not.toMatch(`Default:ok`)
  1227. expect(vm.$el.textContent).not.toMatch(`Content:ok`)
  1228. vm.ok = true
  1229. })
  1230. .then(() => {
  1231. expect(vm.$el.textContent).toMatch(`Default:ok`)
  1232. expect(vm.$el.textContent).toMatch(`Content:ok`)
  1233. })
  1234. .then(done)
  1235. })
  1236. //#9658
  1237. it('fallback for scoped slot with single v-if', () => {
  1238. const vm = new Vue({
  1239. template: `<test v-slot><template v-if="false">hi</template></test>`,
  1240. components: {
  1241. Test: {
  1242. template: `<div><slot>fallback</slot></div>`
  1243. }
  1244. }
  1245. }).$mount()
  1246. expect(vm.$el.textContent).toMatch('fallback')
  1247. })
  1248. // #9699
  1249. // Component only has normal slots, but is passing down $scopedSlots directly
  1250. // $scopedSlots should not be marked as stable in this case
  1251. it('render function passing $scopedSlots w/ normal slots down', done => {
  1252. const one = {
  1253. template: `<div><slot name="footer"/></div>`
  1254. }
  1255. const two = {
  1256. render(h) {
  1257. return h(one, {
  1258. scopedSlots: this.$scopedSlots
  1259. })
  1260. }
  1261. }
  1262. const vm = new Vue({
  1263. data: { count: 0 },
  1264. render(h) {
  1265. return h(two, [h('span', { slot: 'footer' }, this.count)])
  1266. }
  1267. }).$mount()
  1268. expect(vm.$el.textContent).toMatch(`0`)
  1269. vm.count++
  1270. waitForUpdate(() => {
  1271. expect(vm.$el.textContent).toMatch(`1`)
  1272. }).then(done)
  1273. })
  1274. // #11652
  1275. it('should update when switching between two components with slot and without slot', done => {
  1276. const Child = {
  1277. template: `<div><slot/></div>`
  1278. }
  1279. const parent = new Vue({
  1280. template: `<div>
  1281. <child v-if="flag"><template #default>foo</template></child>
  1282. <child v-else></child>
  1283. </div>`,
  1284. data: {
  1285. flag: true
  1286. },
  1287. components: { Child }
  1288. }).$mount()
  1289. expect(parent.$el.textContent).toMatch(`foo`)
  1290. parent.flag = false
  1291. waitForUpdate(() => {
  1292. expect(parent.$el.textContent).toMatch(``)
  1293. }).then(done)
  1294. })
  1295. })