forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshallow.spec.js
207 lines (182 loc) · 6.55 KB
/
shallow.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
import { compileToFunctions } from 'vue-template-compiler'
import Vue from 'vue'
import { mount, shallow } from '~vue/test-utils'
import Component from '~resources/components/component.vue'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue'
import ComponentWithLifecycleHooks from '~resources/components/component-with-lifecycle-hooks.vue'
import ComponentWithoutName from '~resources/components/component-without-name.vue'
import ComponentAsAClassWithChild from '~resources/components/component-as-a-class-with-child.vue'
import RecursiveComponent from '~resources/components/recursive-component.vue'
import { vueVersion } from '~resources/utils'
import { describeRunIf } from 'conditional-specs'
describeRunIf(process.env.TEST_ENV !== 'node',
'shallowMount', () => {
let info
beforeEach(() => {
info = sinon.stub(console, 'info')
})
afterEach(() => {
info.restore()
})
it('returns new VueWrapper of Vue localVue if no options are passed', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = shallow(compiled)
expect(wrapper.isVueComponent).to.equal(true)
expect(wrapper.vm).to.be.an('object')
})
it('returns new VueWrapper of Vue localVue with all children stubbed', () => {
const wrapper = shallow(ComponentWithNestedChildren)
expect(wrapper.isVueComponent).to.equal(true)
expect(wrapper.findAll(Component).length).to.equal(0)
expect(wrapper.findAll(ComponentWithChild).length).to.equal(1)
})
it('returns new VueWrapper of Vue localVue with all children stubbed', () => {
const wrapper = shallow(ComponentWithNestedChildren)
expect(wrapper.isVueComponent).to.equal(true)
expect(wrapper.findAll(Component).length).to.equal(0)
expect(wrapper.findAll(ComponentWithChild).length).to.equal(1)
})
it('does not modify component directly', () => {
const wrapper = shallow(ComponentWithNestedChildren)
expect(wrapper.findAll(Component).length).to.equal(0)
const mountedWrapper = mount(ComponentWithNestedChildren)
expect(mountedWrapper.findAll(Component).length).to.equal(1)
})
it('stubs globally registered components when options.localVue is provided', () => {
const localVue = Vue.extend()
localVue.component('registered-component', ComponentWithLifecycleHooks)
const Component = {
render: h => h('registered-component')
}
shallow(Component, { localVue })
mount(Component, { localVue })
expect(info.callCount).to.equal(4)
})
it('stubs globally registered components', () => {
Vue.component('registered-component', ComponentWithLifecycleHooks)
const Component = {
render: h => h('registered-component')
}
shallow(Component)
mount(Component)
expect(info.callCount).to.equal(4)
})
it('does not call stubbed children lifecycle hooks', () => {
shallow(ComponentWithNestedChildren)
expect(info.called).to.equal(false)
})
it('stubs extended components', () => {
const ComponentWithPTag = {
template: `<p></p>`
}
const BaseComponent = {
template: `
<div>
<component-with-p-tag />
</div>
`,
components: {
ComponentWithPTag
}
}
const TestComponent = {
extends: BaseComponent
}
const wrapper = shallow(TestComponent)
expect(wrapper.find(ComponentWithPTag).exists()).to.equal(true)
expect(wrapper.find('p').exists()).to.equal(false)
})
it('stubs nested extended components', () => {
const ComponentWithPTag = {
template: `<p></p>`
}
const BaseComponent = {
template: `
<div>
<component-with-p-tag />
</div>
`,
components: {
ComponentWithPTag
}
}
const ExtendedBaseComponent = {
extends: BaseComponent
}
const TestComponent = {
extends: ExtendedBaseComponent
}
const wrapper = shallow(TestComponent)
expect(wrapper.find(ComponentWithPTag).exists()).to.equal(true)
expect(wrapper.find('p').exists()).to.equal(false)
})
it('stubs Vue class component children', () => {
if (vueVersion < 2.3) {
return
}
const wrapper = shallow(ComponentAsAClassWithChild)
expect(wrapper.find(Component).exists()).to.equal(true)
expect(wrapper.findAll('div').length).to.equal(1)
})
it('works correctly with find, contains, findAll, and is on unnamed components', () => {
const TestComponent = {
template: `
<div>
<component-without-name />
</div>
`,
components: {
ComponentWithoutName
}
}
const wrapper = shallow(TestComponent)
expect(wrapper.contains(ComponentWithoutName)).to.equal(true)
expect(wrapper.find(ComponentWithoutName).exists()).to.equal(true)
expect(wrapper.findAll(ComponentWithoutName).length).to.equal(1)
})
it('works correctly with find, contains, findAll, and is on named components', () => {
const TestComponent = {
template: `
<div>
<a-component />
</div>
`,
components: {
AComponent: Component
}
}
const wrapper = shallow(TestComponent)
expect(wrapper.contains(Component)).to.equal(true)
expect(wrapper.find(Component).exists()).to.equal(true)
expect(wrapper.findAll(Component).length).to.equal(1)
})
it('works correctly with find on recursive components', () => {
// this is for a bug that I've been unable to replicate.
// Sometimes components mutate their components, in this line—
RecursiveComponent.components = {
RecursiveComponent: { render: h => h('div') }
}
expect(shallow(RecursiveComponent, {
propsData: {
items: ['', '']
}
}).findAll(RecursiveComponent).length).to.equal(2)
RecursiveComponent.components = {
'recursive-component': { render: h => h('div') }
}
expect(shallow(RecursiveComponent, {
propsData: {
items: ['', '']
}
}).findAll(RecursiveComponent).length).to.equal(2)
})
it('throws an error when the component fails to mount', () => {
expect(() => shallow({
template: '<div></div>',
mounted: function () {
throw (new Error('Error'))
}
})).to.throw()
})
})