wxml.spec.js 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
const {
  transformTemplate
} = require('../lib/mp-weixin/transform/template-transformer')

function assertCodegen(wxmlCode, vueCode) {
  expect(transformTemplate(wxmlCode)[0]).toBe(vueCode)
}
describe('wxml:compiler', () => {
fxy060608's avatar
fxy060608 已提交
9 10 11
  it('generate event', () => {
    assertCodegen(
      `<view bindtouchstart="startDrag" catchtouchmove="{{ catchMove ? 'noop' : '' }}"/>`,
fxy060608's avatar
fxy060608 已提交
12
      `<uni-shadow-root><view @touchstart="startDrag" @touchmove.stop.prevent="_$self[(catchMove ? 'noop' : '')||'_$noop']($event)"></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
13 14 15
    )
    assertCodegen(
      `<uni-transition bind:click="click" bindtouchstart="startDrag" catchtouchmove="{{ catchMove ? 'noop' : '' }}"/>`,
fxy060608's avatar
fxy060608 已提交
16
      `<uni-shadow-root><uni-transition @click="click" @touchstart.native="startDrag" @touchmove.native.stop.prevent="_$self[(catchMove ? 'noop' : '')||'_$noop']($event)"></uni-transition></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
17
    )
fxy060608's avatar
fxy060608 已提交
18 19 20 21
  })
  it('generate class', () => {
    assertCodegen(
      `<view class="van-notice-bar__content {{ !scrollable && !wrapable ? 'van-ellipsis' : '' }}"></view>`,
fxy060608's avatar
fxy060608 已提交
22
      `<uni-shadow-root><view :class="'van-notice-bar__content '+(!scrollable && !wrapable ? 'van-ellipsis' : '')"></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
23
    )
fxy060608's avatar
fxy060608 已提交
24
  })
fxy060608's avatar
fxy060608 已提交
25 26
  it('generate v-if', () => {
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
27
      '<block wx:if="{{ !loading }}" loading loading-text="">{{ item.name }}</block>',
fxy060608's avatar
fxy060608 已提交
28
      `<uni-shadow-root><block v-if="(!loading)" loading loading-text>{{ item.name }}</block></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33
    )
  })
  it('generate v-for', () => {
    assertCodegen(
      '<view wx:for="{{ isSimple(columns) ? [columns] : columns }}" wx:for-item="item1" wx:key="{{ index }}"/>',
fxy060608's avatar
fxy060608 已提交
34
      `<uni-shadow-root><view v-for="(item1,index) in (isSimple(columns) ? [columns] : columns)" :key="item1.index"></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
35 36
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
37
      '<view wx:for="{{ columns }}" wx:for-item="item1" wx:key="item1"/>',
fxy060608's avatar
fxy060608 已提交
38
      `<uni-shadow-root><view v-for="(item1,index) in (columns)" :key="item1"></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
39 40 41
    )
    assertCodegen(
      '<view wx:for="{{ columns }}" wx:for-item="index" wx:key="*this"/>',
fxy060608's avatar
fxy060608 已提交
42
      `<uni-shadow-root><view v-for="(index,___i___) in (columns)" :key="index"></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
43 44 45 46
    )
    assertCodegen(
      '<view wx:for="{{ columns }}" wx:for-item="item" wx:key="{{item.value}}"/>',
      `<uni-shadow-root><view v-for="(item,index) in (columns)" :key="item.value"></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51
    )
  })
  it('generate root element', () => {
    assertCodegen(
      '<view></view><view></view>',
fxy060608's avatar
fxy060608 已提交
52
      `<uni-shadow-root><view></view><view></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57
    )

    assertCodegen(
      `<view></view>
<wxs></wxs>`,
fxy060608's avatar
fxy060608 已提交
58
      `<uni-shadow-root><view></view></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
59 60 61 62 63
    )


    assertCodegen(
      '<slot></slot>',
fxy060608's avatar
fxy060608 已提交
64
      `<uni-shadow-root><slot></slot></uni-shadow-root>`
fxy060608's avatar
fxy060608 已提交
65 66 67
    )
  })
})