Browse Source

test: test the right branches of code

Evan You 6 years ago
parent
commit
af585081b1

+ 2 - 2
packages/compiler-core/__tests__/transforms/vBind.spec.ts

@@ -87,7 +87,7 @@ describe('compiler: transform v-bind', () => {
 
 
   test('should error if no expression', () => {
   test('should error if no expression', () => {
     const onError = jest.fn()
     const onError = jest.fn()
-    parseWithVBind(`<div v-bind />`, { onError })
+    parseWithVBind(`<div v-bind:arg />`, { onError })
     expect(onError.mock.calls[0][0]).toMatchObject({
     expect(onError.mock.calls[0][0]).toMatchObject({
       code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
       code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
       loc: {
       loc: {
@@ -97,7 +97,7 @@ describe('compiler: transform v-bind', () => {
         },
         },
         end: {
         end: {
           line: 1,
           line: 1,
-          column: 12
+          column: 16
         }
         }
       }
       }
     })
     })

+ 9 - 3
packages/compiler-core/__tests__/transforms/vOn.spec.ts

@@ -103,9 +103,9 @@ describe('compiler: transform v-bind', () => {
     })
     })
   })
   })
 
 
-  test('should error if no expression', () => {
+  test('should error if no expression AND no modifier', () => {
     const onError = jest.fn()
     const onError = jest.fn()
-    parseWithVOn(`<div v-on />`, { onError })
+    parseWithVOn(`<div v-on:click />`, { onError })
     expect(onError.mock.calls[0][0]).toMatchObject({
     expect(onError.mock.calls[0][0]).toMatchObject({
       code: ErrorCodes.X_V_ON_NO_EXPRESSION,
       code: ErrorCodes.X_V_ON_NO_EXPRESSION,
       loc: {
       loc: {
@@ -115,11 +115,17 @@ describe('compiler: transform v-bind', () => {
         },
         },
         end: {
         end: {
           line: 1,
           line: 1,
-          column: 10
+          column: 16
         }
         }
       }
       }
     })
     })
   })
   })
 
 
+  test('should NOT error if no expression but has modifier', () => {
+    const onError = jest.fn()
+    parseWithVOn(`<div v-on:click.prevent />`, { onError })
+    expect(onError).not.toHaveBeenCalled()
+  })
+
   test.todo('.once modifier')
   test.todo('.once modifier')
 })
 })

+ 5 - 2
packages/compiler-core/src/transforms/vOn.ts

@@ -7,8 +7,11 @@ import { isSimpleIdentifier } from '../utils'
 // v-on without arg is handled directly in ./element.ts due to it affecting
 // v-on without arg is handled directly in ./element.ts due to it affecting
 // codegen for the entire props object. This transform here is only for v-on
 // codegen for the entire props object. This transform here is only for v-on
 // *with* args.
 // *with* args.
-export const transformOn: DirectiveTransform = ({ arg, exp, loc }, context) => {
-  if (!exp) {
+export const transformOn: DirectiveTransform = (
+  { arg, exp, loc, modifiers },
+  context
+) => {
+  if (!exp && !modifiers.length) {
     context.onError(createCompilerError(ErrorCodes.X_V_ON_NO_EXPRESSION, loc))
     context.onError(createCompilerError(ErrorCodes.X_V_ON_NO_EXPRESSION, loc))
   }
   }
   const { content, children, isStatic, loc: argLoc } = arg!
   const { content, children, isStatic, loc: argLoc } = arg!