forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubs.spec.js
268 lines (243 loc) · 7.85 KB
/
stubs.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
import { mount, config } from '~vue-test-utils'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
import Component from '~resources/components/component.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import { createLocalVue } from '~vue-test-utils'
import Vue from 'vue'
describe('mount.stub', () => {
let info
let warn
let configStubsSave
beforeEach(() => {
info = sinon.stub(console, 'info')
warn = sinon.stub(console, 'error')
configStubsSave = config.stubs
config.stubs = {}
})
afterEach(() => {
info.restore()
warn.restore()
config.stubs = configStubsSave
})
it('accepts valid component stubs', () => {
const ComponentWithRender = { render: h => h('div') }
const ComponentWithoutRender = { template: '<div></div>' }
const ExtendedComponent = Vue.extend({ template: '<div></div>' })
mount(ComponentWithChild, {
stubs: {
ChildComponent: ComponentAsAClass,
ChildComponent2: ComponentWithRender,
ChildComponent3: ComponentWithoutRender,
ChildComponent4: ExtendedComponent
}
})
})
it('replaces component with template string ', () => {
const wrapper = mount(ComponentWithChild, {
stubs: {
ChildComponent: '<div class="stub"></div>'
}
})
expect(wrapper.findAll('.stub').length).to.equal(1)
expect(wrapper.findAll(Component).length).to.equal(1)
})
it('replaces component with a component', () => {
const wrapper = mount(ComponentWithChild, {
stubs: {
ChildComponent: {
render: h => h('div'),
mounted () {
console.info('stubbed')
}
}
}
})
expect(wrapper.findAll(Component).length).to.equal(1)
expect(info.calledWith('stubbed')).to.equal(true)
})
it('does not error if component to stub contains no components', () => {
mount(Component, {
stubs: {
doesNotExist: Component
}
})
})
it('does not modify component directly', () => {
const wrapper = mount(ComponentWithNestedChildren, {
stubs: {
ChildComponent: '<div />'
}
})
expect(wrapper.findAll(Component).length).to.equal(0)
const mountedWrapper = mount(ComponentWithNestedChildren)
expect(mountedWrapper.findAll(Component).length).to.equal(1)
})
it('stubs components on component if they do not already exist', () => {
const ComponentWithGlobalComponent = {
render: h => h('registered-component')
}
const wrapper = mount(ComponentWithGlobalComponent, {
stubs: {
'registered-component': Component
}
})
expect(wrapper.findAll(Component).length).to.equal(1)
})
it('stubs components with dummy when passed as an array', () => {
const ComponentWithGlobalComponent = {
render: h => h('registered-component')
}
mount(ComponentWithGlobalComponent, {
stubs: ['registered-component']
})
expect(warn.called).to.equal(false)
})
it('stubs components with dummy when passed a boolean', () => {
const ComponentWithGlobalComponent = {
render: h => h('registered-component')
}
mount(ComponentWithGlobalComponent, {
stubs: {
'registered-component': true
}
})
expect(warn.called).to.equal(false)
})
it('stubs components with dummy when passed as an array', () => {
const ComponentWithGlobalComponent = {
render: h => h('registered-component')
}
const invalidValues = [{}, [], 3]
const error = '[vue-test-utils]: each item in an options.stubs array must be a string'
invalidValues.forEach(invalidValue => {
const fn = () => mount(ComponentWithGlobalComponent, {
stubs: [invalidValue]
})
expect(fn).to.throw().with.property('message', error)
})
})
it('throws error if passed string in object when vue-template-compiler is undefined', () => {
const compilerSave = require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = undefined
delete require.cache[require.resolve('../../../../../src/mount')]
const mountFresh = require('../../../../../src/mount').default
const message = '[vue-test-utils]: vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined'
const fn = () => mountFresh(Component, {
stubs: {
ChildComponent: '<div />'
}
})
try {
expect(fn).to.throw().with.property('message', message)
} catch (err) {
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
throw err
}
require.cache[require.resolve('vue-template-compiler')].exports.compileToFunctions = compilerSave
})
it('does not stub component when set to false', () => {
const wrapper = mount(ComponentWithChild, {
stubs: {
ChildComponent: false
}})
expect(wrapper.find('span').contains('div')).to.equal(true)
})
it('combines with stubs from config', () => {
const localVue = createLocalVue()
config.stubs['time-component'] = '<p />'
const SpanComponent = {
render: h => h('span')
}
const TimeComponent = {
render: h => h('time')
}
localVue.component('span-component', SpanComponent)
localVue.component('time-component', TimeComponent)
const TestComponent = {
render: h => h('div', [
h('span-component'),
h('time-component')
])
}
const wrapper = mount(TestComponent, {
stubs: {
'span-component': '<p />'
},
localVue
})
expect(wrapper.findAll('p').length).to.equal(2)
})
it('prioritize mounting options over config', () => {
const localVue = createLocalVue()
config.stubs['time-component'] = '<p />'
const TimeComponent = {
render: h => h('time')
}
localVue.component('time-component', TimeComponent)
const TestComponent = {
render: h => h('div', [
h('time-component')
])
}
const wrapper = mount(TestComponent, {
stubs: {
'time-component': '<span />'
},
localVue
})
expect(wrapper.contains('span')).to.equal(true)
})
it('converts config to array if stubs is an array', () => {
const localVue = createLocalVue()
config.stubs['time-component'] = '<p />'
const TimeComponent = {
render: h => h('time')
}
localVue.component('time-component', TimeComponent)
const TestComponent = {
render: h => h('div', [
h('time-component')
])
}
const wrapper = mount(TestComponent, {
stubs: ['a-component'],
localVue
})
expect(wrapper.contains('time')).to.equal(false)
expect(wrapper.contains('p')).to.equal(false)
expect(wrapper.html()).to.equal('<div><!----></div>')
})
it('handles components without a render function', () => {
const TestComponent = {
template: `
<div>
<stub-component />
</div>
`,
components: {
stubComponent: { template: '<div />' }
}
}
const StubComponent = {
template: '<div>No render function</div>'
}
const wrapper = mount(TestComponent, {
stubs: {
'stub-component': StubComponent
}
})
expect(wrapper.text()).contains('No render function')
})
it('throws an error when passed an invalid value as stub', () => {
const error = '[vue-test-utils]: options.stub values must be passed a string or component'
const invalidValues = [1, null, [], {}, NaN]
invalidValues.forEach(invalidValue => {
const fn = () => mount(ComponentWithChild, {
stubs: {
ChildComponent: invalidValue
}})
expect(fn).to.throw().with.property('message', error)
})
})
})