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 8 9 10 11 12 13 14 15

function parseWithVOn(template: string, options: CompilerOptions = {}) {
  const { ast } = compile(template, options)
  return {
    root: ast,
    node: ast.children[0] as ElementNode,
  }
}

describe('compiler(mp): transform v-on', () => {
fxy060608's avatar
fxy060608 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  test('lazy element', () => {
    assert(
      `<editor/>`,
      `<editor/>`,
      `(_ctx, _cache) => {
  return {}
}`
    )
    assert(
      `<editor @ready="ready"/>`,
      `<block wx:if="{{r0}}"><editor bindready="{{a}}"/></block>`,
      `(_ctx, _cache) => {
  return { a: _vOn(_ctx.ready) }
}`
    )
  })
fxy060608's avatar
fxy060608 已提交
32 33 34 35
  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 已提交
36
      code: MPErrorCodes.X_V_ON_DYNAMIC_EVENT,
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
      loc: {
        start: {
          line: 1,
          column: 6,
        },
        end: {
          line: 1,
          column: 28,
        },
      },
    })
  })
})