const compiler = require('../lib')
function assertCodegen (template, templateCode, renderCode = 'with(this){}', options = {}) {
const res = compiler.compile(template, {
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(
'',
''
)
})
it('generate scoped slot', () => {
assertCodegen(
'{{ bar.foo }}',
'{{foo}}'
)
assertCodegen(
'{{ bar.foo }}',
'{{foo}}'
)
})
it('generate named scoped slot', () => {
assertCodegen(
'{{ bar.foo }}',
'{{foo}}'
)
assertCodegen(
'{{ bar.foo }}',
'{{foo}}'
)
})
it('generate scoped slot with multiline v-if', () => {
assertCodegen(
'{{ bar.foo }}',
'{{foo}}'
)
assertCodegen(
'{{ bar.foo }}',
'{{foo}}'
)
})
it('generate scoped slot', () => {
assertCodegen(
'{{ user.lastName }}',
''
)
assertCodegen(
'{{ user.lastName }}',
''
)
})
it('generate scoped slot with scopedSlotsCompiler: auto', () => {
assertCodegen(
'{{item}}',
'{{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($mp.component.data.scopedSlotsCompiler==="augmented"){const $root=$mp.data.$root;$setScopedSlotsParams("default",{"item":item})}}',
{
scopedSlotsCompiler: 'auto'
}
)
assertCodegen(
'',
'',
'with(this){if($mp.component.data.scopedSlotsCompiler==="augmented"){const $root=$mp.data.$root;$setScopedSlotsParams("default",object)}}',
{
scopedSlotsCompiler: 'auto'
}
)
})
it('generate vue id', () => {
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
})
it('generate text trim', () => {
assertCodegen(
'\nN: {{title}}\n′',
'{{\'N: \'+title+"\\\\n′"}}'
)
assertCodegen(
'我是第一行1\n我的第二行',
'我是第一行1\n我的第二行'
)
assertCodegen(
'我是第一行2\n我的第二行1{{title}}',
'{{"我是第一行2\\\\n我的第二行1"+title}}'
)
assertCodegen(
`我是第一行3
我的第二行2{{title}}`,
'{{"我是第一行3\\\\n 我的第二行2"+title}}'
)
})
})