forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.spec.js
400 lines (364 loc) · 11.1 KB
/
mount.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
import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'
import { mount, createLocalVue } from '~vue/test-utils'
import Component from '~resources/components/component.vue'
import ComponentWithProps from '~resources/components/component-with-props.vue'
import ComponentWithMixin from '~resources/components/component-with-mixin.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import { injectSupported, vueVersion } from '~resources/utils'
import { describeRunIf, itDoNotRunIf } from 'conditional-specs'
import Vuex from 'vuex'
describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
const windowSave = window
beforeEach(() => {
sinon.stub(console, 'error')
})
afterEach(() => {
window = windowSave // eslint-disable-line no-native-reassign
console.error.restore()
})
it('returns new VueWrapper with mounted Vue instance if no options are passed', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = mount(compiled)
expect(wrapper.vm).to.be.an('object')
})
it('returns new VueWrapper with mounted Vue instance when root is functional component', () => {
const FunctionalComponent = {
functional: true,
render (h) {
return h('div', {}, [
h('p', {
class: {
foo: true
}
}),
h('p')
])
},
name: 'common'
}
const wrapper = mount(FunctionalComponent)
expect(wrapper.findAll('p').length).to.equal(2)
})
it('returns new VueWrapper with mounted Vue instance with props, if passed as propsData', () => {
const prop1 = { test: 'TEST' }
const wrapper = mount(ComponentWithProps, { propsData: { prop1 }})
expect(wrapper.vm).to.be.an('object')
if (wrapper.vm.$props) {
expect(wrapper.vm.$props.prop1).to.equal(prop1)
} else {
expect(wrapper.vm.$options.propsData.prop1).to.equal(prop1)
}
})
itDoNotRunIf(vueVersion < 2.3,
'handles propsData for extended components', () => {
const prop1 = 'test'
const TestComponent = Vue.extend(ComponentWithProps)
const wrapper = mount(TestComponent, {
propsData: {
prop1
}
})
expect(wrapper.text()).to.contain(prop1)
})
it('handles uncompiled extended Vue component', () => {
const BaseComponent = {
template: '<div />'
}
const TestComponent = {
extends: BaseComponent
}
const wrapper = mount(TestComponent)
expect(wrapper.findAll('div').length).to.equal(1)
})
it('handles nested uncompiled extended Vue component', () => {
const BaseComponent = {
template: '<div />'
}
const TestComponentA = {
extends: BaseComponent
}
const TestComponentB = {
extends: TestComponentA
}
const TestComponentC = {
extends: TestComponentB
}
const TestComponentD = {
extends: TestComponentC
}
const wrapper = mount(TestComponentD)
expect(wrapper.findAll('div').length).to.equal(1)
})
it('does not use cached component', () => {
ComponentWithMixin.methods.someMethod = sinon.stub()
mount(ComponentWithMixin)
expect(ComponentWithMixin.methods.someMethod.callCount).to.equal(1)
ComponentWithMixin.methods.someMethod = sinon.stub()
mount(ComponentWithMixin)
expect(ComponentWithMixin.methods.someMethod.callCount).to.equal(1)
})
it('throws an error if window is undefined', () => {
if (
!(navigator.userAgent.includes && navigator.userAgent.includes('node.js'))
) {
console.log('window read only. skipping test ...')
return
}
const message =
'[vue-test-utils]: window is undefined, vue-test-utils needs to be run in a browser environment.\n You can run the tests in node using JSDOM'
window = undefined // eslint-disable-line no-native-reassign
expect(() => mount(compileToFunctions('<div />')))
.to.throw()
.with.property('message', message)
})
it('compiles inline templates', () => {
const wrapper = mount({
template: `<div>foo</div>`
})
expect(wrapper.vm).to.be.an('object')
expect(wrapper.html()).to.equal(`<div>foo</div>`)
})
itDoNotRunIf(
vueVersion < 2.3,
'overrides methods', () => {
const stub = sinon.stub()
const TestComponent = Vue.extend({
template: '<div />',
methods: {
callStub () {
stub()
}
}
})
mount(TestComponent, {
methods: {
callStub () {}
}
}).vm.callStub()
expect(stub).not.called
})
it.skip('overrides component prototype', () => {
const mountSpy = sinon.spy()
const destroySpy = sinon.spy()
const Component = Vue.extend({})
const { $mount: originalMount, $destroy: originalDestroy } = Component.prototype
Component.prototype.$mount = function (...args) {
originalMount.apply(this, args)
mountSpy()
return this
}
Component.prototype.$destroy = function () {
originalDestroy.apply(this)
destroySpy()
}
const wrapper = mount(Component)
expect(mountSpy).called
expect(destroySpy).not.called
wrapper.destroy()
expect(destroySpy).called
})
// Problems accessing options of twice extended components in Vue < 2.3
itDoNotRunIf(vueVersion < 2.3, 'compiles extended components', () => {
const TestComponent = Vue.component('test-component', {
template: '<div></div>'
})
const wrapper = mount(TestComponent)
expect(wrapper.html()).to.equal(`<div></div>`)
})
itDoNotRunIf(
vueVersion < 2.4, // auto resolve of default export added in 2.4
'handles components as dynamic imports', (done) => {
const TestComponent = {
template: '<div><async-component /></div>',
components: {
AsyncComponent: () => import('~resources/components/component.vue')
}
}
const wrapper = mount(TestComponent)
setTimeout(() => {
expect(wrapper.find(Component).exists()).to.equal(true)
done()
})
})
it('logs if component is extended', () => {
const msg =
`[vue-test-utils]: The child component <ChildComponent> has been modified to ensure ` +
`it is created with properties injected by Vue Test Utils. \n` +
`This is because the component was created with Vue.extend, ` +
`or uses the Vue Class Component decorator. \n` +
`Because the component has been modified, it is not possible ` +
`to find it with a component selector. To find the ` +
`component, you must stub it manually using the stubs mounting ` +
`option, or use a name or ref selector. \n` +
`You can hide this warning by setting the Vue Test Utils ` +
`config.logModifiedComponents option to false.`
const ChildComponent = Vue.extend({
template: '<span />'
})
const TestComponent = {
template: '<child-component />',
components: {
ChildComponent
}
}
mount(TestComponent)
expect(console.error).calledWith(msg)
})
it('deletes mounting options before passing options to component', () => {
const wrapper = mount(
{
render: h => h('div')
},
{
provide: {
prop: 'val'
},
attachToDocument: 'attachToDocument',
mocks: {
prop: 'val'
},
slots: {
prop: Component
},
localVue: createLocalVue(),
stubs: {
prop: 'val'
},
attrs: {
prop: 'val'
},
listeners: {
prop: 'val'
}
}
)
if (injectSupported) {
expect(typeof wrapper.vm.$options.provide).to.equal('object')
}
expect(wrapper.vm.$options.attachToDocument).to.equal(undefined)
expect(wrapper.vm.$options.mocks).to.equal(undefined)
expect(wrapper.vm.$options.slots).to.equal(undefined)
expect(wrapper.vm.$options.localVue).to.equal(undefined)
expect(wrapper.vm.$options.stubs).to.equal(undefined)
expect(wrapper.vm.$options.context).to.equal(undefined)
expect(wrapper.vm.$options.attrs).to.equal(undefined)
expect(wrapper.vm.$options.listeners).to.equal(undefined)
})
itDoNotRunIf(
vueVersion < 2.3,
'injects store correctly', () => {
const localVue = createLocalVue()
localVue.use(Vuex)
const store = new Vuex.Store()
const wrapper = mount(ComponentAsAClass, {
store,
localVue
})
wrapper.vm.getters
mount(
{
template: '<div>{{$store.getters}}</div>'
},
{ store, localVue }
)
})
it('propagates errors when they are thrown', () => {
const TestComponent = {
template: '<div></div>',
mounted: function () {
throw new Error('Error in mounted')
}
}
const fn = () => mount(TestComponent)
expect(fn).to.throw('Error in mounted')
})
it('propagates errors when they are thrown by a nested component', () => {
const childComponent = {
template: '<div></div>',
mounted: function () {
throw new Error('Error in mounted')
}
}
const rootComponent = {
render: function (h) {
return h('div', [h(childComponent)])
}
}
const fn = () => {
mount(rootComponent)
}
expect(fn).to.throw('Error in mounted')
})
itDoNotRunIf(vueVersion < 2.2, 'logs errors once after mount', done => {
Vue.config.errorHandler = null
const TestComponent = {
template: '<div/>',
updated: function () {
throw new Error('Error in updated')
}
}
const wrapper = mount(TestComponent, {
sync: false
})
wrapper.vm.$forceUpdate()
setTimeout(() => {
vueVersion > 2.1
? expect(console.error).calledTwice
: expect(console.error).calledOnce
done()
})
})
it('restores user error handler after mount', () => {
const existingErrorHandler = () => {}
Vue.config.errorHandler = existingErrorHandler
const TestComponent = {
template: '<div/>'
}
mount(TestComponent)
expect(Vue.config.errorHandler).to.equal(existingErrorHandler)
Vue.config.errorHandler = null
})
it('adds unused propsData as attributes', () => {
const wrapper = mount(
ComponentWithProps, {
attachToDocument: true,
propsData: {
prop1: 'prop1',
extra: 'attr'
},
attrs: {
height: '50px'
}
})
if (vueVersion > 2.3) {
expect(wrapper.vm.$attrs).to.eql({ height: '50px', extra: 'attr' })
}
expect(wrapper.html()).to.equal(`<div height="50px" extra="attr"><p class="prop-1">prop1</p> <p class="prop-2"></p></div>`)
})
it('overwrites the component options with the instance options', () => {
const Component = {
template: '<div>{{ foo() }}{{ bar() }}{{ baz() }}</div>',
methods: {
foo () {
return 'a'
},
bar () {
return 'b'
}
}
}
const options = {
methods: {
bar () {
return 'B'
},
baz () {
return 'C'
}
}
}
const wrapper = mount(Component, options)
expect(wrapper.text()).to.equal('aBC')
})
})