vOn.mp.spec.ts 1.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { ElementNode } from '@vue/compiler-core'
import { compile } from '../src'
fxy060608's avatar
fxy060608 已提交
3
import { MPErrorCodes } from '../src/errors'
fxy060608's avatar
fxy060608 已提交
4
import { CompilerOptions } from '../src/options'
fxy060608's avatar
fxy060608 已提交
5
import { assert } from './testUtils'
fxy060608's avatar
fxy060608 已提交
6 7

function parseWithVOn(template: string, options: CompilerOptions = {}) {
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13
  const { ast } = compile(template, {
    generatorOpts: {
      concise: true,
    },
    ...options,
  })
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20
  return {
    root: ast,
    node: ast.children[0] as ElementNode,
  }
}

describe('compiler(mp): transform v-on', () => {
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29 30 31 32
  test('lazy element', () => {
    assert(
      `<editor/>`,
      `<editor/>`,
      `(_ctx, _cache) => {
  return {}
}`
    )
    assert(
      `<editor @ready="ready"/>`,
      `<block wx:if="{{r0}}"><editor bindready="{{a}}"/></block>`,
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
33
  return { a: _o(_ctx.ready) }
fxy060608's avatar
fxy060608 已提交
34 35 36
}`
    )
  })
fxy060608's avatar
fxy060608 已提交
37 38 39 40
  test('should error if dynamic event', () => {
    const onError = jest.fn()
    parseWithVOn(`<div v-on:[event]="onClick" />`, { onError })
    expect(onError.mock.calls[0][0]).toMatchObject({
fxy060608's avatar
fxy060608 已提交
41
      code: MPErrorCodes.X_V_ON_DYNAMIC_EVENT,
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
      loc: {
        start: {
          line: 1,
          column: 6,
        },
        end: {
          line: 1,
          column: 28,
        },
      },
    })
  })
})