const compiler = require('../lib')
function assertCodegen (template, templateCode, renderCode = 'with(this){}', options = {}, mpOptions = {}) {
const res = compiler.compile(template, Object.assign({
resourcePath: 'test.wxml',
mp: Object.assign({
minified: true,
isTest: true,
platform: 'mp-weixin'
}, mpOptions)
}, options))
expect(res.template).toBe(templateCode)
expect(res.render).toBe(renderCode)
}
describe('mp:compiler-extra', () => {
it('generate mp filter ', () => {
assertCodegen(
/* eslint-disable no-template-curly-in-string */
'{{t.a}}{{t[\'a\']}}{{t.a(b)}}{{t[\'a\'](b)}}{{u.t.a(b)}}{{u.t.a}}',
'{{t.a+t[\'a\']+t.a(b)+t[\'a\'](b)+$root.g0+u.t.a}}',
'with(this){var g0=u.t.a(b);$mp.data=Object.assign({},{$root:{g0:g0}})}', {
filterModules: {
t: {},
a: {}
}
}
)
})
it('generate scopeId', () => {
assertCodegen(
'',
'',
undefined, {
scopeId: 'data-v-1'
}
)
assertCodegen(
'',
'',
undefined, {
scopeId: 'data-v-2'
}
)
assertCodegen(
'',
'',
undefined, {
scopeId: 'data-v-3'
}
)
assertCodegen(
'',
'',
undefined, {
scopeId: 'data-v-4'
}
)
assertCodegen(
'',
'',
undefined, {
scopeId: 'data-v-5'
}
)
assertCodegen(
'',
'',
undefined, {
scopeId: 'data-v-6'
}
)
// assertCodegen(
// '',
// ``,
// `with(this){var c0=__get_class(view,"data-v-6");$mp.data=Object.assign({},{$root:{c0:c0}})}`, {
// scopeId: 'data-v-6'
// }
// )
assertCodegen(
'',
'',
undefined, {
scopeId: 'data-v-7'
}
)
// assertCodegen(
// '',
// ``,
// `with(this){var c0=__get_class(view,"view data-v-7");$mp.data=Object.assign({},{$root:{c0:c0}})}`, {
// scopeId: 'data-v-7'
// }
// )
})
it('generate staticStyle upx and px', () => {
assertCodegen(
'text',
'text'
)
assertCodegen(
'text',
'text'
)
assertCodegen(
'text',
'text'
)
assertCodegen(
'text',
'text',
undefined,
undefined, {
transformPx: true
}
)
})
it('generate text trim', () => {
assertCodegen(
'text',
'text'
)
assertCodegen(
' text ',
'text'
)
assertCodegen(
'{{line_one_cn+\' \'}}',
'{{line_one_cn+\' \'}}'
)
assertCodegen(
'{{" "+line_one_cn}}',
'{{" "+line_one_cn}}'
)
assertCodegen(
'\nN: {{title}}\n′',
'{{\'N: \'+title+"\\n′"}}'
)
assertCodegen(
'我是第一行\n我的第二行',
'我是第一行\n我的第二行'
)
assertCodegen(
'我是第一行\n我的第二行1{{title}}',
'{{"我是第一行\\n我的第二行1"+title}}'
)
assertCodegen(
`我是第一行
我的第二行2{{title}}`,
'{{"我是第一行\\n我的第二行2"+title}}'
)
assertCodegen(
' text text ',
'text text'
)
assertCodegen(
'text {{text}} text',
'{{"text "+text+" text"}}'
)
// assertCodegen(
// 'text {{text}} \ntext',
// `{{"text " + text + " \ntext"}}`
// )
assertCodegen(
' text {{text}} 文本 ',
'{{\'text \'+text+\' 文本\'}}'
)
assertCodegen(
'{{text}} text text ',
'{{text+\' text text\'}}'
)
assertCodegen(
' {{text}} text text ',
'{{\'\'+text+\' text text\'}}'
)
assertCodegen(
'{{text}} text text {{text}}',
'{{text+" text text "+text}}'
)
assertCodegen(
' {{text}} text text {{text}} ',
'{{\'\'+text+" text text "+text+\'\'}}'
)
})
it('generate default slot', () => {
assertCodegen(
'text',
'text'
)
assertCodegen(
'text123213',
'text123213'
)
assertCodegen(
'text',
'text'
)
})
it('generate input value', () => {
assertCodegen(
'',
''
)
})
it('generate v-slot', () => {
assertCodegen(
'',
''
)
assertCodegen(
'fc默认',
'fc默认'
)
assertCodegen(
'text',
'text'
)
assertCodegen(
'text123213',
'text123213'
)
assertCodegen(
'',
''
)
assertCodegen(
`
Here might be a page title
A paragraph for the main content.
Here's some contact info
`,
'Here might be a page titleHere\'s some contact infoA paragraph for the main content.'
)
})
it('generate events inside v-for', () => {
assertCodegen(
'',
''
)
// TODO vue的数字 item 是从1,小程序是从0,后续考虑抹平差异
assertCodegen(
'1count++">',
'1',
'with(this){if(!_isMounted){e0=e=>count++}}'
)
assertCodegen(
'2count++">',
'2',
'with(this){if(!_isMounted){e0=e=>count++}}'
)
assertCodegen(
'3',
'3'
)
assertCodegen(
'33',
'33'
)
assertCodegen(
'4',
'4'
)
assertCodegen(
'5',
'5'
)
assertCodegen(
'6',
'6'
)
assertCodegen(
'7',
'7'
)
assertCodegen(
`
,
`,
','
)
assertCodegen(
'9',
'9'
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
`
`,
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
'',
'with(this){if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return $test.test(item,item.length)}}}'
)
assertCodegen(
'',
'',
'with(this){if(!_isMounted){e0=function($event,item){var _temp=arguments[arguments.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return $test.test(item,item.length,undefined)}}}'
)
assertCodegen(
'showEventInfo(event,item)">',
'',
'with(this){if(!_isMounted){e0=(event,item,...args)=>{var _temp=args[args.length-1].currentTarget.dataset,_temp2=_temp.eventParams||_temp["event-params"],item=_temp2.item;var _temp,_temp2;return showEventInfo(event,item)}}}'
)
})
it('generate class binding', () => {
assertCodegen(
'
1
',
'1'
)
assertCodegen(
'1
',
'1'
)
assertCodegen(
'2
',
'2'
)
assertCodegen(
'3
',
'3'
)
assertCodegen(
'4
',
'4'
)
assertCodegen(
'5
',
'5'
)
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'
)
})
it('generate style binding', () => {
assertCodegen(
'1
',
'1'
)
assertCodegen(
'1
',
'1'
)
assertCodegen(
'1
',
'1'
)
assertCodegen(
'2
',
'2'
)
assertCodegen(
'3
',
'3'
)
assertCodegen(
'4
',
'4'
)
assertCodegen(
'5
',
'5'
)
assertCodegen(
'6
',
'6',
'with(this){var s0=__get_style([baseStyles,overridingStyles]);$mp.data=Object.assign({},{$root:{s0:s0}})}'
)
assertCodegen(
'7
',
'7'
)
// assertCodegen(
// `7
`,
// `7`,
// `with(this){var s0=__get_style(styleObject);$mp.data=Object.assign({},{$root:{s0:s0}})}`
// )
assertCodegen(
'8
',
'8'
)
assertCodegen(
'9
',
'9'
)
assertCodegen(
'10
',
'10'
)
})
it('generate events with v-on directive on custom component', () => {
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
})
it('generate v-model directive on custom component', () => {
assertCodegen(
'1',
'1'
)
assertCodegen(
'2',
'2'
)
assertCodegen(
'3',
'3'
)
assertCodegen(
'4',
'4'
)
assertCodegen(
'4',
'4'
)
})
it('generate object property on custom component', () => {
assertCodegen(
'',
''
)
})
it('generate v-text directive', () => {
assertCodegen(
'',
'{{aaa1}}'
)
assertCodegen(
'',
'{{aaa1+1}}'
)
assertCodegen(
'',
'aaa2'
)
})
it('generate v-html directive', () => {
assertCodegen(
'',
''
)
})
it('generate v-bind directive with sync modifier', () => {
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
})
it('generate v-model directive with generic modifiers', () => {
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
})
it('generate v-for', () => {
assertCodegen(
'{{handle(item)}}',
'{{item.m0}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{handle(item)}}{{item.title}}',
'{{item.m0+item.$orig.title}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=handle(item);return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{handle(item1)}}{{item1.title}}',
'{{item1.m0+item1.$orig.title}}',
'with(this){var l1=__map(list,function(item,index){var $orig=__get_orig(item);var l0=__map(list1,function(item1,index1){var $orig=__get_orig(item1);var m0=handle(item1);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}'
)
assertCodegen(
'title: {{ section.title|prefix }}{{ sub_titles|prefix }}',
'{{"title: "+section.f0}}{{sub_titles.f1}}',
'with(this){var l1=__map(sections,function(section,index){var $orig=__get_orig(section);var f0=_f("prefix")(section.title);var l0=__map(section.sub_titles,function(sub_titles,_index){var $orig=__get_orig(sub_titles);var f1=_f("prefix")(sub_titles);return{$orig:$orig,f1:f1}});return{$orig:$orig,f0:f0,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}'
)
assertCodegen(
'{{aaa.item.id | test | test1}}',
'{{$root.f0}}',
'with(this){var f0=_f("test1")(_f("test")(aaa.item.id));$mp.data=Object.assign({},{$root:{f0:f0}})}'
)
assertCodegen(
'{{item.item.id | test | test1}}',
'{{item.f0}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var f0=_f("test1")(_f("test")(item.item.id));return{$orig:$orig,f0:f0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{ item.split(\'\').join(\' \') }}',
'{{item.g0}}',
'with(this){var l0=__map(list,function(item,i){var $orig=__get_orig(item);var g0=item.split("").join(" ");return{$orig:$orig,g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
`
{{formatIt(item2.id)}}
`,
'{{\'\'+item2.m0+\'\'}}',
'with(this){var l1=__map(tabList,function(item,index){var $orig=__get_orig(item);var l0=__map(list[item.key],function(item2,index2){var $orig=__get_orig(item2);var m0=formatIt(item2.id);return{$orig:$orig,m0:m0}});return{$orig:$orig,l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}'
)
assertCodegen(
'{{item}}',
'{{item}}'
)
assertCodegen(
'{{item}}',
'{{item}}',
'with(this){var l0={"x-x":"x"};$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{item}}',
'{{item}}',
'with(this){var l0=getList(test);$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{item}}',
'{{item}}',
'with(this){var m0=get(test);$mp.data=Object.assign({},{$root:{m0:m0}})}'
)
})
it('generate TemplateLiteral ', () => {
assertCodegen(
/* eslint-disable no-template-curly-in-string */
'',
''
)
assertCodegen(
/* eslint-disable no-template-curly-in-string */
'',
''
)
})
it('generate event ', () => {
assertCodegen(
'',
''
)
assertCodegen(
'{{item.title}}',
'{{item.title}}'
)
assertCodegen(
'1',
'1'
)
assertCodegen(
'2',
'2'
)
assertCodegen(
'3',
'3'
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'{{item.title}}',
'{{item.title}}'
)
assertCodegen(
'{{item.title}}',
'{{item.title}}'
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
assertCodegen(
'{{ item }}',
'{{item}}'
)
})
it('generate bool attr', () => {
assertCodegen(
'',
''
)
assertCodegen(
'',
''
)
})
it('generate v-if', () => {
assertCodegen(
'{{getValue(key)}}',
'{{$root.m0}}',
'with(this){var m0=show?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0}})}'
)
assertCodegen(
'{{getValue(key)}}',
'{{$root.m1}}',
'with(this){var m0=getValue(key);var m1=m0?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1}})}'
)
assertCodegen(
'{{getValue(key)}}',
'{{$root.m2}}',
'with(this){var m0=test1(key);var m1=test2(key);var m2=m0&&m1?getValue(key):null;$mp.data=Object.assign({},{$root:{m0:m0,m1:m1,m2:m2}})}'
)
assertCodegen(
'{{getValue(item)}}',
'{{item.m0}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{getValue(item)}}',
'{{item.m0}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{getValue(item)}}',
'{{item.m0}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length>0?getValue(item):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{test(\'item\')}}',
'{{item.m0}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=item.length>0?test("item"):null;return{$orig:$orig,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{test(\'item\')}}',
'{{item.m1}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=test(item);var m1=m0?test("item"):null;return{$orig:$orig,m0:m0,m1:m1}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{test(\'item\')}}',
'{{item.m2}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var m0=test(item.id);var m1=m0?test(item.type):null;var m2=m0&&m1?test("item"):null;return{$orig:$orig,m0:m0,m1:m1,m2:m2}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'{{test(item.list)}}',
'{{item.m0}}',
'with(this){var l0=__map(list,function(item,index){var $orig=__get_orig(item);var g0=Object.values(item.list);var m0=g0.length?test(item.list):null;return{$orig:$orig,g0:g0,m0:m0}});$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
})
})