forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.spec.js
249 lines (225 loc) · 7.71 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
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 { injectSupported, vueVersion, describeIf } from '~resources/utils'
describeIf(process.env.TEST_ENV !== 'node',
'mount', () => {
let consoleError
beforeEach(() => {
consoleError = sinon.stub(console, 'error')
})
afterEach(() => {
consoleError.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)
}
})
it('returns new VueWrapper with mounted Vue instance initialized with Vue.extend with props, if passed as propsData', () => {
const prop1 = { test: 'TEST' }
const wrapper = mount(Vue.extend(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)
}
})
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 windowSave = global.window
after(() => {
global.window = windowSave
})
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'
global.window = undefined
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>`)
})
it('logs if component is extended', () => {
const msg = '[vue-test-utils]: an extended child component ChildComponent has been modified to ensure it has the correct instance properties. This means it is not possible to find the component with a component selector. To find the component, you must stub it manually using the mocks mounting option.'
const ChildComponent = Vue.extend({
template: '<span />'
})
const TestComponent = {
template: '<child-component />',
components: {
ChildComponent
}
}
mount(TestComponent)
expect(consoleError).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) {
// provide is added by Vue, it's a function in Vue > 2.3
if (vueVersion > 2.3) {
expect(typeof wrapper.vm.$options.provide).to.equal('function')
} else {
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)
})
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')
})
it('overwrites the component options with the options other than the mounting options when the options for mount contain those', () => {
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')
})
})