component.spec.ts 5.6 KB
Newer Older
1
import {
2
  addMiniProgramPageJson,
3 4 5
  COMPONENT_BIND_LINK,
  createTransformComponentLink,
} from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
6 7
import { assert } from './testUtils'

8
const nodeTransforms = [createTransformComponentLink(COMPONENT_BIND_LINK)]
fxy060608's avatar
fxy060608 已提交
9
describe('compiler: transform component', () => {
fxy060608's avatar
fxy060608 已提交
10
  test('component + component', () => {
fxy060608's avatar
fxy060608 已提交
11
    assert(
fxy060608's avatar
fxy060608 已提交
12
      `<custom><custom1/></custom>`,
fxy060608's avatar
fxy060608 已提交
13
      `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><custom1 u-i="2a9ec0b0-1,2a9ec0b0-0" bind:__l="__l"/></custom>`,
fxy060608's avatar
fxy060608 已提交
14 15
      `(_ctx, _cache) => {
  return {}
fxy060608's avatar
fxy060608 已提交
16 17
}`,
      {
18
        nodeTransforms,
fxy060608's avatar
fxy060608 已提交
19 20 21 22 23 24
      }
    )
  })
  test('component + component + component', () => {
    assert(
      `<custom><custom1><custom2/><custom2/></custom1></custom>`,
fxy060608's avatar
fxy060608 已提交
25
      `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><custom1 u-s="{{['d']}}" u-i="2a9ec0b0-1,2a9ec0b0-0" bind:__l="__l"><custom2 u-i="2a9ec0b0-2,2a9ec0b0-1" bind:__l="__l"/><custom2 u-i="2a9ec0b0-3,2a9ec0b0-1" bind:__l="__l"/></custom1></custom>`,
fxy060608's avatar
fxy060608 已提交
26 27 28 29
      `(_ctx, _cache) => {
  return {}
}`,
      {
30
        nodeTransforms,
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36
      }
    )
  })
  test('component with v-for', () => {
    assert(
      `<custom v-for="item in items"/>`,
fxy060608's avatar
fxy060608 已提交
37
      `<custom wx:for="{{a}}" wx:for-item="item" u-i="{{item.a}}" bind:__l="__l"/>`,
fxy060608's avatar
fxy060608 已提交
38
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
39
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-0' + '-' + i0 }; }) }
fxy060608's avatar
fxy060608 已提交
40 41
}`,
      {
42
        nodeTransforms,
fxy060608's avatar
fxy060608 已提交
43 44 45 46
      }
    )
    assert(
      `<custom v-for="(item,key,index) in items"/>`,
fxy060608's avatar
fxy060608 已提交
47
      `<custom wx:for="{{a}}" wx:for-item="item" u-i="{{item.a}}" bind:__l="__l"/>`,
fxy060608's avatar
fxy060608 已提交
48
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
49
  return { a: _f(_ctx.items, (item, key, index) => { return { a: '2a9ec0b0-0' + '-' + index }; }) }
fxy060608's avatar
fxy060608 已提交
50 51
}`,
      {
52
        nodeTransforms,
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58
      }
    )
  })
  test('component + component with v-for', () => {
    assert(
      `<custom><custom1 v-for="item in items"/></custom>`,
fxy060608's avatar
fxy060608 已提交
59
      `<custom u-s="{{['d']}}" u-i="2a9ec0b0-0" bind:__l="__l"><custom1 wx:for="{{a}}" wx:for-item="item" u-i="{{item.a}}" bind:__l="__l"/></custom>`,
fxy060608's avatar
fxy060608 已提交
60
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
61
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }; }) }
fxy060608's avatar
fxy060608 已提交
62 63
}`,
      {
64
        nodeTransforms,
fxy060608's avatar
fxy060608 已提交
65 66 67 68 69 70
      }
    )
  })
  test('component with v-for + component', () => {
    assert(
      `<custom v-for="item in items"><custom1/></custom>`,
fxy060608's avatar
fxy060608 已提交
71
      `<custom wx:for="{{a}}" wx:for-item="item" u-s="{{['d']}}" u-i="{{item.b}}" bind:__l="__l"><custom1 u-i="{{item.a}}" bind:__l="__l"/></custom>`,
fxy060608's avatar
fxy060608 已提交
72
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
73
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + ('2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) }
fxy060608's avatar
fxy060608 已提交
74 75
}`,
      {
76
        nodeTransforms,
fxy060608's avatar
fxy060608 已提交
77 78 79 80 81 82
      }
    )
  })
  test('component with v-for + component with v-for', () => {
    assert(
      `<custom v-for="item in items"><custom1 v-for="item1 in item.items"/></custom>`,
fxy060608's avatar
fxy060608 已提交
83
      `<custom wx:for="{{a}}" wx:for-item="item" u-s="{{['d']}}" u-i="{{item.b}}" bind:__l="__l"><custom1 wx:for="{{item.a}}" wx:for-item="item1" u-i="{{item1.a}}" bind:__l="__l"/></custom>`,
fxy060608's avatar
fxy060608 已提交
84
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
85
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.items, (item1, k1, i1) => { return { a: '2a9ec0b0-1' + '-' + i0 + '-' + i1 + ',' + ('2a9ec0b0-0' + '-' + i0) }; }), b: '2a9ec0b0-0' + '-' + i0 }; }) }
fxy060608's avatar
fxy060608 已提交
86 87
}`,
      {
88
        nodeTransforms,
fxy060608's avatar
fxy060608 已提交
89 90 91
      }
    )
  })
fxy060608's avatar
fxy060608 已提交
92 93 94
  test(`component with boolean attribute`, () => {
    assert(
      `<uni-collapse accordion/>`,
fxy060608's avatar
fxy060608 已提交
95
      `<uni-collapse u-i="2a9ec0b0-0" u-p="{{a}}"/>`,
fxy060608's avatar
fxy060608 已提交
96
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
97 98 99 100 101 102
  return { a: _p({ accordion: true }) }
}`
    )
  })
  test(`component with props`, () => {
    assert(
fxy060608's avatar
fxy060608 已提交
103 104
      `<uni-collapse id="id" ref="a" :ref="b" slot="c" :slot="d" class="e" :class="f" style="g:g;" :style="h" @click="i" v-model:first="j" v-model:last="k" prop-a="l" :prop-b="m" data-a="n" :data-b="o" key="p" :key="r" is="s" :is="t"/>`,
      `<uni-collapse id="id" ref="a" ref="{{a}}" slot="c" slot="{{b}}" class="{{['e', c]}}" style="{{'g:g' + ';' + d}}" bindclick="{{e}}" data-a="n" data-b="{{f}}" key="p" key="{{g}}" is="s" is="{{h}}" u-i="2a9ec0b0-0" bindupdateFirst="{{i}}" bindupdateLast="{{j}}" u-p="{{k}}"/>`,
fxy060608's avatar
fxy060608 已提交
105
      `(_ctx, _cache) => {
106
  return { a: _ctx.b, b: _ctx.d, c: _n(_ctx.f), d: _s(_ctx.h), e: _o(_ctx.i), f: _ctx.o, g: _ctx.r, h: _ctx.t, i: _o($event => _ctx.j = $event), j: _o($event => _ctx.k = $event), k: _p({ ['prop-a']: 'l', ['prop-b']: _ctx.m, first: _ctx.j, last: _ctx.k }) }
107 108 109 110 111 112 113
}`
    )
    assert(
      `<uni-collapse v-if="ok" :accordion="true"/><uni-collapse v-else :accordion="true"/>`,
      `<uni-collapse wx:if="{{a}}" u-i="2a9ec0b0-0" u-p="{{b}}"/><uni-collapse wx:else u-i="2a9ec0b0-1" u-p="{{c||''}}"/>`,
      `(_ctx, _cache) => {
  return _e({ a: _ctx.ok }, _ctx.ok ? { b: _p({ accordion: true }) } : { c: _p({ accordion: true }) })
fxy060608's avatar
fxy060608 已提交
114 115 116
}`
    )
  })
117

118 119 120 121 122
  test(`mini program component`, () => {
    const filename = 'pages/vant/vant'
    addMiniProgramPageJson(filename, {
      usingComponents: {
        'van-button': 'wxcomponents/button/index',
123
        wxparser: 'plugin://wxparserPlugin/wxparser',
124 125 126
      },
    })
    assert(
127 128
      `<wxparser :rich-text="richText" /><van-button custom-style="background-color: unset;" :close-on-click-overlay="true"><template #default><view/></template><template #head><view/></template></van-button>`,
      `<wxparser rich-text="{{a}}" u-t="m" u-i="dc555fe4-0" bind:__l="__l"/><van-button u-t="m" u-i="dc555fe4-1" bind:__l="__l" u-p="{{b}}"><view/><view slot="head"/></van-button>`,
129
      `(_ctx, _cache) => {
130
  return { a: _ctx.richText, b: _p({ customStyle: 'background-color: unset;', closeOnClickOverlay: true }) }
131 132 133 134 135 136 137
}`,
      {
        filename,
        nodeTransforms,
      }
    )
  })
fxy060608's avatar
fxy060608 已提交
138
})