@@ -2,7 +2,6 @@ import { compileToFunctions } from 'vue-template-compiler'
2
2
import mount from '~src/mount'
3
3
import Component from '~resources/components/component.vue'
4
4
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
5
- import FunctionalComponentWithSlots from '~resources/components/functional-component-with-slots.vue'
6
5
7
6
describe ( 'mount.slots' , ( ) => {
8
7
it ( 'mounts component with default slot if passed component in slot object' , ( ) => {
@@ -84,32 +83,50 @@ describe('mount.slots', () => {
84
83
} )
85
84
86
85
it ( 'mounts functional component with default slot if passed component in slot object' , ( ) => {
87
- const wrapper = mount ( FunctionalComponentWithSlots , { slots : { default : Component } } )
86
+ const TestComponent = {
87
+ name : 'component-with-slots' ,
88
+ functional : true ,
89
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
90
+ }
91
+ const wrapper = mount ( TestComponent , { slots : { default : Component } } )
88
92
expect ( wrapper . contains ( Component ) ) . to . equal ( true )
89
93
} )
90
94
91
95
it ( 'mounts component with default slot if passed component in slot object' , ( ) => {
92
- const wrapper = mount ( FunctionalComponentWithSlots , { slots : { default : [ Component ] } } )
96
+ const TestComponent = {
97
+ name : 'component-with-slots' ,
98
+ functional : true ,
99
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
100
+ }
101
+ const wrapper = mount ( TestComponent , { slots : { default : [ Component ] } } )
93
102
expect ( wrapper . contains ( Component ) ) . to . equal ( true )
94
103
} )
95
104
96
105
it ( 'mounts component with default slot if passed object with template prop in slot object' , ( ) => {
106
+ const TestComponent = {
107
+ name : 'component-with-slots' ,
108
+ functional : true ,
109
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
110
+ }
97
111
const compiled = compileToFunctions ( '<div id="div" />' )
98
- const wrapper = mount ( FunctionalComponentWithSlots , { slots : { default : [ compiled ] } } )
112
+ const wrapper = mount ( TestComponent , { slots : { default : [ compiled ] } } )
99
113
expect ( wrapper . contains ( '#div' ) ) . to . equal ( true )
100
114
} )
101
115
102
116
it ( 'mounts component with default slot if passed string in slot object' , ( ) => {
103
- const wrapper = mount ( FunctionalComponentWithSlots , { slots : { default : '<span />' } } )
117
+ const TestComponent = {
118
+ name : 'component-with-slots' ,
119
+ functional : true ,
120
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
121
+ }
122
+ const wrapper = mount ( TestComponent , { slots : { default : '<span />' } } )
104
123
expect ( wrapper . contains ( 'span' ) ) . to . equal ( true )
105
124
} )
106
125
107
126
it ( 'mounts component with named slot if passed string in slot object' , ( ) => {
108
127
const TestComponent = {
109
128
functional : true ,
110
- render ( h , ctx ) {
111
- return h ( 'div' , { } , ctx . slots ( ) . named )
112
- }
129
+ render : ( h , ctx ) => h ( 'div' , { } , ctx . slots ( ) . named )
113
130
}
114
131
const wrapper = mount ( TestComponent , { slots : { named : Component } } )
115
132
expect ( wrapper . contains ( Component ) ) . to . equal ( true )
@@ -118,9 +135,7 @@ describe('mount.slots', () => {
118
135
it ( 'mounts component with named slot if passed string in slot object in array' , ( ) => {
119
136
const TestComponent = {
120
137
functional : true ,
121
- render ( h , ctx ) {
122
- return h ( 'div' , { } , ctx . slots ( ) . named )
123
- }
138
+ render : ( h , ctx ) => h ( 'div' , { } , ctx . slots ( ) . named )
124
139
}
125
140
const wrapper = mount ( TestComponent , { slots : { named : [ Component ] } } )
126
141
expect ( wrapper . contains ( Component ) ) . to . equal ( true )
@@ -129,9 +144,7 @@ describe('mount.slots', () => {
129
144
it ( 'mounts component with named slot if passed string in slot object in array' , ( ) => {
130
145
const TestComponent = {
131
146
functional : true ,
132
- render ( h , ctx ) {
133
- return h ( 'div' , { } , ctx . slots ( ) . named )
134
- }
147
+ render : ( h , ctx ) => h ( 'div' , { } , ctx . slots ( ) . named )
135
148
}
136
149
const wrapper = mount ( TestComponent , { slots : { named : '<span />' } } )
137
150
expect ( wrapper . contains ( 'span' ) ) . to . equal ( true )
@@ -140,11 +153,72 @@ describe('mount.slots', () => {
140
153
it ( 'mounts component with named slot if passed string in slot object in array' , ( ) => {
141
154
const TestComponent = {
142
155
functional : true ,
143
- render ( h , ctx ) {
144
- return h ( 'div' , { } , ctx . slots ( ) . named )
145
- }
156
+ render : ( h , ctx ) => h ( 'div' , { } , ctx . slots ( ) . named )
146
157
}
147
158
const wrapper = mount ( TestComponent , { slots : { named : [ '<span />' ] } } )
148
159
expect ( wrapper . contains ( 'span' ) ) . to . equal ( true )
149
160
} )
161
+
162
+ it ( 'throws error if passed false for named slots' , ( ) => {
163
+ const TestComponent = {
164
+ name : 'component-with-slots' ,
165
+ functional : true ,
166
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
167
+ }
168
+ const fn = ( ) => mount ( TestComponent , { slots : { named : [ false ] } } )
169
+ const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
170
+ expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
171
+ } )
172
+
173
+ it ( 'throws error if passed a number for named slots' , ( ) => {
174
+ const TestComponent = {
175
+ name : 'component-with-slots' ,
176
+ functional : true ,
177
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
178
+ }
179
+ const fn = ( ) => mount ( TestComponent , { slots : { named : [ 1 ] } } )
180
+ const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
181
+ expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
182
+ } )
183
+ it ( 'throws error if passed false for named slots' , ( ) => {
184
+ const TestComponent = {
185
+ name : 'component-with-slots' ,
186
+ functional : true ,
187
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
188
+ }
189
+ const fn = ( ) => mount ( TestComponent , { slots : { named : false } } )
190
+ const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
191
+ expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
192
+ } )
193
+
194
+ it ( 'throws error if passed a number for named slots' , ( ) => {
195
+ const TestComponent = {
196
+ name : 'component-with-slots' ,
197
+ functional : true ,
198
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
199
+ }
200
+ const fn = ( ) => mount ( TestComponent , { slots : { named : 1 } } )
201
+ const message = '[vue-test-utils]: slots[key] must be a Component, string or an array of Components'
202
+ expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
203
+ } )
204
+ it ( 'throws error if passed string in default slot array when vue-template-compiler is undefined' , ( ) => {
205
+ const TestComponent = {
206
+ name : 'component-with-slots' ,
207
+ functional : true ,
208
+ render : ( h , ctx ) => h ( 'div' , ctx . data , ctx . slots ( ) . default )
209
+ }
210
+ const compilerSave = require . cache [ require . resolve ( 'vue-template-compiler' ) ] . exports . compileToFunctions
211
+ require . cache [ require . resolve ( 'vue-template-compiler' ) ] . exports . compileToFunctions = undefined
212
+ delete require . cache [ require . resolve ( '../../../../../src/mount' ) ]
213
+ const mountFresh = require ( '../../../../../src/mount' ) . default
214
+ const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
215
+ const fn = ( ) => mountFresh ( TestComponent , { slots : { default : [ '<span />' ] } } )
216
+ try {
217
+ expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
218
+ } catch ( err ) {
219
+ require . cache [ require . resolve ( 'vue-template-compiler' ) ] . exports . compileToFunctions = compilerSave
220
+ throw err
221
+ }
222
+ require . cache [ require . resolve ( 'vue-template-compiler' ) ] . exports . compileToFunctions = compilerSave
223
+ } )
150
224
} )
0 commit comments