import { parse } from 'compiler/parser/index' import { optimize } from 'compiler/optimizer' import { generate } from 'compiler/codegen' import { isObject } from 'shared/util' import directives from 'web/compiler/directives/index' import { isReservedTag } from 'web/util/index' import { baseOptions } from 'web/compiler/index' function assertCodegen (template, generatedCode, ...args) { let staticRenderFnCodes = [] let generateOptions = baseOptions let proc = null let len = args.length while (len--) { const arg = args[len] if (Array.isArray(arg)) { staticRenderFnCodes = arg } else if (isObject(arg)) { generateOptions = arg } else if (typeof arg === 'function') { proc = arg } } const ast = parse(template, baseOptions) optimize(ast, baseOptions) proc && proc(ast) const res = generate(ast, generateOptions) expect(res.render).toBe(generatedCode) expect(res.staticRenderFns).toEqual(staticRenderFnCodes) } /* eslint-disable quotes */ describe('codegen', () => { it('generate directive', () => { assertCodegen( '
', `with(this){return _h(_e('p',{directives:[{name:"custom1",value:(value1),arg:"arg1",modifiers:{"modifire":true}},{name:"custom2",arg:"arg1"}]}))}` ) }) it('generate v-for directive', () => { assertCodegen( '
', `with(this){return (items)&&_l((items),function(item){return _h(_e('li',{key:item.uid}))})}` ) // iterator syntax assertCodegen( '', `with(this){return (items)&&_l((items),function(item,i){return _h(_e('li'))})}` ) assertCodegen( '', `with(this){return (items)&&_l((items),function(item,key,index){return _h(_e('li'))})}` ) }) it('generate v-if directive', () => { assertCodegen( 'hello
', `with(this){return (show)?_h(_e('p'),[_t("hello")]):void 0}` ) }) it('generate v-else directive', () => { assertCodegen( 'hello
world
hello world
', `with(this){return [_h(_e('p'),[_t("hello world")])]}` ) }) it('generate svg tag', () => { assertCodegen( '', `with(this){return _h(_e('svg',void 0,'svg'),[_h(_e('text',void 0,'svg'),[_t("hello world")])])}` ) }) it('generate MathML tag', () => { assertCodegen( ``, `with(this){return _h(_e('math',void 0,'math'),[_h(_e('matrix',void 0,'math'))])}` ) }) it('generate single slot', () => { assertCodegen( 'hello world
', `with(this){return _h(_e('p',{slot:"one"}),[_t("hello world")])}` ) }) it('generate class binding', () => { // static assertCodegen( 'hello world
', 'with(this){return _m(0)}', [`with(this){return _h(_e('p',{staticClass:"class1"}),[_t("hello world")])}`] ) // dynamic assertCodegen( 'hello world
', `with(this){return _h(_e('p',{class:class1}),[_t("hello world")])}` ) }) it('generate style binding', () => { assertCodegen( 'hello world
', `with(this){return _h(_e('p',{style:(error)}),[_t("hello world")])}` ) }) it('generate transition', () => { assertCodegen( 'hello world
', `with(this){return _h(_e('p',{transition:"expand"}),[_t("hello world")])}` ) }) it('generate dynamic transition with transition on appear', () => { assertCodegen( `hello world
`, `with(this){return _h(_e('p',{transition:{name:'expand',appear:true}}),[_t("hello world")])}` ) }) it('generate v-show directive', () => { assertCodegen( 'hello world
', `with(this){return _h(_e('p',{directives:[{name:"show",value:(shown)}],show:true}),[_t("hello world")])}` ) }) it('generate props with v-bind directive', () => { assertCodegen( '',
`with(this){return _h(_e('p',{props:{"value":msg}}))}`
)
})
it('generate attrs with v-bind directive', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{attrs:{"name":field1}}))}`
)
})
it('generate staticAttrs', () => {
assertCodegen(
'',
`with(this){return _m(0)}`,
[`with(this){return _h(_e('input',{staticAttrs:{"name":"field1"}}))}`]
)
})
it('generate events with v-on directive', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":onInput}}))}`
)
})
it('generate events with keycode', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!==13)return;onInput($event)}}}))}`
)
// multiple keycodes (delete)
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function($event){if($event.keyCode!==8&&$event.keyCode!==46)return;onInput($event)}}}))}`
)
})
it('generate events with modifiers', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function($event){$event.stopPropagation();onInput($event)}}}))}`
)
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function($event){$event.preventDefault();onInput($event)}}}))}`
)
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function($event){if($event.target !== $event.currentTarget)return;onInput($event)}}}))}`
)
})
it('generate events with multiple modifers', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return;onInput($event)}}}))}`
)
})
it('generate events with capture modifier', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"!input":function($event){onInput($event)}}}))}`
)
})
it('generate events with inline statement', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function($event){curent++}}}))}`
)
})
it('generate unhandled events', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":function(){}}}))}`,
ast => {
ast.events.input = undefined
}
)
})
it('generate multiple event handlers', () => {
assertCodegen(
'',
`with(this){return _h(_e('input',{on:{"input":[function($event){curent++},onInput]}}))}`
)
})
it('generate component', () => {
assertCodegen(
'
hello world
hello world
', `with(this){return (show)?_h(_e('p'),[_t("hello world")]):void 0}`, { isReservedTag } ) }) it('not specified isReservedTag option', () => { // this causes all tags to be treated as components, // thus all children are wrapped in thunks. assertCodegen( 'hello world