@@ -2,8 +2,10 @@ import { compileToFunctions } from 'vue-template-compiler'
2
2
import Component from '~resources/components/component.vue'
3
3
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
4
4
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
5
+ import ComponentWithParentName from '~resources/components/component-with-parent-name.vue'
5
6
import { describeWithMountingMethods , vueVersion } from '~resources/utils'
6
7
import { itSkipIf , itDoNotRunIf } from 'conditional-specs'
8
+ import { mount , createLocalVue } from '~vue/test-utils'
7
9
8
10
describeWithMountingMethods ( 'options.slots' , mountingMethod => {
9
11
it ( 'mounts component with default slot if passed component in slot object' , ( ) => {
@@ -221,7 +223,7 @@ describeWithMountingMethods('options.slots', mountingMethod => {
221
223
}
222
224
} )
223
225
224
- it ( 'mounts component with text slot' , ( ) => {
226
+ it ( 'mounts component with default and named text slot' , ( ) => {
225
227
const wrapper = mountingMethod ( ComponentWithSlots , {
226
228
slots : {
227
229
default : 'hello,' ,
@@ -235,6 +237,24 @@ describeWithMountingMethods('options.slots', mountingMethod => {
235
237
}
236
238
} )
237
239
240
+ it ( 'mounts functional component with only named text slot' , ( ) => {
241
+ const TestComponent = {
242
+ name : 'component-with-slots' ,
243
+ functional : true ,
244
+ render : ( h , ctx ) => h ( 'div' , ctx . data , [ ctx . slots ( ) . default , ctx . slots ( ) . footer ] )
245
+ }
246
+ const wrapper = mountingMethod ( TestComponent , {
247
+ slots : {
248
+ footer : 'foo'
249
+ }
250
+ } )
251
+ if ( mountingMethod . name === 'renderToString' ) {
252
+ expect ( wrapper ) . contains ( 'foo' )
253
+ } else {
254
+ expect ( wrapper . text ( ) ) . to . equal ( 'foo' )
255
+ }
256
+ } )
257
+
238
258
it ( 'mounts functional component with text slot' , ( ) => {
239
259
const TestComponent = {
240
260
name : 'component-with-slots' ,
@@ -568,4 +588,68 @@ describeWithMountingMethods('options.slots', mountingMethod => {
568
588
expect ( wrapper . contains ( ComponentAsAClass ) ) . to . equal ( true )
569
589
}
570
590
} )
591
+
592
+ itDoNotRunIf (
593
+ mountingMethod . name === 'renderToString' ,
594
+ 'sets a component which can access the parent component and the child component' ,
595
+ ( ) => {
596
+ const childComponentName = 'component-with-parent-name'
597
+ const localVue = createLocalVue ( )
598
+ localVue . prototype . bar = 'FOO'
599
+ let ParentComponent = mount (
600
+ {
601
+ name : 'parentComponent' ,
602
+ template : '<div><slot /></div>' ,
603
+ data ( ) {
604
+ return {
605
+ childComponentName : ''
606
+ }
607
+ }
608
+ } ,
609
+ {
610
+ components : {
611
+ ComponentWithParentName
612
+ } ,
613
+ slots : {
614
+ default : [
615
+ '<component-with-parent-name :fromLocalVue="bar" />' ,
616
+ '<component-with-parent-name :fromLocalVue="bar" />'
617
+ ]
618
+ } ,
619
+ localVue
620
+ }
621
+ )
622
+ expect ( ParentComponent . vm . childComponentName ) . to . equal ( childComponentName )
623
+ expect ( ParentComponent . vm . $children . length ) . to . equal ( 2 )
624
+ expect ( ParentComponent . vm . $children . every ( c => c . $options . name === childComponentName ) ) . to . equal ( true )
625
+ expect ( ParentComponent . html ( ) ) . to . equal ( '<div><div><span baz="qux">FOO,quux</span></div><div><span baz="qux">FOO,quux</span></div></div>' )
626
+
627
+ ParentComponent = mount (
628
+ {
629
+ name : 'parentComponent' ,
630
+ template : '<div><slot /></div>' ,
631
+ data ( ) {
632
+ return {
633
+ childComponentName : ''
634
+ }
635
+ }
636
+ } ,
637
+ {
638
+ slots : {
639
+ default : {
640
+ name : childComponentName ,
641
+ template : '<p>1234</p>' ,
642
+ mounted ( ) {
643
+ this . $parent . childComponentName = this . $options . name
644
+ }
645
+ }
646
+ }
647
+ }
648
+ )
649
+ expect ( ParentComponent . vm . childComponentName ) . to . equal ( childComponentName )
650
+ expect ( ParentComponent . vm . $children . length ) . to . equal ( 1 )
651
+ expect ( ParentComponent . vm . $children . every ( c => c . $options . name === childComponentName ) ) . to . equal ( true )
652
+ expect ( ParentComponent . html ( ) ) . to . equal ( '<div><p>1234</p></div>' )
653
+ }
654
+ )
571
655
} )
0 commit comments