-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
component-slot.spec.js
1003 lines (927 loc) · 26 KB
/
component-slot.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Vue from 'vue'
describe('Component slot', () => {
let vm, child
function mount (options) {
vm = new Vue({
data: {
msg: 'parent message'
},
template: `<div><test>${options.parentContent || ''}</test></div>`,
components: {
test: {
template: options.childTemplate,
data () {
return {
msg: 'child message'
}
}
}
}
}).$mount()
child = vm.$children[0]
}
it('no content', () => {
mount({
childTemplate: '<div><slot></slot></div>'
})
expect(child.$el.childNodes.length).toBe(0)
})
it('default slot', done => {
mount({
childTemplate: '<div><slot></slot></div>',
parentContent: '<p>{{ msg }}</p>'
})
expect(child.$el.tagName).toBe('DIV')
expect(child.$el.children[0].tagName).toBe('P')
expect(child.$el.children[0].textContent).toBe('parent message')
vm.msg = 'changed'
waitForUpdate(() => {
expect(child.$el.children[0].textContent).toBe('changed')
}).then(done)
})
it('named slot', done => {
mount({
childTemplate: '<div><slot name="test"></slot></div>',
parentContent: '<p slot="test">{{ msg }}</p>'
})
expect(child.$el.tagName).toBe('DIV')
expect(child.$el.children[0].tagName).toBe('P')
expect(child.$el.children[0].textContent).toBe('parent message')
vm.msg = 'changed'
waitForUpdate(() => {
expect(child.$el.children[0].textContent).toBe('changed')
}).then(done)
})
it('named slot with 0 as a number', done => {
mount({
childTemplate: '<div><slot :name="0"></slot></div>',
parentContent: '<p :slot="0">{{ msg }}</p>'
})
expect(child.$el.tagName).toBe('DIV')
expect(child.$el.children[0].tagName).toBe('P')
expect(child.$el.children[0].textContent).toBe('parent message')
vm.msg = 'changed'
waitForUpdate(() => {
expect(child.$el.children[0].textContent).toBe('changed')
}).then(done)
})
it('fallback content', () => {
mount({
childTemplate: '<div><slot><p>{{msg}}</p></slot></div>'
})
expect(child.$el.children[0].tagName).toBe('P')
expect(child.$el.textContent).toBe('child message')
})
it('fallback content with multiple named slots', () => {
mount({
childTemplate: `
<div>
<slot name="a"><p>fallback a</p></slot>
<slot name="b">fallback b</slot>
</div>
`,
parentContent: '<p slot="b">slot b</p>'
})
expect(child.$el.children.length).toBe(2)
expect(child.$el.children[0].textContent).toBe('fallback a')
expect(child.$el.children[1].textContent).toBe('slot b')
})
it('fallback content with mixed named/unnamed slots', () => {
mount({
childTemplate: `
<div>
<slot><p>fallback a</p></slot>
<slot name="b">fallback b</slot>
</div>
`,
parentContent: '<p slot="b">slot b</p>'
})
expect(child.$el.children.length).toBe(2)
expect(child.$el.children[0].textContent).toBe('fallback a')
expect(child.$el.children[1].textContent).toBe('slot b')
})
it('it should work with previous versions of the templates', () => {
const Test = {
render() {
var _vm = this;
var _h = _vm.$createElement;
var _c = _vm._self._c || vm._h;
return _c('div', [_vm._t("default", [_c('p', [_vm._v("slot default")])])], 2)
}
}
let vm = new Vue({
template: `<test/>`,
components: { Test }
}).$mount()
expect(vm.$el.textContent).toBe('slot default')
vm = new Vue({
template: `<test>custom content</test>`,
components: { Test }
}).$mount()
expect(vm.$el.textContent).toBe('custom content')
})
it('fallback content should not be evaluated when the parent is providing it', () => {
const test = jasmine.createSpy('test')
const vm = new Vue({
template: '<test>slot default</test>',
components: {
test: {
template: '<div><slot>{{test()}}</slot></div>',
methods: {
test () {
test()
return 'test'
}
}
}
}
}).$mount()
expect(vm.$el.textContent).toBe('slot default')
expect(test).not.toHaveBeenCalled()
})
it('selector matching multiple elements', () => {
mount({
childTemplate: '<div><slot name="t"></slot></div>',
parentContent: '<p slot="t">1</p><div></div><p slot="t">2</p>'
})
expect(child.$el.innerHTML).toBe('<p>1</p><p>2</p>')
})
it('default content should only render parts not selected', () => {
mount({
childTemplate: `
<div>
<slot name="a"></slot>
<slot></slot>
<slot name="b"></slot>
</div>
`,
parentContent: '<div>foo</div><p slot="a">1</p><p slot="b">2</p>'
})
expect(child.$el.innerHTML).toBe('<p>1</p> <div>foo</div> <p>2</p>')
})
it('name should only match children', function () {
mount({
childTemplate: `
<div>
<slot name="a"><p>fallback a</p></slot>
<slot name="b"><p>fallback b</p></slot>
<slot name="c"><p>fallback c</p></slot>
</div>
`,
parentContent: `
'<p slot="b">select b</p>
'<span><p slot="b">nested b</p></span>
'<span><p slot="c">nested c</p></span>
`
})
expect(child.$el.children.length).toBe(3)
expect(child.$el.children[0].textContent).toBe('fallback a')
expect(child.$el.children[1].textContent).toBe('select b')
expect(child.$el.children[2].textContent).toBe('fallback c')
})
it('should accept expressions in slot attribute and slot names', () => {
mount({
childTemplate: `<div><slot :name="'a'"></slot></div>`,
parentContent: `<p>one</p><p :slot="'a'">two</p>`
})
expect(child.$el.innerHTML).toBe('<p>two</p>')
})
it('slot inside v-if', done => {
const vm = new Vue({
data: {
a: 1,
b: 2,
show: true
},
template: '<test :show="show"><p slot="b">{{b}}</p><p>{{a}}</p></test>',
components: {
test: {
props: ['show'],
template: '<div v-if="show"><slot></slot><slot name="b"></slot></div>'
}
}
}).$mount()
expect(vm.$el.textContent).toBe('12')
vm.a = 2
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('22')
vm.show = false
}).then(() => {
expect(vm.$el.textContent).toBe('')
vm.show = true
vm.a = 3
}).then(() => {
expect(vm.$el.textContent).toBe('32')
}).then(done)
})
it('slot inside v-for', () => {
mount({
childTemplate: '<div><slot v-for="i in 3" :name="i"></slot></div>',
parentContent: '<p v-for="i in 3" :slot="i">{{ i - 1 }}</p>'
})
expect(child.$el.innerHTML).toBe('<p>0</p><p>1</p><p>2</p>')
})
it('nested slots', done => {
const vm = new Vue({
template: '<test><test2><p>{{ msg }}</p></test2></test>',
data: {
msg: 'foo'
},
components: {
test: {
template: '<div><slot></slot></div>'
},
test2: {
template: '<div><slot></slot></div>'
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div><p>foo</p></div>')
vm.msg = 'bar'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<div><p>bar</p></div>')
}).then(done)
})
it('v-if on inserted content', done => {
const vm = new Vue({
template: '<test><p v-if="ok">{{ msg }}</p></test>',
data: {
ok: true,
msg: 'hi'
},
components: {
test: {
template: '<div><slot>fallback</slot></div>'
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<p>hi</p>')
vm.ok = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('fallback')
vm.ok = true
vm.msg = 'bye'
}).then(() => {
expect(vm.$el.innerHTML).toBe('<p>bye</p>')
}).then(done)
})
it('template slot', function () {
const vm = new Vue({
template: '<test><template slot="test">hello</template></test>',
components: {
test: {
template: '<div><slot name="test"></slot> world</div>'
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('hello world')
})
it('combined with v-for', () => {
const vm = new Vue({
template: '<div><test v-for="i in 3" :key="i">{{ i }}</test></div>',
components: {
test: {
template: '<div><slot></slot></div>'
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>1</div><div>2</div><div>3</div>')
})
it('inside template v-if', () => {
mount({
childTemplate: `
<div>
<template v-if="true"><slot></slot></template>
</div>
`,
parentContent: 'foo'
})
expect(child.$el.innerHTML).toBe('foo')
})
it('default slot should use fallback content if has only whitespace', () => {
mount({
childTemplate: `
<div>
<slot name="first"><p>first slot</p></slot>
<slot><p>this is the default slot</p></slot>
<slot name="second"><p>second named slot</p></slot>
</div>
`,
parentContent: `<div slot="first">1</div> <div slot="second">2</div> <div slot="second">2+</div>`
})
expect(child.$el.innerHTML).toBe(
'<div>1</div> <p>this is the default slot</p> <div>2</div><div>2+</div>'
)
})
it('programmatic access to $slots', () => {
const vm = new Vue({
template: '<test><p slot="a">A</p><div>C</div><p slot="b">B</p></test>',
components: {
test: {
render () {
expect(this.$slots.a.length).toBe(1)
expect(this.$slots.a[0].tag).toBe('p')
expect(this.$slots.a[0].children.length).toBe(1)
expect(this.$slots.a[0].children[0].text).toBe('A')
expect(this.$slots.b.length).toBe(1)
expect(this.$slots.b[0].tag).toBe('p')
expect(this.$slots.b[0].children.length).toBe(1)
expect(this.$slots.b[0].children[0].text).toBe('B')
expect(this.$slots.default.length).toBe(1)
expect(this.$slots.default[0].tag).toBe('div')
expect(this.$slots.default[0].children.length).toBe(1)
expect(this.$slots.default[0].children[0].text).toBe('C')
return this.$slots.default[0]
}
}
}
}).$mount()
expect(vm.$el.tagName).toBe('DIV')
expect(vm.$el.textContent).toBe('C')
})
it('warn if user directly returns array', () => {
new Vue({
template: '<test><div slot="foo"></div><div slot="foo"></div></test>',
components: {
test: {
render () {
return this.$slots.foo
}
}
}
}).$mount()
expect('Render function should return a single root node').toHaveBeenWarned()
})
// #3254
it('should not keep slot name when passed further down', () => {
const vm = new Vue({
template: '<test><span slot="foo">foo</span></test>',
components: {
test: {
template: '<child><slot name="foo"></slot></child>',
components: {
child: {
template: `
<div>
<div class="default"><slot></slot></div>
<div class="named"><slot name="foo"></slot></div>
</div>
`
}
}
}
}
}).$mount()
expect(vm.$el.querySelector('.default').textContent).toBe('foo')
expect(vm.$el.querySelector('.named').textContent).toBe('')
})
it('should not keep slot name when passed further down (nested)', () => {
const vm = new Vue({
template: '<wrap><test><span slot="foo">foo</span></test></wrap>',
components: {
wrap: {
template: '<div><slot></slot></div>'
},
test: {
template: '<child><slot name="foo"></slot></child>',
components: {
child: {
template: `
<div>
<div class="default"><slot></slot></div>
<div class="named"><slot name="foo"></slot></div>
</div>
`
}
}
}
}
}).$mount()
expect(vm.$el.querySelector('.default').textContent).toBe('foo')
expect(vm.$el.querySelector('.named').textContent).toBe('')
})
it('should not keep slot name when passed further down (functional)', () => {
const child = {
template: `
<div>
<div class="default"><slot></slot></div>
<div class="named"><slot name="foo"></slot></div>
</div>
`
}
const vm = new Vue({
template: '<test><span slot="foo">foo</span></test>',
components: {
test: {
functional: true,
render (h, ctx) {
const slots = ctx.slots()
return h(child, slots.foo)
}
}
}
}).$mount()
expect(vm.$el.querySelector('.default').textContent).toBe('foo')
expect(vm.$el.querySelector('.named').textContent).toBe('')
})
// #3400
it('named slots should be consistent across re-renders', done => {
const vm = new Vue({
template: `
<comp>
<div slot="foo">foo</div>
</comp>
`,
components: {
comp: {
data () {
return { a: 1 }
},
template: `<div><slot name="foo"></slot>{{ a }}</div>`
}
}
}).$mount()
expect(vm.$el.textContent).toBe('foo1')
vm.$children[0].a = 2
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('foo2')
}).then(done)
})
// #3437
it('should correctly re-create components in slot', done => {
const calls = []
const vm = new Vue({
template: `
<comp ref="child">
<div slot="foo">
<child></child>
</div>
</comp>
`,
components: {
comp: {
data () {
return { ok: true }
},
template: `<div><slot name="foo" v-if="ok"></slot></div>`
},
child: {
template: '<div>child</div>',
created () {
calls.push(1)
},
destroyed () {
calls.push(2)
}
}
}
}).$mount()
expect(calls).toEqual([1])
vm.$refs.child.ok = false
waitForUpdate(() => {
expect(calls).toEqual([1, 2])
vm.$refs.child.ok = true
}).then(() => {
expect(calls).toEqual([1, 2, 1])
vm.$refs.child.ok = false
}).then(() => {
expect(calls).toEqual([1, 2, 1, 2])
}).then(done)
})
it('should support duplicate slots', done => {
const vm = new Vue({
template: `
<foo ref="foo">
<div slot="a">{{ n }}</div>
</foo>
`,
data: {
n: 1
},
components: {
foo: {
data() {
return { ok: true }
},
template: `
<div>
<slot name="a" />
<slot v-if="ok" name="a" />
<pre><slot name="a" /></pre>
</div>
`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe(`<div>1</div> <div>1</div> <pre><div>1</div></pre>`)
vm.n++
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe(`<div>2</div> <div>2</div> <pre><div>2</div></pre>`)
vm.n++
}).then(() => {
expect(vm.$el.innerHTML).toBe(`<div>3</div> <div>3</div> <pre><div>3</div></pre>`)
vm.$refs.foo.ok = false
}).then(() => {
expect(vm.$el.innerHTML).toBe(`<div>3</div> <!----> <pre><div>3</div></pre>`)
vm.n++
vm.$refs.foo.ok = true
}).then(() => {
expect(vm.$el.innerHTML).toBe(`<div>4</div> <div>4</div> <pre><div>4</div></pre>`)
}).then(done)
})
// #3518
it('events should not break when slot is toggled by v-if', done => {
const spy = jasmine.createSpy()
const vm = new Vue({
template: `<test><div class="click" @click="test">hi</div></test>`,
methods: {
test: spy
},
components: {
test: {
data: () => ({
toggle: true
}),
template: `<div v-if="toggle"><slot></slot></div>`
}
}
}).$mount()
document.body.appendChild(vm.$el)
expect(vm.$el.textContent).toBe('hi')
vm.$children[0].toggle = false
waitForUpdate(() => {
vm.$children[0].toggle = true
}).then(() => {
triggerEvent(vm.$el.querySelector('.click'), 'click')
expect(spy).toHaveBeenCalled()
}).then(() => {
document.body.removeChild(vm.$el)
}).then(done)
})
it('renders static tree with text', () => {
const vm = new Vue({
template: `<div><test><template><div></div>Hello<div></div></template></test></div>`,
components: {
test: {
template: '<div><slot></slot></div>'
}
}
})
vm.$mount()
expect('Error when rendering root').not.toHaveBeenWarned()
})
// #3872
it('functional component as slot', () => {
const vm = new Vue({
template: `
<parent>
<child>one</child>
<child slot="a">two</child>
</parent>
`,
components: {
parent: {
template: `<div><slot name="a"></slot><slot></slot></div>`
},
child: {
functional: true,
render (h, { slots }) {
return h('div', slots().default)
}
}
}
}).$mount()
expect(vm.$el.innerHTML.trim()).toBe('<div>two</div><div>one</div>')
})
// #4209
it('slot of multiple text nodes should not be infinitely merged', done => {
const wrap = {
template: `<inner ref="inner">foo<slot></slot></inner>`,
components: {
inner: {
data: () => ({ a: 1 }),
template: `<div>{{a}}<slot></slot></div>`
}
}
}
const vm = new Vue({
template: `<wrap ref="wrap">bar</wrap>`,
components: { wrap }
}).$mount()
expect(vm.$el.textContent).toBe('1foobar')
vm.$refs.wrap.$refs.inner.a++
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('2foobar')
}).then(done)
})
// #4315
it('functional component passing slot content to stateful child component', done => {
const ComponentWithSlots = {
render (h) {
return h('div', this.$slots.slot1)
}
}
const FunctionalComp = {
functional: true,
render (h) {
return h(ComponentWithSlots, [h('span', { slot: 'slot1' }, 'foo')])
}
}
const vm = new Vue({
data: { n: 1 },
render (h) {
return h('div', [this.n, h(FunctionalComp)])
}
}).$mount()
expect(vm.$el.textContent).toBe('1foo')
vm.n++
waitForUpdate(() => {
// should not lose named slot
expect(vm.$el.textContent).toBe('2foo')
}).then(done)
})
it('the elements of slot should be updated correctly', done => {
const vm = new Vue({
data: { n: 1 },
template: '<div><test><span v-for="i in n" :key="i">{{ i }}</span><input value="a"/></test></div>',
components: {
test: {
template: '<div><slot></slot></div>'
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div><span>1</span><input value="a"></div>')
const input = vm.$el.querySelector('input')
input.value = 'b'
vm.n++
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<div><span>1</span><span>2</span><input value="a"></div>')
expect(vm.$el.querySelector('input')).toBe(input)
expect(vm.$el.querySelector('input').value).toBe('b')
}).then(done)
})
// GitHub issue #5888
it('should resolve correctly slot with keep-alive', () => {
const vm = new Vue({
template: `
<div>
<container>
<keep-alive slot="foo">
<child></child>
</keep-alive>
</container>
</div>
`,
components: {
container: {
template:
'<div><slot>default</slot><slot name="foo">named</slot></div>'
},
child: {
template: '<span>foo</span>'
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>default<span>foo</span></div>')
})
// #6372, #6915
it('should handle nested components in slots properly', done => {
const TestComponent = {
template: `
<component :is="toggleEl ? 'b' : 'i'">
<slot />
</component>
`,
data () {
return {
toggleEl: true
}
}
}
const vm = new Vue({
template: `
<div>
<test-component ref="test">
<div>
<foo/>
</div>
<bar>
<foo/>
</bar>
</test-component>
</div>
`,
components: {
TestComponent,
foo: {
template: `<div>foo</div>`
},
bar: {
template: `<div>bar<slot/></div>`
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe(`<b><div><div>foo</div></div> <div>bar<div>foo</div></div></b>`)
vm.$refs.test.toggleEl = false
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe(`<i><div><div>foo</div></div> <div>bar<div>foo</div></div></i>`)
}).then(done)
})
it('should preserve slot attribute if not absorbed by a Vue component', () => {
const vm = new Vue({
template: `
<div>
<div slot="foo"></div>
</div>
`
}).$mount()
expect(vm.$el.children[0].getAttribute('slot')).toBe('foo')
})
it('passing a slot down as named slot', () => {
const Bar = {
template: `<div class="bar"><slot name="foo"/></div>`
}
const Foo = {
components: { Bar },
template: `<div class="foo"><bar><slot slot="foo"/></bar></div>`
}
const vm = new Vue({
components: { Foo },
template: `<div><foo>hello</foo></div>`
}).$mount()
expect(vm.$el.innerHTML).toBe('<div class="foo"><div class="bar">hello</div></div>')
})
it('fallback content for named template slot', () => {
const Bar = {
template: `<div class="bar"><slot name="foo">fallback</slot></div>`
}
const Foo = {
components: { Bar },
template: `<div class="foo"><bar><template slot="foo"/><slot/></template></bar></div>`
}
const vm = new Vue({
components: { Foo },
template: `<div><foo></foo></div>`
}).$mount()
expect(vm.$el.innerHTML).toBe('<div class="foo"><div class="bar">fallback</div></div>')
})
// #7106
it('should not lose functional slot across renders', done => {
const One = {
data: () => ({
foo: true
}),
render (h) {
this.foo
return h('div', this.$slots.slot)
}
}
const Two = {
render (h) {
return h('span', this.$slots.slot)
}
}
const Three = {
functional: true,
render: (h, { children }) => h('span', children)
}
const vm = new Vue({
template: `
<div>
<one ref="one">
<two slot="slot">
<three slot="slot">hello</three>
</two>
</one>
</div>
`,
components: { One, Two, Three }
}).$mount()
expect(vm.$el.textContent).toBe('hello')
// trigger re-render of <one>
vm.$refs.one.foo = false
waitForUpdate(() => {
// should still be there
expect(vm.$el.textContent).toBe('hello')
}).then(done)
})
it('should allow passing named slots as raw children down multiple layers of functional component', () => {
const CompB = {
functional: true,
render (h, { slots }) {
return slots().foo
}
}
const CompA = {
functional: true,
render (h, { children }) {
return h(CompB, children)
}
}
const vm = new Vue({
components: {
CompA
},
template: `
<div>
<comp-a>
<span slot="foo">foo</span>
</comp-a>
</div>
`
}).$mount()
expect(vm.$el.textContent).toBe('foo')
})
// #7817
it('should not match wrong named slot in functional component on re-render', done => {
const Functional = {
functional: true,
render: (h, ctx) => ctx.slots().default
}
const Stateful = {
data () {
return { ok: true }
},
render (h) {
this.ok // register dep
return h('div', [
h(Functional, this.$slots.named)
])
}
}
const vm = new Vue({
template: `<stateful ref="stateful"><div slot="named">foo</div></stateful>`,
components: { Stateful }
}).$mount()
expect(vm.$el.textContent).toBe('foo')
vm.$refs.stateful.ok = false
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('foo')
}).then(done)
})
// #7975
it('should update named slot correctly when its position in the tree changed', done => {
const ChildComponent = {
template: '<b>{{ message }}</b>',
props: ['message']
}
let parentVm
const ParentComponent = {
template: `
<div>
<span v-if="alter">
<span><slot name="foo" /></span>
</span>
<span v-else>
<slot name="foo" />
</span>
</div>
`,
data () {
return {
alter: true
}
},
mounted () {
parentVm = this
}
}
const vm = new Vue({
template: `
<parent-component>
<span slot="foo">
<child-component :message="message" />
</span>
</parent-component>
`,
components: {
ChildComponent,
ParentComponent
},
data () {
return {
message: 1
}
}
}).$mount()
expect(vm.$el.firstChild.innerHTML).toBe('<span><span><b>1</b></span></span>')
parentVm.alter = false
waitForUpdate(() => {
vm.message = 2
}).then(() => {
expect(vm.$el.firstChild.innerHTML).toBe('<span><b>2</b></span>')
}).then(done)
})
// #12102
it('v-if inside scoped slot', () => {
const vm = new Vue({
template: `<test><template #custom><span v-if="false">a</span><span>b</span></template></test>`,
components: {
test: {
template: `<div><slot name="custom"/></div>`
}
}
}).$mount()