@@ -2,22 +2,29 @@ const transpile = require('./index')
2
2
const Vue = require ( 'vue' )
3
3
const { compile } = require ( 'vue-template-compiler' )
4
4
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 ( / f u n c t i o n r e n d e r \( \) \{ | \} $ / g, '' )
8
+ return new Function ( code )
9
+ }
13
10
14
- const toFunction = code => {
15
- code = transpile ( `function render(){${ code } }` )
16
- code = code . replace ( / ^ f u n c t i o n r e n d e r \( \) \{ | \} $ / 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 )
18
16
}
17
+ }
19
18
19
+ test ( 'should work' , ( ) => {
20
20
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
+ ` ) ,
21
28
data : {
22
29
foo : 'hello' ,
23
30
items : [
@@ -26,9 +33,7 @@ test('should work', () => {
26
33
] ,
27
34
a : { id : 'foo' } ,
28
35
b : { class : 'bar' }
29
- } ,
30
- render : toFunction ( res . render ) ,
31
- staticRenderFns : res . staticRenderFns . map ( toFunction )
36
+ }
32
37
} ) . $mount ( )
33
38
34
39
expect ( vm . $el . innerHTML ) . toMatch (
@@ -45,3 +50,40 @@ test('arg spread', () => {
45
50
const code = transpile ( `function render() {${ res . render } }` )
46
51
expect ( code ) . toMatch ( `_vm.store.foo.apply(_vm.store, args)` )
47
52
} )
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