-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional.spec.js
340 lines (316 loc) · 8.66 KB
/
functional.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
import Vue from 'vue'
import { createEmptyVNode } from 'core/vdom/vnode'
describe('Options functional', () => {
it('should work', done => {
const vm = new Vue({
data: { test: 'foo' },
template: '<div><wrap :msg="test">bar</wrap></div>',
components: {
wrap: {
functional: true,
props: ['msg'],
render (h, { props, children }) {
return h('div', null, [props.msg, ' '].concat(children))
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>foo bar</div>')
vm.test = 'qux'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<div>qux bar</div>')
}).then(done)
})
it('should expose all props when not declared', done => {
const fn = {
functional: true,
render (h, { props }) {
return h('div', `${props.msg} ${props.kebabMsg}`)
}
}
const vm = new Vue({
data: { test: 'foo' },
render (h) {
return h('div', [
h(fn, {
props: { msg: this.test },
attrs: { 'kebab-msg': 'bar' }
})
])
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>foo bar</div>')
vm.test = 'qux'
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<div>qux bar</div>')
}).then(done)
})
it('should expose data.on as listeners', () => {
const foo = jasmine.createSpy('foo')
const bar = jasmine.createSpy('bar')
const vm = new Vue({
template: '<div><wrap @click="foo" @test="bar"/></div>',
methods: { foo, bar },
components: {
wrap: {
functional: true,
render (h, { listeners }) {
return h('div', {
on: {
click: [listeners.click, () => listeners.test('bar')]
}
})
}
}
}
}).$mount()
triggerEvent(vm.$el.children[0], 'click')
expect(foo).toHaveBeenCalled()
expect(foo.calls.argsFor(0)[0].type).toBe('click') // should have click event
triggerEvent(vm.$el.children[0], 'mousedown')
expect(bar).toHaveBeenCalledWith('bar')
})
it('should support returning more than one root node', () => {
const vm = new Vue({
template: `<div><test></test></div>`,
components: {
test: {
functional: true,
render (h) {
return [h('span', 'foo'), h('span', 'bar')]
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>foo</span><span>bar</span>')
})
it('should support slots', () => {
const vm = new Vue({
data: { test: 'foo' },
template: '<div><wrap><div slot="a">foo</div><div slot="b">bar</div></wrap></div>',
components: {
wrap: {
functional: true,
props: ['msg'],
render (h, { slots }) {
slots = slots()
return h('div', null, [slots.b, slots.a])
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<div><div>bar</div><div>foo</div></div>')
})
it('should let vnode raw data pass through', done => {
const onValid = jasmine.createSpy('valid')
const vm = new Vue({
data: { msg: 'hello' },
template: `<div>
<validate field="field1" @valid="onValid">
<input type="text" v-model="msg">
</validate>
</div>`,
components: {
validate: {
functional: true,
props: ['field'],
render (h, { props, children, data: { on }}) {
props.child = children[0]
return h('validate-control', { props, on })
}
},
'validate-control': {
props: ['field', 'child'],
render () {
return this.child
},
mounted () {
this.$el.addEventListener('input', this.onInput)
},
destroyed () {
this.$el.removeEventListener('input', this.onInput)
},
methods: {
onInput (e) {
const value = e.target.value
if (this.validate(value)) {
this.$emit('valid', this)
}
},
// something validation logic here
validate (val) {
return val.length > 0
}
}
}
},
methods: { onValid }
}).$mount()
document.body.appendChild(vm.$el)
const input = vm.$el.querySelector('input')
expect(onValid).not.toHaveBeenCalled()
waitForUpdate(() => {
input.value = 'foo'
triggerEvent(input, 'input')
}).then(() => {
expect(onValid).toHaveBeenCalled()
}).then(() => {
document.body.removeChild(vm.$el)
vm.$destroy()
}).then(done)
})
it('create empty vnode when render return null', () => {
const child = {
functional: true,
render () {
return null
}
}
const vm = new Vue({
components: {
child
}
})
const h = vm.$createElement
const vnode = h('child')
expect(vnode).toEqual(createEmptyVNode())
})
// #7282
it('should normalize top-level arrays', () => {
const Foo = {
functional: true,
render (h) {
return [h('span', 'hi'), null]
}
}
const vm = new Vue({
template: `<div><foo/></div>`,
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>hi</span>')
})
it('should work when used as named slot and returning array', () => {
const Foo = {
template: `<div><slot name="test"/></div>`
}
const Bar = {
functional: true,
render: h => ([
h('div', 'one'),
h('div', 'two'),
h(Baz)
])
}
const Baz = {
functional: true,
render: h => h('div', 'three')
}
const vm = new Vue({
template: `<foo><bar slot="test"/></foo>`,
components: { Foo, Bar }
}).$mount()
expect(vm.$el.innerHTML).toBe('<div>one</div><div>two</div><div>three</div>')
})
it('should apply namespace when returning arrays', () => {
const Child = {
functional: true,
render: h => ([h('foo'), h('bar')])
}
const vm = new Vue({
template: `<svg><child/></svg>`,
components: { Child }
}).$mount()
expect(vm.$el.childNodes[0].namespaceURI).toContain('svg')
expect(vm.$el.childNodes[1].namespaceURI).toContain('svg')
})
it('should work with render fns compiled from template', done => {
// code generated via vue-template-es2015-compiler
var render = function (_h, _vm) {
var _c = _vm._c
return _c(
'div',
[
_c('h2', { staticClass: 'red' }, [_vm._v(_vm._s(_vm.props.msg))]),
_vm._t('default'),
_vm._t('slot2'),
_vm._t('scoped', null, { msg: _vm.props.msg }),
_vm._m(0),
_c('div', { staticClass: 'clickable', on: { click: _vm.parent.fn }}, [
_vm._v('click me')
])
],
2
)
}
var staticRenderFns = [
function (_h, _vm) {
var _c = _vm._c
return _c('div', [_vm._v('Some '), _c('span', [_vm._v('text')])])
}
]
const child = {
functional: true,
_compiled: true,
render,
staticRenderFns
}
const parent = new Vue({
components: {
child
},
data: {
msg: 'hello'
},
template: `
<div>
<child :msg="msg">
<span>{{ msg }}</span>
<div slot="slot2">Second slot</div>
<template slot="scoped" slot-scope="scope">{{ scope.msg }}</template>
</child>
</div>
`,
methods: {
fn () {
this.msg = 'bye'
}
}
}).$mount()
function assertMarkup () {
expect(parent.$el.innerHTML).toBe(
`<div>` +
`<h2 class="red">${parent.msg}</h2>` +
`<span>${parent.msg}</span> ` +
`<div>Second slot</div>` +
parent.msg +
// static
`<div>Some <span>text</span></div>` +
`<div class="clickable">click me</div>` +
`</div>`
)
}
assertMarkup()
triggerEvent(parent.$el.querySelector('.clickable'), 'click')
waitForUpdate(assertMarkup).then(done)
})
// #8468
it('should normalize nested arrays when use functional components with v-for', () => {
const Foo = {
functional: true,
props: {
name: {}
},
render (h, context) {
return [h('span', 'hi'), h('span', context.props.name)]
}
}
const vm = new Vue({
template: `<div><foo v-for="name in names" :name="name" /></div>`,
data: {
names: ['foo', 'bar']
},
components: { Foo }
}).$mount()
expect(vm.$el.innerHTML).toBe('<span>hi</span><span>foo</span><span>hi</span><span>bar</span>')
})
})