component-slot.spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import Vue from 'vue'
  2. describe('Component slot', () => {
  3. let vm, child
  4. function mount (options) {
  5. vm = new Vue({
  6. data: {
  7. msg: 'parent message'
  8. },
  9. template: `<div><test>${options.parentContent || ''}</test></div>`,
  10. components: {
  11. test: {
  12. template: options.childTemplate,
  13. data () {
  14. return {
  15. msg: 'child message'
  16. }
  17. }
  18. }
  19. }
  20. }).$mount()
  21. child = vm.$children[0]
  22. }
  23. it('no content', () => {
  24. mount({
  25. childTemplate: '<div><slot></slot></div>'
  26. })
  27. expect(child.$el.childNodes.length).toBe(0)
  28. })
  29. it('default slot', done => {
  30. mount({
  31. childTemplate: '<div><slot></slot></div>',
  32. parentContent: '<p>{{ msg }}</p>'
  33. })
  34. expect(child.$el.tagName).toBe('DIV')
  35. expect(child.$el.children[0].tagName).toBe('P')
  36. expect(child.$el.children[0].textContent).toBe('parent message')
  37. vm.msg = 'changed'
  38. waitForUpdate(() => {
  39. expect(child.$el.children[0].textContent).toBe('changed')
  40. }).then(done)
  41. })
  42. it('named slot', done => {
  43. mount({
  44. childTemplate: '<div><slot name="test"></slot></div>',
  45. parentContent: '<p slot="test">{{ msg }}</p>'
  46. })
  47. expect(child.$el.tagName).toBe('DIV')
  48. expect(child.$el.children[0].tagName).toBe('P')
  49. expect(child.$el.children[0].textContent).toBe('parent message')
  50. vm.msg = 'changed'
  51. waitForUpdate(() => {
  52. expect(child.$el.children[0].textContent).toBe('changed')
  53. }).then(done)
  54. })
  55. it('fallback content', () => {
  56. mount({
  57. childTemplate: '<div><slot><p>{{msg}}</p></slot></div>'
  58. })
  59. expect(child.$el.children[0].tagName).toBe('P')
  60. expect(child.$el.textContent).toBe('child message')
  61. })
  62. it('fallback content with multiple named slots', () => {
  63. mount({
  64. childTemplate: `
  65. <div>
  66. <slot name="a"><p>fallback a</p></slot>
  67. <slot name="b">fallback b</slot>
  68. </div>
  69. `,
  70. parentContent: '<p slot="b">slot b</p>'
  71. })
  72. expect(child.$el.children.length).toBe(2)
  73. expect(child.$el.children[0].textContent).toBe('fallback a')
  74. expect(child.$el.children[1].textContent).toBe('slot b')
  75. })
  76. it('fallback content with mixed named/unnamed slots', () => {
  77. mount({
  78. childTemplate: `
  79. <div>
  80. <slot><p>fallback a</p></slot>
  81. <slot name="b">fallback b</slot>
  82. </div>
  83. `,
  84. parentContent: '<p slot="b">slot b</p>'
  85. })
  86. expect(child.$el.children.length).toBe(2)
  87. expect(child.$el.children[0].textContent).toBe('fallback a')
  88. expect(child.$el.children[1].textContent).toBe('slot b')
  89. })
  90. it('selector matching multiple elements', () => {
  91. mount({
  92. childTemplate: '<div><slot name="t"></slot></div>',
  93. parentContent: '<p slot="t">1</p><div></div><p slot="t">2</p>'
  94. })
  95. expect(child.$el.innerHTML).toBe('<p>1</p><p>2</p>')
  96. })
  97. it('default content should only render parts not selected', () => {
  98. mount({
  99. childTemplate: `
  100. <div>
  101. <slot name="a"></slot>
  102. <slot></slot>
  103. <slot name="b"></slot>
  104. </div>
  105. `,
  106. parentContent: '<div>foo</div><p slot="a">1</p><p slot="b">2</p>'
  107. })
  108. expect(child.$el.innerHTML).toBe('<p>1</p> <div>foo</div> <p>2</p>')
  109. })
  110. it('name should only match children', function () {
  111. mount({
  112. childTemplate: `
  113. <div>
  114. <slot name="a"><p>fallback a</p></slot>
  115. <slot name="b"><p>fallback b</p></slot>
  116. <slot name="c"><p>fallback c</p></slot>
  117. </div>
  118. `,
  119. parentContent: `
  120. '<p slot="b">select b</p>
  121. '<span><p slot="b">nested b</p></span>
  122. '<span><p slot="c">nested c</p></span>
  123. `
  124. })
  125. expect(child.$el.children.length).toBe(3)
  126. expect(child.$el.children[0].textContent).toBe('fallback a')
  127. expect(child.$el.children[1].textContent).toBe('select b')
  128. expect(child.$el.children[2].textContent).toBe('fallback c')
  129. })
  130. it('should accept expressions in slot attribute and slot names', () => {
  131. mount({
  132. childTemplate: `<div><slot :name="'a'"></slot></div>`,
  133. parentContent: `<p>one</p><p :slot="'a'">two</p>`
  134. })
  135. expect(child.$el.innerHTML).toBe('<p>two</p>')
  136. })
  137. it('slot inside v-if', done => {
  138. const vm = new Vue({
  139. data: {
  140. a: 1,
  141. b: 2,
  142. show: true
  143. },
  144. template: '<test :show="show"><p slot="b">{{b}}</p><p>{{a}}</p></test>',
  145. components: {
  146. test: {
  147. props: ['show'],
  148. template: '<div v-if="show"><slot></slot><slot name="b"></slot></div>'
  149. }
  150. }
  151. }).$mount()
  152. expect(vm.$el.textContent).toBe('12')
  153. vm.a = 2
  154. waitForUpdate(() => {
  155. expect(vm.$el.textContent).toBe('22')
  156. vm.show = false
  157. }).then(() => {
  158. expect(vm.$el.textContent).toBe('')
  159. vm.show = true
  160. vm.a = 3
  161. }).then(() => {
  162. expect(vm.$el.textContent).toBe('32')
  163. }).then(done)
  164. })
  165. it('slot inside v-for', () => {
  166. mount({
  167. childTemplate: '<div><slot v-for="i in 3" :name="i"></slot></div>',
  168. parentContent: '<p v-for="i in 3" :slot="i">{{ i - 1 }}</p>'
  169. })
  170. expect(child.$el.innerHTML).toBe('<p>0</p><p>1</p><p>2</p>')
  171. })
  172. it('nested slots', done => {
  173. const vm = new Vue({
  174. template: '<test><test2><p>{{ msg }}</p></test2></test>',
  175. data: {
  176. msg: 'foo'
  177. },
  178. components: {
  179. test: {
  180. template: '<div><slot></slot></div>'
  181. },
  182. test2: {
  183. template: '<div><slot></slot></div>'
  184. }
  185. }
  186. }).$mount()
  187. expect(vm.$el.innerHTML).toBe('<div><p>foo</p></div>')
  188. vm.msg = 'bar'
  189. waitForUpdate(() => {
  190. expect(vm.$el.innerHTML).toBe('<div><p>bar</p></div>')
  191. }).then(done)
  192. })
  193. it('v-if on inserted content', done => {
  194. const vm = new Vue({
  195. template: '<test><p v-if="ok">{{ msg }}</p></test>',
  196. data: {
  197. ok: true,
  198. msg: 'hi'
  199. },
  200. components: {
  201. test: {
  202. template: '<div><slot>fallback</slot></div>'
  203. }
  204. }
  205. }).$mount()
  206. expect(vm.$el.innerHTML).toBe('<p>hi</p>')
  207. vm.ok = false
  208. waitForUpdate(() => {
  209. expect(vm.$el.innerHTML).toBe('fallback')
  210. vm.ok = true
  211. vm.msg = 'bye'
  212. }).then(() => {
  213. expect(vm.$el.innerHTML).toBe('<p>bye</p>')
  214. }).then(done)
  215. })
  216. it('template slot', function () {
  217. const vm = new Vue({
  218. template: '<test><template slot="test">hello</template></test>',
  219. components: {
  220. test: {
  221. template: '<div><slot name="test"></slot> world</div>'
  222. }
  223. }
  224. }).$mount()
  225. expect(vm.$el.innerHTML).toBe('hello world')
  226. })
  227. it('combined with v-for', () => {
  228. const vm = new Vue({
  229. template: '<div><test v-for="i in 3" :key="i">{{ i }}</test></div>',
  230. components: {
  231. test: {
  232. template: '<div><slot></slot></div>'
  233. }
  234. }
  235. }).$mount()
  236. expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
  237. })
  238. it('inside template v-if', () => {
  239. mount({
  240. childTemplate: `
  241. <div>
  242. <template v-if="true"><slot></slot></template>
  243. </div>
  244. `,
  245. parentContent: 'foo'
  246. })
  247. expect(child.$el.innerHTML).toBe('foo')
  248. })
  249. it('default slot should use fallback content if has only whitespace', () => {
  250. mount({
  251. childTemplate: `
  252. <div>
  253. <slot name="first"><p>first slot</p></slot>
  254. <slot><p>this is the default slot</p></slot>
  255. <slot name="second"><p>second named slot</p></slot>
  256. </div>
  257. `,
  258. parentContent: `<div slot="first">1</div> <div slot="second">2</div> <div slot="second">2+</div>`
  259. })
  260. expect(child.$el.innerHTML).toBe(
  261. '<div>1</div> <p>this is the default slot</p> <div>2</div><div>2+</div>'
  262. )
  263. })
  264. it('programmatic access to $slots', () => {
  265. const vm = new Vue({
  266. template: '<test><p slot="a">A</p><div>C</div><p slot="b">B</p></test>',
  267. components: {
  268. test: {
  269. render () {
  270. expect(this.$slots.a.length).toBe(1)
  271. expect(this.$slots.a[0].tag).toBe('p')
  272. expect(this.$slots.a[0].children.length).toBe(1)
  273. expect(this.$slots.a[0].children[0].text).toBe('A')
  274. expect(this.$slots.b.length).toBe(1)
  275. expect(this.$slots.b[0].tag).toBe('p')
  276. expect(this.$slots.b[0].children.length).toBe(1)
  277. expect(this.$slots.b[0].children[0].text).toBe('B')
  278. expect(this.$slots.default.length).toBe(1)
  279. expect(this.$slots.default[0].tag).toBe('div')
  280. expect(this.$slots.default[0].children.length).toBe(1)
  281. expect(this.$slots.default[0].children[0].text).toBe('C')
  282. return this.$slots.default[0]
  283. }
  284. }
  285. }
  286. }).$mount()
  287. expect(vm.$el.tagName).toBe('DIV')
  288. expect(vm.$el.textContent).toBe('C')
  289. })
  290. it('warn if user directly returns array', () => {
  291. new Vue({
  292. template: '<test><div></div></test>',
  293. components: {
  294. test: {
  295. render () {
  296. return this.$slots.default
  297. }
  298. }
  299. }
  300. }).$mount()
  301. expect('Render function should return a single root node').toHaveBeenWarned()
  302. })
  303. // #3254
  304. it('should not keep slot name when passed further down', () => {
  305. const vm = new Vue({
  306. template: '<test><span slot="foo">foo</span></test>',
  307. components: {
  308. test: {
  309. template: '<child><slot name="foo"></slot></child>',
  310. components: {
  311. child: {
  312. template: `
  313. <div>
  314. <div class="default"><slot></slot></div>
  315. <div class="named"><slot name="foo"></slot></div>
  316. </div>
  317. `
  318. }
  319. }
  320. }
  321. }
  322. }).$mount()
  323. expect(vm.$el.querySelector('.default').textContent).toBe('foo')
  324. expect(vm.$el.querySelector('.named').textContent).toBe('')
  325. })
  326. it('should not keep slot name when passed further down (nested)', () => {
  327. const vm = new Vue({
  328. template: '<wrap><test><span slot="foo">foo</span></test></wrap>',
  329. components: {
  330. wrap: {
  331. template: '<div><slot></slot></div>'
  332. },
  333. test: {
  334. template: '<child><slot name="foo"></slot></child>',
  335. components: {
  336. child: {
  337. template: `
  338. <div>
  339. <div class="default"><slot></slot></div>
  340. <div class="named"><slot name="foo"></slot></div>
  341. </div>
  342. `
  343. }
  344. }
  345. }
  346. }
  347. }).$mount()
  348. expect(vm.$el.querySelector('.default').textContent).toBe('foo')
  349. expect(vm.$el.querySelector('.named').textContent).toBe('')
  350. })
  351. it('should not keep slot name when passed further down (functional)', () => {
  352. const child = {
  353. template: `
  354. <div>
  355. <div class="default"><slot></slot></div>
  356. <div class="named"><slot name="foo"></slot></div>
  357. </div>
  358. `
  359. }
  360. const vm = new Vue({
  361. template: '<test><span slot="foo">foo</span></test>',
  362. components: {
  363. test: {
  364. functional: true,
  365. render (h, ctx) {
  366. const slots = ctx.slots()
  367. return h(child, slots.foo)
  368. }
  369. }
  370. }
  371. }).$mount()
  372. expect(vm.$el.querySelector('.default').textContent).toBe('foo')
  373. expect(vm.$el.querySelector('.named').textContent).toBe('')
  374. })
  375. // #3400
  376. it('named slots should be consistent across re-renders', done => {
  377. const vm = new Vue({
  378. template: `
  379. <comp>
  380. <div slot="foo">foo</div>
  381. </comp>
  382. `,
  383. components: {
  384. comp: {
  385. data () {
  386. return { a: 1 }
  387. },
  388. template: `<div><slot name="foo"></slot>{{ a }}</div>`
  389. }
  390. }
  391. }).$mount()
  392. expect(vm.$el.textContent).toBe('foo1')
  393. vm.$children[0].a = 2
  394. waitForUpdate(() => {
  395. expect(vm.$el.textContent).toBe('foo2')
  396. }).then(done)
  397. })
  398. // #3437
  399. it('should correctly re-create components in slot', done => {
  400. const calls = []
  401. const vm = new Vue({
  402. template: `
  403. <comp ref="child">
  404. <div slot="foo">
  405. <child></child>
  406. </div>
  407. </comp>
  408. `,
  409. components: {
  410. comp: {
  411. data () {
  412. return { ok: true }
  413. },
  414. template: `<div><slot name="foo" v-if="ok"></slot></div>`
  415. },
  416. child: {
  417. template: '<div>child</div>',
  418. created () {
  419. calls.push(1)
  420. },
  421. destroyed () {
  422. calls.push(2)
  423. }
  424. }
  425. }
  426. }).$mount()
  427. expect(calls).toEqual([1])
  428. vm.$refs.child.ok = false
  429. waitForUpdate(() => {
  430. expect(calls).toEqual([1, 2])
  431. vm.$refs.child.ok = true
  432. }).then(() => {
  433. expect(calls).toEqual([1, 2, 1])
  434. vm.$refs.child.ok = false
  435. }).then(() => {
  436. expect(calls).toEqual([1, 2, 1, 2])
  437. }).then(done)
  438. })
  439. it('warn duplicate slots', () => {
  440. new Vue({
  441. template: `<div>
  442. <test>
  443. <div>foo</div>
  444. <div slot="a">bar</div>
  445. </test>
  446. </div>`,
  447. components: {
  448. test: {
  449. template: `<div>
  450. <slot></slot><slot></slot>
  451. <div v-for="i in 3"><slot name="a"></slot></div>
  452. </div>`
  453. }
  454. }
  455. }).$mount()
  456. expect('Duplicate presence of slot "default"').toHaveBeenWarned()
  457. expect('Duplicate presence of slot "a"').toHaveBeenWarned()
  458. })
  459. it('should not warn valid conditional slots', () => {
  460. new Vue({
  461. template: `<div>
  462. <test>
  463. <div>foo</div>
  464. </test>
  465. </div>`,
  466. components: {
  467. test: {
  468. template: `<div>
  469. <slot v-if="true"></slot>
  470. <slot v-else></slot>
  471. </div>`
  472. }
  473. }
  474. }).$mount()
  475. expect('Duplicate presence of slot "default"').not.toHaveBeenWarned()
  476. })
  477. // #3518
  478. it('events should not break when slot is toggled by v-if', done => {
  479. const spy = jasmine.createSpy()
  480. const vm = new Vue({
  481. template: `<test><div class="click" @click="test">hi</div></test>`,
  482. methods: {
  483. test: spy
  484. },
  485. components: {
  486. test: {
  487. data: () => ({
  488. toggle: true
  489. }),
  490. template: `<div v-if="toggle"><slot></slot></div>`
  491. }
  492. }
  493. }).$mount()
  494. expect(vm.$el.textContent).toBe('hi')
  495. vm.$children[0].toggle = false
  496. waitForUpdate(() => {
  497. vm.$children[0].toggle = true
  498. }).then(() => {
  499. triggerEvent(vm.$el.querySelector('.click'), 'click')
  500. expect(spy).toHaveBeenCalled()
  501. }).then(done)
  502. })
  503. it('renders static tree with text', () => {
  504. const vm = new Vue({
  505. template: `<div><test><template><div></div>Hello<div></div></template></test></div>`,
  506. components: {
  507. test: {
  508. template: '<div><slot></slot></div>'
  509. }
  510. }
  511. })
  512. vm.$mount()
  513. expect('Error when rendering root').not.toHaveBeenWarned()
  514. })
  515. // #3872
  516. it('functional component as slot', () => {
  517. const vm = new Vue({
  518. template: `
  519. <parent>
  520. <child>one</child>
  521. <child slot="a">two</child>
  522. </parent>
  523. `,
  524. components: {
  525. parent: {
  526. template: `<div><slot name="a"></slot><slot></slot></div>`
  527. },
  528. child: {
  529. functional: true,
  530. render (h, { slots }) {
  531. return h('div', slots().default)
  532. }
  533. }
  534. }
  535. }).$mount()
  536. expect(vm.$el.innerHTML.trim()).toBe('<div>two</div><div>one</div>')
  537. })
  538. // #4209
  539. it('slot of multiple text nodes should not be infinitely merged', done => {
  540. const wrap = {
  541. template: `<inner ref="inner">foo<slot></slot></inner>`,
  542. components: {
  543. inner: {
  544. data: () => ({ a: 1 }),
  545. template: `<div>{{a}}<slot></slot></div>`
  546. }
  547. }
  548. }
  549. const vm = new Vue({
  550. template: `<wrap ref="wrap">bar</wrap>`,
  551. components: { wrap }
  552. }).$mount()
  553. expect(vm.$el.textContent).toBe('1foobar')
  554. vm.$refs.wrap.$refs.inner.a++
  555. waitForUpdate(() => {
  556. expect(vm.$el.textContent).toBe('2foobar')
  557. }).then(done)
  558. })
  559. // #4315
  560. it('functional component passing slot content to stateful child component', done => {
  561. const ComponentWithSlots = {
  562. render (h) {
  563. return h('div', this.$slots.slot1)
  564. }
  565. }
  566. const FunctionalComp = {
  567. functional: true,
  568. render (h) {
  569. return h(ComponentWithSlots, [h('span', { slot: 'slot1' }, 'foo')])
  570. }
  571. }
  572. const vm = new Vue({
  573. data: { n: 1 },
  574. render (h) {
  575. return h('div', [this.n, h(FunctionalComp)])
  576. }
  577. }).$mount()
  578. expect(vm.$el.textContent).toBe('1foo')
  579. vm.n++
  580. waitForUpdate(() => {
  581. // should not lose named slot
  582. expect(vm.$el.textContent).toBe('2foo')
  583. }).then(done)
  584. })
  585. })