ref.spec.ts 5.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { transformRef } from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
2
import { BindingTypes } from '@vue/compiler-core'
fxy060608's avatar
fxy060608 已提交
3 4
import { assert } from './testUtils'

fxy060608's avatar
fxy060608 已提交
5
const nodeTransforms = [transformRef]
fxy060608's avatar
fxy060608 已提交
6
describe('compiler: transform ref', () => {
fxy060608's avatar
fxy060608 已提交
7
  test('without ref', () => {
fxy060608's avatar
fxy060608 已提交
8 9
    assert(
      `<custom/>`,
fxy060608's avatar
fxy060608 已提交
10
      `<custom u-i="2a9ec0b0-0"/>`,
fxy060608's avatar
fxy060608 已提交
11 12
      `(_ctx, _cache) => {
  return {}
fxy060608's avatar
fxy060608 已提交
13 14 15 16
}`,
      {
        nodeTransforms,
      }
fxy060608's avatar
fxy060608 已提交
17 18
    )
    assert(
fxy060608's avatar
fxy060608 已提交
19
      `<custom/><custom/><custom1/>`,
fxy060608's avatar
fxy060608 已提交
20
      `<custom u-i="2a9ec0b0-0"/><custom u-i="2a9ec0b0-1"/><custom1 u-i="2a9ec0b0-2"/>`,
fxy060608's avatar
fxy060608 已提交
21 22
      `(_ctx, _cache) => {
  return {}
fxy060608's avatar
fxy060608 已提交
23 24 25 26
}`,
      {
        nodeTransforms,
      }
fxy060608's avatar
fxy060608 已提交
27 28 29 30 31
    )
  })
  test('static ref', () => {
    assert(
      `<custom ref="custom"/>`,
fxy060608's avatar
fxy060608 已提交
32
      `<custom class="r" u-r="custom" u-i="2a9ec0b0-0"/>`,
33
      `import { resolveComponent as _resolveComponent, sr as _sr } from "vue"
fxy060608's avatar
fxy060608 已提交
34
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
35
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41 42 43 44 45 46 47

export function render(_ctx, _cache) {
  return { a: _sr('custom', '2a9ec0b0-0') }
}`,
      {
        inline: false,
        nodeTransforms,
      }
    )
    assert(
      `<custom v-for="item in items" ref="custom"/>`,
      `<custom wx:for="{{a}}" wx:for-item="item" class="r-i-f" u-r="custom" u-i="{{item.b}}"/>`,
48
      `import { resolveComponent as _resolveComponent, sr as _sr, f as _f } from "vue"
fxy060608's avatar
fxy060608 已提交
49
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
50
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64

export function render(_ctx, _cache) {
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _sr('custom', '2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`,
      {
        inline: false,
        nodeTransforms,
      }
    )
  })
  test('static ref with inline', () => {
    assert(
      `<custom ref="custom"/>`,
      `<custom class="r" u-r="custom" u-i="2a9ec0b0-0"/>`,
fxy060608's avatar
fxy060608 已提交
65
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
66
  return { a: _sr((_value, _refs) => { _refs['custom'] = _value; }, '2a9ec0b0-0') }
fxy060608's avatar
fxy060608 已提交
67 68 69 70
}`,
      {
        nodeTransforms,
      }
fxy060608's avatar
fxy060608 已提交
71 72 73
    )
    assert(
      `<custom v-for="item in items" ref="custom"/>`,
fxy060608's avatar
fxy060608 已提交
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 111 112 113 114 115 116
      `<custom wx:for="{{a}}" wx:for-item="item" class="r-i-f" u-r="custom" u-i="{{item.b}}"/>`,
      `(_ctx, _cache) => {
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _sr((_value, _refs) => { _refs['custom'] = _value; }, '2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`,
      {
        nodeTransforms,
      }
    )
  })
  test('static ref with inline and setup-ref', () => {
    assert(
      `<custom ref="custom"/>`,
      `<custom class="r" u-r="custom" u-i="2a9ec0b0-0"/>`,
      `(_ctx, _cache) => {
  return { a: _sr((_value, _refs) => { _refs['custom'] = _value; custom.value = _value; }, '2a9ec0b0-0') }
}`,
      {
        bindingMetadata: {
          custom: BindingTypes.SETUP_REF,
        },
        nodeTransforms,
      }
    )
  })
  test('static ref with inline and setup-maybe-ref', () => {
    assert(
      `<custom ref="custom"/>`,
      `<custom class="r" u-r="custom" u-i="2a9ec0b0-0"/>`,
      `(_ctx, _cache) => {
  return { a: _sr((_value, _refs) => { _refs['custom'] = _value; _isRef(custom) && (custom.value = _value); }, '2a9ec0b0-0') }
}`,
      {
        bindingMetadata: {
          custom: BindingTypes.SETUP_MAYBE_REF,
        },
        nodeTransforms,
      }
    )
  })
  test('static ref with inline and setup-let', () => {
    assert(
      `<custom ref="custom"/>`,
      `<custom class="r" u-r="custom" u-i="2a9ec0b0-0"/>`,
fxy060608's avatar
fxy060608 已提交
117
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
118
  return { a: _sr((_value, _refs) => { _refs['custom'] = _value; _isRef(custom) ? custom.value = _value : custom = _value; }, '2a9ec0b0-0') }
fxy060608's avatar
fxy060608 已提交
119 120
}`,
      {
fxy060608's avatar
fxy060608 已提交
121 122 123
        bindingMetadata: {
          custom: BindingTypes.SETUP_LET,
        },
fxy060608's avatar
fxy060608 已提交
124 125
        nodeTransforms,
      }
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130
    )
  })
  test('dynamic ref', () => {
    assert(
      `<custom :ref="custom"/>`,
fxy060608's avatar
fxy060608 已提交
131
      `<custom class="r" u-r="{{b}}" u-i="2a9ec0b0-0"/>`,
132
      `import { resolveComponent as _resolveComponent, sr as _sr } from "vue"
fxy060608's avatar
fxy060608 已提交
133
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
134
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140 141 142 143 144 145 146

export function render(_ctx, _cache) {
  return { a: _sr(_ctx.custom, '2a9ec0b0-0'), b: _ctx.custom }
}`,
      {
        inline: false,
        nodeTransforms,
      }
    )
    assert(
      `<custom v-for="item in items" :ref="custom"/>`,
      `<custom wx:for="{{a}}" wx:for-item="item" class="r-i-f" u-r="{{b}}" u-i="{{item.b}}"/>`,
147
      `import { resolveComponent as _resolveComponent, sr as _sr, f as _f } from "vue"
fxy060608's avatar
fxy060608 已提交
148
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
149
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
fxy060608's avatar
fxy060608 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163

export function render(_ctx, _cache) {
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _sr(_ctx.custom, '2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }), b: _ctx.custom }
}`,
      {
        inline: false,
        nodeTransforms,
      }
    )
  })
  test('dynamic ref with inline', () => {
    assert(
      `<custom :ref="custom"/>`,
      `<custom class="r" u-r="{{b}}" u-i="2a9ec0b0-0"/>`,
fxy060608's avatar
fxy060608 已提交
164
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
165
  return { a: _sr(_ctx.custom, '2a9ec0b0-0'), b: _ctx.custom }
fxy060608's avatar
fxy060608 已提交
166 167 168 169
}`,
      {
        nodeTransforms,
      }
fxy060608's avatar
fxy060608 已提交
170 171 172
    )
    assert(
      `<custom v-for="item in items" :ref="custom"/>`,
fxy060608's avatar
fxy060608 已提交
173
      `<custom wx:for="{{a}}" wx:for-item="item" class="r-i-f" u-r="{{b}}" u-i="{{item.b}}"/>`,
fxy060608's avatar
fxy060608 已提交
174
      `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
175
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _sr(_ctx.custom, '2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }), b: _ctx.custom }
fxy060608's avatar
fxy060608 已提交
176 177 178 179
}`,
      {
        nodeTransforms,
      }
fxy060608's avatar
fxy060608 已提交
180 181 182
    )
  })
})