const compiler = require('../lib')
function assertCodegen (template, templateCode, renderCode = 'with(this){}', mpOptions = {}, baseOptions = {}) {
const res = compiler.compile(template, Object.assign({
resourcePath: 'test.wxml',
mp: Object.assign({
minified: true,
isTest: true,
platform: 'mp-alipay'
}, mpOptions)
}, baseOptions))
expect(res.template).toBe(templateCode)
expect(res.render).toBe(renderCode)
}
describe('mp:compiler-mp-alipay', () => {
it('generate v-for directive', () => {
assertCodegen(
'',
''
)
})
it('generate v-else-if with v-else directive', () => {
assertCodegen(
'helloworldbye',
'helloworldbye'
)
})
it('generate ref', () => {
assertCodegen(
'text',
'text'
)
assertCodegen(
'text123213',
'text123213'
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'text',
'text',
undefined,
undefined,
{
wxComponents: { component1: '/mycomponents/component1' }
}
)
assertCodegen(
'text',
'text',
undefined,
undefined,
{
wxComponents: { component1: '/mycomponents/component1' }
}
)
assertCodegen(
'text',
'text',
undefined,
undefined,
{
wxComponents: { 'credit-pay': 'plugin://myPlugin/CreditPay' }
}
)
})
it('generate slot fallback content', () => {
assertCodegen(
'slot',
'slot'
)
})
it('generate default slot', () => {
assertCodegen(
'text',
'text'
)
assertCodegen(
'text123213',
'text123213'
)
assertCodegen(
'text',
'text'
)
})
it('generate scoped slot', () => {
assertCodegen(
'{{props.text}}',
'{{props.text}}'
)
assertCodegen(
'{{text}}',
'{{__SCOPED__.text}}'
)
})
it('generate scoped slot with scopedSlotsCompiler: auto', () => {
assertCodegen(
'{{item}}',
'{{__SCOPED__.item}}',
'with(this){}',
{
scopedSlotsCompiler: 'auto'
}
)
assertCodegen(
'{{getValue(item)}}',
'{{$root.m1}}',
'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(
'{{getValue(item.text)}}',
'{{$root.m1}}',
'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(
'',
'',
'with(this){if($scope.props.scopedSlotsCompiler==="augmented"){$setScopedSlotsParams("default",{"item":item})}}',
{
scopedSlotsCompiler: 'auto'
}
)
assertCodegen(
'',
'',
'with(this){if($scope.props.scopedSlotsCompiler==="augmented"){$setScopedSlotsParams("default",object)}}',
{
scopedSlotsCompiler: 'auto'
}
)
})
it('generate class binding', () => {
assertCodegen(
'
1
',
'1'
)
assertCodegen(
'2
',
'2'
)
assertCodegen(
'3
',
'3'
)
assertCodegen(
'4
',
'4'
)
assertCodegen(
'5
',
'5'
)
assertCodegen(
'52
',
'52'
)
assertCodegen(
'6
',
'6'
)
// assertCodegen(
// `6
`,
// `6`,
// `with(this){var c0=__get_class(computedClassObject,"container");$mp.data=Object.assign({},{$root:{c0:c0}})}`
// )
assertCodegen(
'7
',
'7'
)
assertCodegen(
'8
',
'8'
)
assertCodegen(
'9
',
'9'
)
assertCodegen(
'10
',
'10'
)
assertCodegen(
'hello world
',
'hello world'
)
})
it('generate attrs with mergeVirtualHostAttributes', () => {
assertCodegen(
'hello world',
'hello world',
'with(this){}',
{
mergeVirtualHostAttributes: true
}
)
assertCodegen(
'hello world',
'hello world',
'with(this){}',
{
mergeVirtualHostAttributes: true
}
)
assertCodegen(
'hello world',
'hello world',
'with(this){}',
{
mergeVirtualHostAttributes: true
}
)
assertCodegen(
'hello world',
'hello world',
'with(this){}',
{
mergeVirtualHostAttributes: true
}
)
})
it('generate getPhoneNumber', () => {
assertCodegen(
'',
''
)
})
it('generate events with v-on directive', () => {
assertCodegen(
'',
'',
'with(this){}'
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
})
})