1
+ import { h , inject } from 'vue'
1
2
import { config , mount } from '../src'
2
3
import Hello from './components/Hello.vue'
3
4
@@ -11,20 +12,33 @@ describe('config', () => {
11
12
mocks : undefined ,
12
13
provide : undefined
13
14
}
15
+
16
+ jest . clearAllMocks ( )
14
17
} )
15
18
16
19
describe ( 'components' , ( ) => {
17
20
const Component = {
18
- template : '<div>{{ msg }} <hello/></div>' ,
21
+ components : { Hello } ,
22
+ template : '<div>{{ msg }} <Hello /></div>' ,
19
23
props : [ 'msg' ]
20
24
}
21
25
22
26
it ( 'allows setting components globally' , ( ) => {
23
- config . global . components = { Hello }
24
- const wrapper1 = mount ( Component , { props : { msg : 'Wrapper1' } } )
25
- const wrapper2 = mount ( Component , { props : { msg : 'Wrapper2' } } )
26
- expect ( wrapper1 . text ( ) ) . toEqual ( 'Wrapper1 Hello world' )
27
- expect ( wrapper2 . text ( ) ) . toEqual ( 'Wrapper2 Hello world' )
27
+ const HelloLocal = {
28
+ name : 'Hello' ,
29
+ render ( ) {
30
+ return h ( 'div' , 'Hello Local' )
31
+ }
32
+ }
33
+ config . global . components = { Hello : HelloLocal }
34
+ const wrapper1 = mount ( Component , {
35
+ props : { msg : 'Wrapper1' }
36
+ } )
37
+ const wrapper2 = mount ( Component , {
38
+ props : { msg : 'Wrapper2' }
39
+ } )
40
+ expect ( wrapper1 . text ( ) ) . toEqual ( 'Wrapper1 Hello Local' )
41
+ expect ( wrapper2 . text ( ) ) . toEqual ( 'Wrapper2 Hello Local' )
28
42
} )
29
43
30
44
it ( 'allows overwriting globally set component config on a per mount instance' , ( ) => {
@@ -93,4 +107,126 @@ describe('config', () => {
93
107
) . toEqual ( 'baz' )
94
108
} )
95
109
} )
110
+
111
+ describe ( 'provide' , ( ) => {
112
+ const Comp = {
113
+ setup ( ) {
114
+ const theme = inject ( 'theme' )
115
+ return ( ) => h ( 'div' , theme )
116
+ }
117
+ }
118
+
119
+ it ( 'sets a provide everywhere' , ( ) => {
120
+ config . global . provide = {
121
+ theme : 'dark'
122
+ }
123
+ const wrapper = mount ( Comp )
124
+ expect ( wrapper . html ( ) ) . toContain ( 'dark' )
125
+ } )
126
+
127
+ it ( 'overrides with a local provide' , ( ) => {
128
+ config . global . provide = {
129
+ theme : 'dark'
130
+ }
131
+ const wrapper = mount ( Comp , {
132
+ global : {
133
+ provide : {
134
+ theme : 'light'
135
+ }
136
+ }
137
+ } )
138
+ expect ( wrapper . html ( ) ) . toContain ( 'light' )
139
+ } )
140
+ } )
141
+
142
+ describe ( 'mixins' , ( ) => {
143
+ const createdHook = jest . fn ( )
144
+ const mixin = {
145
+ created ( ) {
146
+ createdHook ( )
147
+ }
148
+ }
149
+ const Component = {
150
+ render ( ) {
151
+ return h ( 'div' )
152
+ }
153
+ }
154
+
155
+ it ( 'sets a mixin everywhere' , ( ) => {
156
+ config . global . mixins = [ mixin ]
157
+ mount ( Component )
158
+
159
+ // once on root, once in the mounted component
160
+ expect ( createdHook ) . toHaveBeenCalledTimes ( 2 )
161
+ } )
162
+
163
+ it ( 'concats with locally defined mixins' , ( ) => {
164
+ config . global . mixins = [ mixin ]
165
+ const localHook = jest . fn ( )
166
+ const localMixin = {
167
+ created ( ) {
168
+ localHook ( this . $options . name )
169
+ }
170
+ }
171
+
172
+ mount ( Component , {
173
+ global : {
174
+ mixins : [ localMixin ]
175
+ }
176
+ } )
177
+
178
+ // once on root, once in the mounted component
179
+ expect ( localHook ) . toHaveBeenCalledTimes ( 2 )
180
+ expect ( createdHook ) . toHaveBeenCalledTimes ( 2 )
181
+ } )
182
+ } )
183
+
184
+ describe ( 'stubs' , ( ) => {
185
+ const Foo = {
186
+ name : 'Foo' ,
187
+ render ( ) {
188
+ return h ( 'div' , 'real foo' )
189
+ }
190
+ }
191
+
192
+ const Component = {
193
+ render ( ) {
194
+ return h ( 'div' , h ( Foo ) )
195
+ }
196
+ }
197
+
198
+ beforeEach ( ( ) => {
199
+ config . global . stubs = {
200
+ Foo : {
201
+ name : 'Foo' ,
202
+ render ( ) {
203
+ return h ( 'div' , 'config foo stub' )
204
+ }
205
+ }
206
+ }
207
+ } )
208
+
209
+ it ( 'sets a stub globally' , ( ) => {
210
+ const wrapper = mount ( Component )
211
+
212
+ // once on root, once in the mounted component
213
+ expect ( wrapper . html ( ) ) . toContain ( 'config foo stub' )
214
+ } )
215
+
216
+ it ( 'overrides config stub with locally defined stub' , ( ) => {
217
+ const wrapper = mount ( Component , {
218
+ global : {
219
+ stubs : {
220
+ Foo : {
221
+ render ( ) {
222
+ return h ( 'div' , 'local foo stub' )
223
+ }
224
+ }
225
+ }
226
+ }
227
+ } )
228
+
229
+ expect ( wrapper . html ( ) ) . toContain ( 'local foo stub' )
230
+ } )
231
+ } )
96
232
} )
0 commit comments