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

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

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

describe('mp:compiler-mp-baidu', () => {
  it('generate v-for directive', () => {
    assertCodegen(
      '<view><view v-for="(item,index) in items" :key="index"></view></view>',
fxy060608's avatar
fxy060608 已提交
21
      '<view><block s-for="{{items}}" s-for-item="item" s-for-index="index" s-key="index"><view></view></block></view>'
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26
    )
  })
  it('generate scoped slot', () => {
    assertCodegen(
      '<foo><template slot-scope="bar">{{ bar.foo }}</template></foo>',
27
      '<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view>{{foo}}</view></foo>'
fxy060608's avatar
fxy060608 已提交
28 29 30
    )
    assertCodegen(
      '<foo><view slot-scope="bar">{{ bar.foo }}</view></foo>',
31
      '<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view><view>{{foo}}</view></view></foo>'
fxy060608's avatar
fxy060608 已提交
32 33 34 35 36 37
    )
  })

  it('generate named scoped slot', () => {
    assertCodegen(
      '<foo><template slot="foo" slot-scope="bar">{{ bar.foo }}</template></foo>',
fxy060608's avatar
fxy060608 已提交
38
      '<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><view slot="foo">{{foo}}</view></foo>'
fxy060608's avatar
fxy060608 已提交
39 40 41
    )
    assertCodegen(
      '<foo><view slot="foo" slot-scope="bar">{{ bar.foo }}</view></foo>',
fxy060608's avatar
fxy060608 已提交
42
      '<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><view slot="foo"><view>{{foo}}</view></view></foo>'
fxy060608's avatar
fxy060608 已提交
43 44 45 46 47 48
    )
  })

  it('generate scoped slot with multiline v-if', () => {
    assertCodegen(
      '<foo><template v-if="\nshow\n" slot-scope="bar">{{ bar.foo }}</template></foo>',
49
      '<foo vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view><block s-if="{{show}}">{{foo}}</block><block s-else><block></block></block></view></foo>'
fxy060608's avatar
fxy060608 已提交
50 51 52
    )
    assertCodegen(
      '<foo><view v-if="\nshow\n" slot="foo" slot-scope="bar">{{ bar.foo }}</view></foo>',
fxy060608's avatar
fxy060608 已提交
53
      '<foo vue-id="551070e6-1" vue-slots="{{[\'foo\']}}"><view slot="foo"><block s-if="{{show}}"><view>{{foo}}</view></block></view></foo>'
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59
    )
  })

  it('generate scoped slot', () => {
    assertCodegen(
      '<span><slot v-bind:user="user">{{ user.lastName }}</slot></span>',
60
      '<label class="_span"><block s-if="{{$slots.default}}"><slot var-user="user"></slot></block><block s-else>{{user.lastName}}</block></label>'
fxy060608's avatar
fxy060608 已提交
61 62 63
    )
    assertCodegen(
      '<span><slot name="header" v-bind:user="user">{{ user.lastName }}</slot></span>',
fxy060608's avatar
fxy060608 已提交
64
      '<label class="_span"><block s-if="{{$slots.header}}"><slot name="header" var-user="user"></slot></block><block s-else>{{user.lastName}}</block></label>'
fxy060608's avatar
fxy060608 已提交
65 66
    )
  })
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  it('generate scoped slot with scopedSlotsCompiler: auto', () => {
    assertCodegen(
      '<my-component><template v-slot="{item}">{{item}}<template></my-component>',
      '<my-component vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><view>{{item}}</view></my-component>',
      'with(this){}',
      {
        scopedSlotsCompiler: 'auto'
      }
    )
    assertCodegen(
      '<my-component><template v-slot="{item}">{{getValue(item)}}<template></my-component>',
      '<my-component scoped-slots-compiler="augmented" vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block><block s-if="{{$root.m0}}">{{$root.m1}}</block></block></my-component>',
      'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default","item")):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
      {
        scopedSlotsCompiler: 'auto'
      }
    )
    assertCodegen(
      '<my-component><template v-slot="item">{{getValue(item.text)}}<template></my-component>',
      '<my-component scoped-slots-compiler="augmented" vue-id="551070e6-1" vue-slots="{{[\'default\']}}"><block><block s-if="{{$root.m0}}">{{$root.m1}}</block></block></my-component>',
      'with(this){var m0=$hasScopedSlotsParams("551070e6-1");var m1=m0?getValue($getScopedSlotsParams("551070e6-1","default").text):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}',
      {
        scopedSlotsCompiler: 'auto'
      }
    )
    assertCodegen(
      '<view><slot :item="item"><slot></view>',
      '<view><block s-if="{{$slots.default}}"><slot var-item="item"></slot></block><block s-else><slot></slot></block></view>',
      'with(this){if($mp.component.data.scopedSlotsCompiler==="augmented"){const $root=$mp.data.$root;$setScopedSlotsParams("default",{"item":item})}}',
      {
        scopedSlotsCompiler: 'auto'
      }
    )
    assertCodegen(
      '<view><slot v-bind="object"><slot></view>',
      '<view><block s-if="{{$slots.default}}"><slot></slot></block><block s-else><slot></slot></block></view>',
      'with(this){if($mp.component.data.scopedSlotsCompiler==="augmented"){const $root=$mp.data.$root;$setScopedSlotsParams("default",object)}}',
      {
        scopedSlotsCompiler: 'auto'
      }
    )
  })

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  it('generate vue id', () => {
    assertCodegen(
      '<Test/>',
      '<test vue-id="551070e6-1"></test>'
    )
    assertCodegen(
      '<Test a="a">',
      '<test vue-id="551070e6-1" a="a"></test>'
    )
    assertCodegen(
      '<view><Test v-for="item in items" :key="item"/></view>',
      '<view><block s-for="{{items}}" s-for-item="item" s-for-index="__i0__" s-key="*this"><test vue-id="{{\'551070e6-1-\'+__i0__}}"></test></block></view>'
    )
    assertCodegen(
      '<view><Test v-for="item in items" :key="item"><Test v-for="item in item.items" :key="item"></Test></Test></view>',
      '<view><block s-for="{{items}}" s-for-item="item" s-for-index="__i0__" s-key="*this"><test vue-id="{{\'551070e6-1-\'+__i0__}}" vue-slots="{{[\'default\']}}"><block s-for="{{item.items}}" s-for-item="item" s-for-index="__i1__" s-key="*this"><test vue-id="{{(\'551070e6-2-\'+__i0__+\'-\'+__i1__)+\',\'+(\'551070e6-1-\'+__i0__)}}"></test></block></test></block></view>'
    )
    assertCodegen(
      '<view><Test v-for="(item,index) in items" :key="item"><Test v-for="(item,index1) in item.items" :key="item"></Test></Test></view>',
      '<view><block s-for="{{items}}" s-for-item="item" s-for-index="index" s-key="*this"><test vue-id="{{\'551070e6-1-\'+index}}" vue-slots="{{[\'default\']}}"><block s-for="{{item.items}}" s-for-item="item" s-for-index="index1" s-key="*this"><test vue-id="{{(\'551070e6-2-\'+index+\'-\'+index1)+\',\'+(\'551070e6-1-\'+index)}}"></test></block></test></block></view>'
    )
  })

  it('generate text trim', () => {
    assertCodegen(
      '<text>\nN: {{title}}\n′</text>',
      '<text>{{\'N: \'+title+"\\\\n′"}}</text>'
    )
    assertCodegen(
      '<text>我是第一行1\n我的第二行</text>',
      '<text>我是第一行1\n我的第二行</text>'
    )
    assertCodegen(
      '<text>我是第一行2\n我的第二行1{{title}}</text>',
      '<text>{{"我是第一行2\\\\n我的第二行1"+title}}</text>'
    )
    assertCodegen(
      `<text>我是第一行3
    我的第二行2{{title}}</text>`,
      '<text>{{"我是第一行3\\\\n    我的第二行2"+title}}</text>'
    )
  })
})