compiler-mp-alipay.spec.js 9.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
const compiler = require('../lib')

3 4
function assertCodegen (template, templateCode, renderCode = 'with(this){}', mpOptions = {}, baseOptions = {}) {
  const res = compiler.compile(template, Object.assign({
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9
    resourcePath: 'test.wxml',
    mp: Object.assign({
      minified: true,
      isTest: true,
      platform: 'mp-alipay'
10 11
    }, mpOptions)
  }, baseOptions))
fxy060608's avatar
fxy060608 已提交
12 13 14 15 16 17 18 19 20

  expect(res.template).toBe(templateCode)
  expect(res.render).toBe(renderCode)
}

describe('mp:compiler-mp-alipay', () => {
  it('generate v-for directive', () => {
    assertCodegen(
      '<view><view v-for="(item,index) in items" :key="index"></view></view>',
21
      '<view><view a:for="{{items}}" a:for-item="item" a:for-index="index" a:key="index"></view></view>'
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27
    )
  })

  it('generate v-else-if with v-else directive', () => {
    assertCodegen(
      '<view><view v-if="show">hello</view><view v-else-if="hide">world</view><view v-else>bye</view></view>',
fxy060608's avatar
fxy060608 已提交
28
      '<view><block a:if="{{show}}"><view>hello</view></block><block a:else><block a:if="{{hide}}"><view>world</view></block><block a:else><view>bye</view></block></block></view>'
fxy060608's avatar
fxy060608 已提交
29
    )
fxy060608's avatar
fxy060608 已提交
30 31 32 33
  })
  it('generate ref', () => {
    assertCodegen(
      '<component1 ref="c1">text</component1>',
fxy060608's avatar
fxy060608 已提交
34
      '<component1 vue-id="551070e6-1" ref="__r" data-ref="c1" onVueInit="__l">text</component1>'
fxy060608's avatar
fxy060608 已提交
35 36 37
    )
    assertCodegen(
      '<component1 :ref="c2">text<text>123213</text></component1>',
fxy060608's avatar
fxy060608 已提交
38
      '<component1 vue-id="551070e6-1" ref="__r" data-ref="{{c2}}" onVueInit="__l">text<text>123213</text></component1>'
fxy060608's avatar
fxy060608 已提交
39 40 41
    )
    assertCodegen(
      '<component1 v-for="item in items" ref="c3"></component1>',
42
      '<component1 vue-id="{{\'551070e6-1-\'+__i0__}}" ref="__r" data-ref-in-for="c3" a:for="{{items}}" a:for-item="item" a:for-index="__i0__" onVueInit="__l"></component1>'
fxy060608's avatar
fxy060608 已提交
43 44 45
    )
    assertCodegen(
      '<component1 v-for="item in items" :ref="c4"></component1>',
46
      '<component1 vue-id="{{\'551070e6-1-\'+__i0__}}" ref="__r" data-ref-in-for="{{c4}}" a:for="{{items}}" a:for-item="item" a:for-index="__i0__" onVueInit="__l"></component1>'
fxy060608's avatar
fxy060608 已提交
47
    )
48 49 50 51 52 53 54 55 56
    assertCodegen(
      '<component1 @change="onChange">text</component1>',
      '<component1 onChange="__e" vue-id="551070e6-1" data-event-opts="{{[[\'^change\',[[\'onChange\']]]]}}" data-com-type="wx" ref="__r" onVueInit="__l">text</component1>',
      undefined,
      undefined,
      {
        wxComponents: { component1: '/mycomponents/component1' }
      }
    )
fxy060608's avatar
fxy060608 已提交
57 58 59 60
  })
  it('generate default slot', () => {
    assertCodegen(
      '<component1>text</component1>',
fxy060608's avatar
fxy060608 已提交
61
      '<component1 vue-id="551070e6-1" onVueInit="__l">text</component1>'
fxy060608's avatar
fxy060608 已提交
62 63 64
    )
    assertCodegen(
      '<component1>text<text>123213</text></component1>',
fxy060608's avatar
fxy060608 已提交
65
      '<component1 vue-id="551070e6-1" onVueInit="__l">text<text>123213</text></component1>'
fxy060608's avatar
fxy060608 已提交
66 67 68
    )
    assertCodegen(
      '<component1>text<block slot="right"></block></component1>',
fxy060608's avatar
fxy060608 已提交
69
      '<component1 vue-id="551070e6-1" onVueInit="__l">text<view slot="right"></view></component1>'
fxy060608's avatar
fxy060608 已提交
70 71
    )
  })
72 73 74 75

  it('generate scoped slot', () => {
    assertCodegen(
      '<component1 :text="\'text\'"><template v-slot="props"><view :class="{text:props.text}">{{props.text}}</view></template></component1>',
76
      '<component1 vue-id="551070e6-1" text="text" onVueInit="__l"><view slot-scope="props"><view class="{{((props.text)?\'text\':\'\')}}">{{props.text}}</view></view></component1>'
77 78 79
    )
    assertCodegen(
      '<component1 :text="\'text\'"><template v-slot="{text}"><view :class="{text:text}">{{text}}</view></template></component1>',
80
      '<component1 vue-id="551070e6-1" text="text" onVueInit="__l"><view slot-scope="__SCOPED__"><view class="{{((__SCOPED__.text)?\'text\':\'\')}}">{{__SCOPED__.text}}</view></view></component1>'
81 82 83
    )
  })

fxy060608's avatar
fxy060608 已提交
84 85 86
  it('generate class binding', () => {
    assertCodegen(
      '<div :class="{ active: isActive }">1</div>',
fxy060608's avatar
fxy060608 已提交
87
      '<view class="{{((\'_div\')+\' \'+((isActive)?\'active\':\'\'))}}">1</view>'
fxy060608's avatar
fxy060608 已提交
88 89
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
90 91
      '<p class="static" :class="{ active: isActive, \'text-danger\': hasError }">2</p>',
      '<view class="{{(((\'static _p\')+\' \'+((isActive)?\'active\':\'\'))+\' \'+((hasError)?\'text-danger\':\'\'))}}">2</view>'
fxy060608's avatar
fxy060608 已提交
92 93 94
    )
    assertCodegen(
      '<p class="static" :class="[activeClass, errorClass]">3</p>',
fxy060608's avatar
fxy060608 已提交
95
      '<view class="{{(((\'static _p\')+\' \'+activeClass)+\' \'+errorClass)}}">3</view>'
fxy060608's avatar
fxy060608 已提交
96 97
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
98 99
      '<p class="static" :class="[isActive ? activeClass : \'\', errorClass]">4</p>',
      '<view class="{{(((\'static _p\')+\' \'+(isActive?activeClass:\'\'))+\' \'+errorClass)}}">4</view>'
fxy060608's avatar
fxy060608 已提交
100 101
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
102
      '<p class="static" :class="[{ active: isActive }, errorClass]">5</p>',
D
DCloud_LXH 已提交
103
      '<view class="{{(((\'static _p\')+\' \'+((isActive)?\'active\':\'\'))+\' \'+errorClass)}}">5</view>'
fxy060608's avatar
fxy060608 已提交
104
    )
105 106 107 108
    assertCodegen(
      '<p class="static" :class="[{ active: isActive, disabled: isDisabled }, errorClass]">52</p>',
      '<view class="{{(((\'static _p\')+\' \'+(((isActive)?\'active\':\'\')+\' \'+((isDisabled)?\'disabled\':\'\')))+\' \'+errorClass)}}">52</view>'
    )
fxy060608's avatar
fxy060608 已提交
109
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
110 111
      '<div class="container" :class="computedClassObject">6</div>',
      '<view class="{{((\'container _div\')+\' \'+computedClassObject)}}">6</view>'
fxy060608's avatar
fxy060608 已提交
112 113 114 115 116 117 118
    )
    //     assertCodegen(
    //       `<div class="container" :class="computedClassObject">6</div>`,
    //       `<view class="{{$root.c0}}">6</view>`,
    //       `with(this){var c0=__get_class(computedClassObject,"container");$mp.data=Object.assign({},{$root:{c0:c0}})}`
    //     )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
119 120
      '<p :class="index === currentIndex ? activeClass : itemClass">7</p>',
      '<view class="{{((\'_p\')+\' \'+(index===currentIndex?activeClass:itemClass))}}">7</view>'
fxy060608's avatar
fxy060608 已提交
121 122
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
123 124
      '<p :class="\'m-content-head-\'+message.user">8</p>',
      '<view class="{{((\'_p\')+\' \'+(\'m-content-head-\'+message.user))}}">8</view>'
fxy060608's avatar
fxy060608 已提交
125 126
    )
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
127 128
      '<p :class="classStr1 || classStr2" class="bg">9</p>',
      '<view class="{{((\'bg _p\')+\' \'+(classStr1||classStr2))}}">9</view>'
fxy060608's avatar
fxy060608 已提交
129
    )
D
DCloud_LXH 已提交
130 131 132 133
    assertCodegen(
      '<p class="static" :class="[{ active: isActive }, errorClass, [flex, \'flex-row\']]">10</p>',
      '<view class="{{((((\'static _p\')+\' \'+((isActive)?\'active\':\'\'))+\' \'+errorClass)+\' \'+((flex)+\' \'+\'flex-row\'))}}">10</view>'
    )
fxy060608's avatar
fxy060608 已提交
134 135
  })

fxy060608's avatar
fxy060608 已提交
136 137 138
  it('generate getPhoneNumber', () => {
    assertCodegen(
      '<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>',
fxy060608's avatar
fxy060608 已提交
139
      '<button open-type="getAuthorize" scope="phoneNumber" data-event-opts="{{[[\'getAuthorize\',[[\'$onAliGetAuthorize\',[\'getPhoneNumber\',\'$event\']]]],[\'error\',[[\'$onAliAuthError\',[\'getPhoneNumber\',\'$event\']]]]]}}" onGetAuthorize="__e" onError="__e">获取手机号</button>'
fxy060608's avatar
fxy060608 已提交
140 141 142
    )
  })

fxy060608's avatar
fxy060608 已提交
143 144
  it('generate events with v-on directive', () => {
    assertCodegen(
fxy060608's avatar
fxy060608 已提交
145
      '<uni-list-item title="标题文字" note="描述信息" show-extra-icon="true" :extra-icon="{color: \'#4cd964\',size: \'22\',type: \'spinner\'}"></uni-list-item>',
146 147
      '<uni-list-item vue-id="551070e6-1" title="标题文字" note="描述信息" show-extra-icon="true" extra-icon="{{({color:\'#4cd964\',size:\'22\',type:\'spinner\'})}}" onVueInit="__l"></uni-list-item>',
      'with(this){}'
fxy060608's avatar
fxy060608 已提交
148 149 150
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
151 152
      '<view @click="onClick" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchCancel" @longtap="onLongTap" @longpress="onLongPress"></view>',
      '<view data-event-opts="{{[[\'tap\',[[\'onClick\',[\'$event\']]]],[\'touchStart\',[[\'onTouchStart\',[\'$event\']]]],[\'touchMove\',[[\'onTouchMove\',[\'$event\']]]],[\'touchEnd\',[[\'onTouchEnd\',[\'$event\']]]],[\'touchCancel\',[[\'onTouchCancel\',[\'$event\']]]],[\'longTap\',[[\'onLongTap\',[\'$event\']]]],[\'longTap\',[[\'onLongPress\',[\'$event\']]]]]}}" onTap="__e" onTouchStart="__e" onTouchMove="__e" onTouchEnd="__e" onTouchCancel="__e" onLongTap="__e"></view>'
fxy060608's avatar
fxy060608 已提交
153 154 155
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
156 157
      '<form @submit="formSubmit" @reset="formReset"/>',
      '<form data-event-opts="{{[[\'submit\',[[\'formSubmit\',[\'$event\']]]]]}}" onSubmit="__e" onReset="formReset"></form>'
fxy060608's avatar
fxy060608 已提交
158 159 160
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
161 162
      '<map @callouttap="calloutTap" @controltap="controlTap" @markertap="markerTap" @regionchange="regionChange" @tap="tap"/>',
      '<map data-event-opts="{{[[\'tap\',[[\'tap\',[\'$event\']]]]]}}" onCalloutTap="calloutTap" onControlTap="controlTap" onMarkerTap="markerTap" onRegionChange="regionChange" onTap="__e"></map>'
fxy060608's avatar
fxy060608 已提交
163 164 165
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
166 167
      '<view @transitionend="transitionEnd" @animationstart="animationStart" @animationiteration="animationIteration" @animationend="animationEnd" @firstappear="firstAppear"/>',
      '<view data-event-opts="{{[[\'transitionEnd\',[[\'transitionEnd\',[\'$event\']]]],[\'animationStart\',[[\'animationStart\',[\'$event\']]]],[\'animationIteration\',[[\'animationIteration\',[\'$event\']]]],[\'animationEnd\',[[\'animationEnd\',[\'$event\']]]],[\'firstAppear\',[[\'firstAppear\',[\'$event\']]]]]}}" onTransitionEnd="__e" onAnimationStart="__e" onAnimationIteration="__e" onAnimationEnd="__e" onFirstAppear="__e"></view>'
fxy060608's avatar
fxy060608 已提交
168 169 170
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
171 172
      '<scroll-view @scrolltoupper="scrollToUpper" @scrolltolower="scrollToLower"/>',
      '<scroll-view data-event-opts="{{[[\'scrollToUpper\',[[\'scrollToUpper\',[\'$event\']]]],[\'scrollToLower\',[[\'scrollToLower\',[\'$event\']]]]]}}" onScrollToUpper="__e" onScrollToLower="__e"></scroll-view>'
fxy060608's avatar
fxy060608 已提交
173 174 175
    )

    assertCodegen(
fxy060608's avatar
fxy060608 已提交
176 177
      '<movable-view @changeend="changeEnd"/>',
      '<movable-view data-event-opts="{{[[\'changeEnd\',[[\'changeEnd\',[\'$event\']]]]]}}" onChangeEnd="__e"></movable-view>'
fxy060608's avatar
fxy060608 已提交
178 179
    )
  })
180
})