Skip to content

Commit 1331c2a

Browse files
committed
test: add test case for scope rest spread and trailing function comma
1 parent 57e00d5 commit 1331c2a

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

test.js

+57-15
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@ const transpile = require('./index')
22
const Vue = require('vue')
33
const { compile } = require('vue-template-compiler')
44

5-
test('should work', () => {
6-
const res = compile(`
7-
<div>
8-
<div>{{ foo }}</div>
9-
<div v-for="{ name } in items">{{ name }}</div>
10-
<div v-bind="{ ...a, ...b }"/>
11-
</div>
12-
`)
5+
const toFunction = code => {
6+
code = transpile(`function render(){${code}}`)
7+
code = code.replace(/function render\(\)\{|\}$/g, '')
8+
return new Function(code)
9+
}
1310

14-
const toFunction = code => {
15-
code = transpile(`function render(){${code}}`)
16-
code = code.replace(/^function render\(\)\{|\}$/g, '')
17-
return new Function(code)
11+
const compileAsFunctions = template => {
12+
const { render, staticRenderFns } = compile(template)
13+
return {
14+
render: toFunction(render),
15+
staticRenderFns: staticRenderFns.map(toFunction)
1816
}
17+
}
1918

19+
test('should work', () => {
2020
const vm = new Vue({
21+
...compileAsFunctions(`
22+
<div>
23+
<div>{{ foo }}</div>
24+
<div v-for="{ name } in items">{{ name }}</div>
25+
<div v-bind="{ ...a, ...b }"/>
26+
</div>
27+
`),
2128
data: {
2229
foo: 'hello',
2330
items: [
@@ -26,9 +33,7 @@ test('should work', () => {
2633
],
2734
a: { id: 'foo' },
2835
b: { class: 'bar' }
29-
},
30-
render: toFunction(res.render),
31-
staticRenderFns: res.staticRenderFns.map(toFunction)
36+
}
3237
}).$mount()
3338

3439
expect(vm.$el.innerHTML).toMatch(
@@ -45,3 +50,40 @@ test('arg spread', () => {
4550
const code = transpile(`function render() {${res.render}}`)
4651
expect(code).toMatch(`_vm.store.foo.apply(_vm.store, args)`)
4752
})
53+
54+
test('rest spread in scope position', () => {
55+
const vm = new Vue({
56+
...compileAsFunctions(`
57+
<foo v-slot="{ foo, ...rest }">{{ rest }}</foo>
58+
`),
59+
components: {
60+
foo: {
61+
render(h) {
62+
return h('div', this.$scopedSlots.default({
63+
foo: 1,
64+
bar: 2,
65+
baz: 3
66+
}))
67+
}
68+
}
69+
}
70+
}).$mount()
71+
72+
expect(vm.$el.innerHTML).toMatch(
73+
JSON.stringify({ bar: 2, baz: 3 }, null, 2)
74+
)
75+
})
76+
77+
test('trailing function comma', () => {
78+
const spy = jest.fn()
79+
const vm = new Vue({
80+
...compileAsFunctions(`
81+
<button @click="spy(1,)" />
82+
`),
83+
methods: {
84+
spy
85+
}
86+
}).$mount()
87+
vm.$el.click()
88+
expect(spy).toHaveBeenCalled()
89+
})

0 commit comments

Comments
 (0)