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(
`
`,
``
)
})
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 m0=handle(item);return{$orig:__get_orig(item),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 m0=handle(item);return{$orig:__get_orig(item),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 l0=__map(list1,function(item1,index1){var m0=handle(item1);return{$orig:__get_orig(item1),m0:m0}});return{$orig:__get_orig(item),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 f0=_f("prefix")(section.title);var l0=__map(section.sub_titles,function(sub_titles,_index){var f1=_f("prefix")(sub_titles);return{$orig:__get_orig(sub_titles),f1:f1}});return{$orig:__get_orig(section),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 f0=_f("test1")(_f("test")(item.item.id));return{$orig:__get_orig(item),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 g0=item.split("").join(" ");return{$orig:__get_orig(item),g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}`
)
})
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}}`
)
})
})