@@ -6,16 +6,14 @@ import {
6
6
CompilerOptions ,
7
7
ErrorCodes ,
8
8
NodeTypes ,
9
- CallExpression
9
+ CallExpression ,
10
+ PlainElementCodegenNode
10
11
} from '../../src'
11
12
import { transformOn } from '../../src/transforms/vOn'
12
13
import { transformElement } from '../../src/transforms/transformElement'
13
14
import { transformExpression } from '../../src/transforms/transformExpression'
14
15
15
- function parseWithVOn (
16
- template : string ,
17
- options : CompilerOptions = { }
18
- ) : ElementNode {
16
+ function parseWithVOn ( template : string , options : CompilerOptions = { } ) {
19
17
const ast = parse ( template )
20
18
transform ( ast , {
21
19
nodeTransforms : [ transformExpression , transformElement ] ,
@@ -24,12 +22,15 @@ function parseWithVOn(
24
22
} ,
25
23
...options
26
24
} )
27
- return ast . children [ 0 ] as ElementNode
25
+ return {
26
+ root : ast ,
27
+ node : ast . children [ 0 ] as ElementNode
28
+ }
28
29
}
29
30
30
31
describe ( 'compiler: transform v-on' , ( ) => {
31
32
test ( 'basic' , ( ) => {
32
- const node = parseWithVOn ( `<div v-on:click="onClick"/>` )
33
+ const { node } = parseWithVOn ( `<div v-on:click="onClick"/>` )
33
34
const props = ( node . codegenNode as CallExpression )
34
35
. arguments [ 1 ] as ObjectExpression
35
36
expect ( props . properties [ 0 ] ) . toMatchObject ( {
@@ -65,7 +66,7 @@ describe('compiler: transform v-on', () => {
65
66
} )
66
67
67
68
test ( 'dynamic arg' , ( ) => {
68
- const node = parseWithVOn ( `<div v-on:[event]="handler"/>` )
69
+ const { node } = parseWithVOn ( `<div v-on:[event]="handler"/>` )
69
70
const props = ( node . codegenNode as CallExpression )
70
71
. arguments [ 1 ] as ObjectExpression
71
72
expect ( props . properties [ 0 ] ) . toMatchObject ( {
@@ -82,7 +83,7 @@ describe('compiler: transform v-on', () => {
82
83
} )
83
84
84
85
test ( 'dynamic arg with prefixing' , ( ) => {
85
- const node = parseWithVOn ( `<div v-on:[event]="handler"/>` , {
86
+ const { node } = parseWithVOn ( `<div v-on:[event]="handler"/>` , {
86
87
prefixIdentifiers : true
87
88
} )
88
89
const props = ( node . codegenNode as CallExpression )
@@ -101,7 +102,7 @@ describe('compiler: transform v-on', () => {
101
102
} )
102
103
103
104
test ( 'dynamic arg with complex exp prefixing' , ( ) => {
104
- const node = parseWithVOn ( `<div v-on:[event(foo)]="handler"/>` , {
105
+ const { node } = parseWithVOn ( `<div v-on:[event(foo)]="handler"/>` , {
105
106
prefixIdentifiers : true
106
107
} )
107
108
const props = ( node . codegenNode as CallExpression )
@@ -127,7 +128,7 @@ describe('compiler: transform v-on', () => {
127
128
} )
128
129
129
130
test ( 'should wrap as function if expression is inline statement' , ( ) => {
130
- const node = parseWithVOn ( `<div @click="i++"/>` )
131
+ const { node } = parseWithVOn ( `<div @click="i++"/>` )
131
132
const props = ( node . codegenNode as CallExpression )
132
133
. arguments [ 1 ] as ObjectExpression
133
134
expect ( props . properties [ 0 ] ) . toMatchObject ( {
@@ -140,7 +141,7 @@ describe('compiler: transform v-on', () => {
140
141
} )
141
142
142
143
test ( 'inline statement w/ prefixIdentifiers: true' , ( ) => {
143
- const node = parseWithVOn ( `<div @click="foo($event)"/>` , {
144
+ const { node } = parseWithVOn ( `<div @click="foo($event)"/>` , {
144
145
prefixIdentifiers : true
145
146
} )
146
147
const props = ( node . codegenNode as CallExpression )
@@ -163,7 +164,7 @@ describe('compiler: transform v-on', () => {
163
164
} )
164
165
165
166
test ( 'should NOT wrap as function if expression is already function expression' , ( ) => {
166
- const node = parseWithVOn ( `<div @click="$event => foo($event)"/>` )
167
+ const { node } = parseWithVOn ( `<div @click="$event => foo($event)"/>` )
167
168
const props = ( node . codegenNode as CallExpression )
168
169
. arguments [ 1 ] as ObjectExpression
169
170
expect ( props . properties [ 0 ] ) . toMatchObject ( {
@@ -176,7 +177,7 @@ describe('compiler: transform v-on', () => {
176
177
} )
177
178
178
179
test ( 'should NOT wrap as function if expression is complex member expression' , ( ) => {
179
- const node = parseWithVOn ( `<div @click="a['b' + c]"/>` )
180
+ const { node } = parseWithVOn ( `<div @click="a['b' + c]"/>` )
180
181
const props = ( node . codegenNode as CallExpression )
181
182
. arguments [ 1 ] as ObjectExpression
182
183
expect ( props . properties [ 0 ] ) . toMatchObject ( {
@@ -189,7 +190,7 @@ describe('compiler: transform v-on', () => {
189
190
} )
190
191
191
192
test ( 'complex member expression w/ prefixIdentifiers: true' , ( ) => {
192
- const node = parseWithVOn ( `<div @click="a['b' + c]"/>` , {
193
+ const { node } = parseWithVOn ( `<div @click="a['b' + c]"/>` , {
193
194
prefixIdentifiers : true
194
195
} )
195
196
const props = ( node . codegenNode as CallExpression )
@@ -204,7 +205,7 @@ describe('compiler: transform v-on', () => {
204
205
} )
205
206
206
207
test ( 'function expression w/ prefixIdentifiers: true' , ( ) => {
207
- const node = parseWithVOn ( `<div @click="e => foo(e)"/>` , {
208
+ const { node } = parseWithVOn ( `<div @click="e => foo(e)"/>` , {
208
209
prefixIdentifiers : true
209
210
} )
210
211
const props = ( node . codegenNode as CallExpression )
@@ -249,5 +250,81 @@ describe('compiler: transform v-on', () => {
249
250
expect ( onError ) . not . toHaveBeenCalled ( )
250
251
} )
251
252
252
- test . todo ( '.once modifier' )
253
+ describe ( 'cacheHandler' , ( ) => {
254
+ test ( 'empty handler' , ( ) => {
255
+ const { root, node } = parseWithVOn ( `<div v-on:click.prevent />` , {
256
+ prefixIdentifiers : true ,
257
+ cacheHandlers : true
258
+ } )
259
+ expect ( root . cached ) . toBe ( 1 )
260
+ const args = ( node . codegenNode as PlainElementCodegenNode ) . arguments
261
+ // should not treat cached handler as dynamicProp, so no flags
262
+ expect ( args . length ) . toBe ( 2 )
263
+ expect ( ( args [ 1 ] as ObjectExpression ) . properties [ 0 ] . value ) . toMatchObject ( {
264
+ type : NodeTypes . JS_CACHE_EXPRESSION ,
265
+ index : 1 ,
266
+ value : {
267
+ type : NodeTypes . SIMPLE_EXPRESSION ,
268
+ content : `() => {}`
269
+ }
270
+ } )
271
+ } )
272
+
273
+ test ( 'member expression handler' , ( ) => {
274
+ const { root, node } = parseWithVOn ( `<div v-on:click="foo" />` , {
275
+ prefixIdentifiers : true ,
276
+ cacheHandlers : true
277
+ } )
278
+ expect ( root . cached ) . toBe ( 1 )
279
+ const args = ( node . codegenNode as PlainElementCodegenNode ) . arguments
280
+ // should not treat cached handler as dynamicProp, so no flags
281
+ expect ( args . length ) . toBe ( 2 )
282
+ expect ( ( args [ 1 ] as ObjectExpression ) . properties [ 0 ] . value ) . toMatchObject ( {
283
+ type : NodeTypes . JS_CACHE_EXPRESSION ,
284
+ index : 1 ,
285
+ value : {
286
+ type : NodeTypes . COMPOUND_EXPRESSION ,
287
+ children : [ `$event => (` , { content : `_ctx.foo($event)` } , `)` ]
288
+ }
289
+ } )
290
+ } )
291
+
292
+ test ( 'inline function expression handler' , ( ) => {
293
+ const { root, node } = parseWithVOn ( `<div v-on:click="() => foo()" />` , {
294
+ prefixIdentifiers : true ,
295
+ cacheHandlers : true
296
+ } )
297
+ expect ( root . cached ) . toBe ( 1 )
298
+ const args = ( node . codegenNode as PlainElementCodegenNode ) . arguments
299
+ // should not treat cached handler as dynamicProp, so no flags
300
+ expect ( args . length ) . toBe ( 2 )
301
+ expect ( ( args [ 1 ] as ObjectExpression ) . properties [ 0 ] . value ) . toMatchObject ( {
302
+ type : NodeTypes . JS_CACHE_EXPRESSION ,
303
+ index : 1 ,
304
+ value : {
305
+ type : NodeTypes . COMPOUND_EXPRESSION ,
306
+ children : [ `() => ` , { content : `_ctx.foo` } , `()` ]
307
+ }
308
+ } )
309
+ } )
310
+
311
+ test ( 'inline statement handler' , ( ) => {
312
+ const { root, node } = parseWithVOn ( `<div v-on:click="foo++" />` , {
313
+ prefixIdentifiers : true ,
314
+ cacheHandlers : true
315
+ } )
316
+ expect ( root . cached ) . toBe ( 1 )
317
+ const args = ( node . codegenNode as PlainElementCodegenNode ) . arguments
318
+ // should not treat cached handler as dynamicProp, so no flags
319
+ expect ( args . length ) . toBe ( 2 )
320
+ expect ( ( args [ 1 ] as ObjectExpression ) . properties [ 0 ] . value ) . toMatchObject ( {
321
+ type : NodeTypes . JS_CACHE_EXPRESSION ,
322
+ index : 1 ,
323
+ value : {
324
+ type : NodeTypes . COMPOUND_EXPRESSION ,
325
+ children : [ `$event => (` , { content : `_ctx.foo` } , `++` , `)` ]
326
+ }
327
+ } )
328
+ } )
329
+ } )
253
330
} )
0 commit comments