import { transformRef } from '@dcloudio/uni-cli-shared'
import { BindingTypes } from '@vue/compiler-core'
import { assert } from './testUtils'
const nodeTransforms = [transformRef]
describe('compiler: transform ref', () => {
test('without ref', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return {}
}`,
{
nodeTransforms,
}
)
assert(
``,
``,
`(_ctx, _cache) => {
return {}
}`,
{
nodeTransforms,
}
)
})
test('static ref', () => {
assert(
``,
``,
`import { resolveComponent as _resolveComponent, sr as _sr } from "vue"
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
export function render(_ctx, _cache) {
return { a: _sr('custom', '2a9ec0b0-0') }
}`,
{
inline: false,
nodeTransforms,
}
)
assert(
``,
``,
`import { resolveComponent as _resolveComponent, sr as _sr, f as _f } from "vue"
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
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(
``,
``,
`(_ctx, _cache) => {
return { a: _sr((_value, _refs) => { _refs['custom'] = _value; }, '2a9ec0b0-0') }
}`,
{
nodeTransforms,
}
)
assert(
``,
``,
`(_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(
``,
``,
`(_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(
``,
``,
`(_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(
``,
``,
`(_ctx, _cache) => {
return { a: _sr((_value, _refs) => { _refs['custom'] = _value; _isRef(custom) ? custom.value = _value : custom = _value; }, '2a9ec0b0-0') }
}`,
{
bindingMetadata: {
custom: BindingTypes.SETUP_LET,
},
nodeTransforms,
}
)
})
test('dynamic ref', () => {
assert(
``,
``,
`import { resolveComponent as _resolveComponent, sr as _sr } from "vue"
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
export function render(_ctx, _cache) {
return { a: _sr(_ctx.custom, '2a9ec0b0-0'), b: _ctx.custom }
}`,
{
inline: false,
nodeTransforms,
}
)
assert(
``,
``,
`import { resolveComponent as _resolveComponent, sr as _sr, f as _f } from "vue"
const __BINDING_COMPONENTS__ = '{"custom":{"name":"_component_custom","type":"unknown"}}'
if (!Array) {const _component_custom = _resolveComponent("custom");Math.max.call(null, _component_custom);}
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(
``,
``,
`(_ctx, _cache) => {
return { a: _sr(_ctx.custom, '2a9ec0b0-0'), b: _ctx.custom }
}`,
{
nodeTransforms,
}
)
assert(
``,
``,
`(_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 }
}`,
{
nodeTransforms,
}
)
})
})