-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-element.spec.js
245 lines (228 loc) · 7.43 KB
/
create-element.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
import Vue from 'vue'
import { createEmptyVNode } from 'core/vdom/vnode'
describe('create-element', () => {
it('render vnode with basic reserved tag using createElement', () => {
const vm = new Vue({
data: { msg: 'hello world' }
})
const h = vm.$createElement
const vnode = h('p', {})
expect(vnode.tag).toBe('p')
expect(vnode.data).toEqual({})
expect(vnode.children).toBeUndefined()
expect(vnode.text).toBeUndefined()
expect(vnode.elm).toBeUndefined()
expect(vnode.ns).toBeUndefined()
expect(vnode.context).toEqual(vm)
})
it('render vnode with component using createElement', () => {
const vm = new Vue({
data: { message: 'hello world' },
components: {
'my-component': {
props: ['msg']
}
}
})
const h = vm.$createElement
const vnode = h('my-component', { props: { msg: vm.message }})
expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
expect(vnode.children).toBeUndefined()
expect(vnode.text).toBeUndefined()
expect(vnode.elm).toBeUndefined()
expect(vnode.ns).toBeUndefined()
expect(vnode.context).toEqual(vm)
})
it('render vnode with custom tag using createElement', () => {
const vm = new Vue({
data: { msg: 'hello world' }
})
const h = vm.$createElement
const tag = 'custom-tag'
const vnode = h(tag, {})
expect(vnode.tag).toBe('custom-tag')
expect(vnode.data).toEqual({})
expect(vnode.children).toBeUndefined()
expect(vnode.text).toBeUndefined()
expect(vnode.elm).toBeUndefined()
expect(vnode.ns).toBeUndefined()
expect(vnode.context).toEqual(vm)
expect(vnode.componentOptions).toBeUndefined()
})
it('render empty vnode with falsy tag using createElement', () => {
const vm = new Vue({
data: { msg: 'hello world' }
})
const h = vm.$createElement
const vnode = h(null, {})
expect(vnode).toEqual(createEmptyVNode())
})
it('render vnode with not string tag using createElement', () => {
const vm = new Vue({
data: { msg: 'hello world' }
})
const h = vm.$createElement
const vnode = h(Vue.extend({ // Component class
props: ['msg']
}), { props: { msg: vm.message }})
expect(vnode.tag).toMatch(/vue-component-[0-9]+/)
expect(vnode.componentOptions.propsData).toEqual({ msg: vm.message })
expect(vnode.children).toBeUndefined()
expect(vnode.text).toBeUndefined()
expect(vnode.elm).toBeUndefined()
expect(vnode.ns).toBeUndefined()
expect(vnode.context).toEqual(vm)
})
it('render vnode with createElement with children', () => {
const vm = new Vue({})
const h = vm.$createElement
const vnode = h('p', void 0, [h('br'), 'hello world', h('br')])
expect(vnode.children[0].tag).toBe('br')
expect(vnode.children[1].text).toBe('hello world')
expect(vnode.children[2].tag).toBe('br')
})
it('render vnode with children, omitting data', () => {
const vm = new Vue({})
const h = vm.$createElement
const vnode = h('p', [h('br'), 'hello world', h('br')])
expect(vnode.children[0].tag).toBe('br')
expect(vnode.children[1].text).toBe('hello world')
expect(vnode.children[2].tag).toBe('br')
})
it('render vnode with children, including boolean and null type', () => {
const vm = new Vue({})
const h = vm.$createElement
const vnode = h('p', [h('br'), true, 123, h('br'), 'abc', null])
expect(vnode.children.length).toBe(4)
expect(vnode.children[0].tag).toBe('br')
expect(vnode.children[1].text).toBe('123')
expect(vnode.children[2].tag).toBe('br')
expect(vnode.children[3].text).toBe('abc')
})
it('render svg elements with correct namespace', () => {
const vm = new Vue({})
const h = vm.$createElement
const vnode = h('svg', [h('a', [h('foo', [h('bar')])])])
expect(vnode.ns).toBe('svg')
// should apply ns to children recursively
expect(vnode.children[0].ns).toBe('svg')
expect(vnode.children[0].children[0].ns).toBe('svg')
expect(vnode.children[0].children[0].children[0].ns).toBe('svg')
})
it('render MathML elements with correct namespace', () => {
const vm = new Vue({})
const h = vm.$createElement
const vnode = h('math', [h('matrix')])
expect(vnode.ns).toBe('math')
// should apply ns to children
expect(vnode.children[0].ns).toBe('math')
// although not explicitly listed, elements nested under <math>
// should not be treated as component
expect(vnode.children[0].componentOptions).toBeUndefined()
})
it('render svg foreignObject with correct namespace', () => {
const vm = new Vue({})
const h = vm.$createElement
const vnode = h('svg', [h('foreignObject', [h('p')])])
expect(vnode.ns).toBe('svg')
expect(vnode.children[0].ns).toBe('svg')
expect(vnode.children[0].children[0].ns).toBeUndefined()
})
// #6642
it('render svg foreignObject component with correct namespace', () => {
const vm = new Vue({
template: `
<svg>
<test></test>
</svg>
`,
components: {
test: {
template: `
<foreignObject>
<p xmlns="http://www.w3.org/1999/xhtml"></p>
</foreignObject>
`
}
}
}).$mount()
const testComp = vm.$children[0]
expect(testComp.$vnode.ns).toBe('svg')
expect(testComp._vnode.tag).toBe('foreignObject')
expect(testComp._vnode.ns).toBe('svg')
expect(testComp._vnode.children[0].tag).toBe('p')
expect(testComp._vnode.children[0].ns).toBeUndefined()
})
// #6506
it('render SVGAElement in a component correctly', () => {
const vm = new Vue({
template: `
<svg>
<test></test>
</svg>
`,
components: {
test: { render: h => h('a') }
}
}).$mount()
const testComp = vm.$children[0]
expect(testComp.$vnode.ns).toBe('svg')
expect(testComp._vnode.tag).toBe('a')
expect(testComp._vnode.ns).toBe('svg')
})
it('warn observed data objects', () => {
new Vue({
data: {
data: {}
},
render (h) {
return h('div', this.data)
}
}).$mount()
expect('Avoid using observed data object as vnode data').toHaveBeenWarned()
})
it('warn non-primitive key', () => {
new Vue({
render (h) {
return h('div', { key: {}})
}
}).$mount()
expect('Avoid using non-primitive value as key').toHaveBeenWarned()
})
it('doesn\'t warn boolean key', () => {
new Vue({
render (h) {
return h('div', { key: true })
}
}).$mount()
expect('Avoid using non-primitive value as key').not.toHaveBeenWarned()
})
it('nested child elements should be updated correctly', done => {
const vm = new Vue({
data: { n: 1 },
render (h) {
const list = []
for (let i = 0; i < this.n; i++) {
list.push(h('span', i))
}
const input = h('input', {
attrs: {
value: 'a',
type: 'text'
}
})
return h('div', [[...list, input]])
}
}).$mount()
expect(vm.$el.innerHTML).toContain('<span>0</span><input')
const el = vm.$el.querySelector('input')
el.value = 'b'
vm.n++
waitForUpdate(() => {
expect(vm.$el.innerHTML).toContain('<span>0</span><span>1</span><input')
expect(vm.$el.querySelector('input')).toBe(el)
expect(vm.$el.querySelector('input').value).toBe('b')
}).then(done)
})
})