-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest.js
100 lines (91 loc) · 2.14 KB
/
test.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
const transpile = require('./index')
const Vue = require('vue')
const { compile } = require('vue-template-compiler')
const toFunction = code => {
code = transpile(`function render(){${code}}`)
code = code.replace(/function render\(\)\{|\}$/g, '')
return new Function(code)
}
const compileAsFunctions = template => {
const { render, staticRenderFns } = compile(template)
return {
render: toFunction(render),
staticRenderFns: staticRenderFns.map(toFunction)
}
}
test('should work', () => {
const vm = new Vue({
...compileAsFunctions(`
<div>
<div>{{ foo }}</div>
<div v-for="{ name } in items">{{ name }}</div>
<div v-bind="{ ...a, ...b }"/>
</div>
`),
data: {
foo: 'hello',
items: [
{ name: 'foo' },
{ name: 'bar' }
],
a: { id: 'foo' },
b: { class: 'bar' }
}
}).$mount()
expect(vm.$el.innerHTML).toMatch(
`<div>hello</div> ` +
`<div>foo</div><div>bar</div> ` +
`<div id="foo" class="bar"></div>`
)
})
test('arg spread', () => {
const res = compile(`
<button @click="(...args) => { store.foo(...args) }">Go</button>
`)
const code = transpile(`function render() {${res.render}}`)
expect(code).toMatch(`_vm.store.foo.apply(_vm.store, args)`)
})
test('rest spread in scope position', () => {
const vm = new Vue({
...compileAsFunctions(`
<foo v-slot="{ foo, ...rest }">{{ rest }}</foo>
`),
components: {
foo: {
render(h) {
return h('div', this.$scopedSlots.default({
foo: 1,
bar: 2,
baz: 3
}))
}
}
}
}).$mount()
expect(vm.$el.innerHTML).toMatch(
JSON.stringify({ bar: 2, baz: 3 }, null, 2)
)
})
test('trailing function comma', () => {
const spy = jest.fn()
const vm = new Vue({
...compileAsFunctions(`
<button @click="spy(1,)" />
`),
methods: {
spy
}
}).$mount()
vm.$el.click()
expect(spy).toHaveBeenCalled()
})
test('v-model code', () => {
const vm = new Vue({
...compileAsFunctions(`
<input v-model="text" />
`),
data: {
text: 'foo'
}
}).$mount()
})