const compiler = require('../lib') function assertCodegen (template, generatedCode, ...args) { const compiled = compiler.compile(template, { mp: { platform: 'app-plus' }, view: true }) expect(compiled.render).toBe(generatedCode) } /* eslint-disable quotes */ describe('codegen', () => { it('generate directive', () => { assertCodegen( '
', `with(this){return _c('v-uni-view',{directives:[{name:"custom1",rawName:"v-custom1:arg1.modifier",value:($r['0']['v-custom1']),expression:"$r['0']['v-custom1']",arg:"arg1",modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}],attrs:{"_i":0}})}` ) // extra assertCodegen( '', `with(this){return _c('v-uni-view',{directives:[{name:"custom1",rawName:"v-custom1:[arg1].modifier",value:($r['0']['v-custom1']),expression:"$r['0']['v-custom1']",arg:$r['0']['v-custom1-arg'],modifiers:{"modifier":true}},{name:"custom2",rawName:"v-custom2"}],attrs:{"_i":0}})}` ) }) it('generate filters', () => { assertCodegen( 'hello
', `with(this){return ($r['0']['v-if'])?_c('v-uni-view',{attrs:{"_i":0}},[_v("hello")]):_e()}` ) }) it('generate v-else directive', () => { assertCodegen( 'hello
world
hello
world
hello
world
bye
hello
world
elseif
bye
{{hello}}
hello world
', `with(this){return _c('v-uni-view',{attrs:{"slot":"one","_i":0},slot:"one"},[_v("hello world")])}` ) }) it('generate scoped slot', () => { assertCodegen( 'hello world
', `with(this){return _c('v-uni-view',{staticClass:"class1",attrs:{"_i":0}},[_v("hello world")])}` ) // dynamic assertCodegen( 'hello world
', `with(this){return _c('v-uni-view',{class:$r['0']['c'],attrs:{"_i":0}},[_v("hello world")])}` ) }) it('generate style binding', () => { assertCodegen( 'hello world
', `with(this){return _c('v-uni-view',{style:($r['0']['s']),attrs:{"_i":0}},[_v("hello world")])}` ) }) it('generate v-show directive', () => { assertCodegen( 'hello world
', `with(this){return _c('v-uni-view',{directives:[{name:"show",rawName:"v-show",value:($r['0']['v-show']),expression:"$r['0']['v-show']"}],attrs:{"_i":0}},[_v("hello world")])}` ) }) it('generate DOM props with v-bind directive', () => { // input + value assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},domProps:{"value":$r['0']['a-value']}})}` ) // non input assertCodegen( '', `with(this){return _c('v-uni-view',{attrs:{"value":$r['0']['a-value'],"_i":0}})}` ) }) it('generate attrs with v-bind directive', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"name":$r['0']['a-name'],"_i":0}})}` ) }) it('generate static attrs', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"name":"field1","_i":0}})}` ) }) it('generate events with v-on directive', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) }) it('generate events with method call', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // empty arguments assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // without semicolon assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // multiple args assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // expression in args assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // tricky symbols in args assertCodegen( ``, `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // function name including a `function` part (#9920) // 2.6.10 暂未修复此 bug // assertCodegen( // '', // `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return functionName()}}})}` // ) }) it('generate events with multiple statements', () => { // normal function assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // function with multiple args assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) }) it('generate events with keycode', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"enter":true})}}})}` ) // multiple keycodes (delete) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"delete":true})}}})}` ) // multiple keycodes (esc) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"esc":true})}}})}` ) // multiple keycodes (space) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"space":true})}}})}` ) // multiple keycodes (chained) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"keydown":function($event){return $handleViewEvent($event,{"enter":true,"delete":true})}}})}` ) // number keycode assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"13":true})}}})}` ) // custom keycode assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"custom":true})}}})}` ) }) it('generate events with generic modifiers', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"stop":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"prevent":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"self":true})}}})}` ) }) // GitHub Issues #5146 it('generate events with generic modifiers and keycode correct order', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"keydown":function($event){return $handleViewEvent($event,{"enter":true,"prevent":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"keydown":function($event){return $handleViewEvent($event,{"enter":true,"stop":true})}}})}` ) }) it('generate events with mouse event modifiers', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"click":function($event){return $handleViewEvent($event,{"ctrl":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"click":function($event){return $handleViewEvent($event,{"shift":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"click":function($event){return $handleViewEvent($event,{"alt":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"click":function($event){return $handleViewEvent($event,{"meta":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"click":function($event){return $handleViewEvent($event,{"exact":true})}}})}` ) assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"click":function($event){return $handleViewEvent($event,{"ctrl":true,"exact":true})}}})}` ) }) it('generate events with multiple modifiers', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"stop":true,"prevent":true,"self":true})}}})}` ) }) it('generate events with capture modifier', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"!input":function($event){return $handleViewEvent($event,{"capture":true})}}})}` ) }) it('generate events with once modifier', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"~input":function($event){return $handleViewEvent($event,{"once":true})}}})}` ) }) it('generate events with capture and once modifier', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"~!input":function($event){return $handleViewEvent($event,{"once":true,"capture":true})}}})}` ) }) it('generate events with once and capture modifier', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"~!input":function($event){return $handleViewEvent($event,{"once":true,"capture":true})}}})}` ) }) it('generate events with inline statement', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) }) it('generate events with inline function expression', () => { // normal function assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // normal named function assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // arrow with no args assertCodegen( 'current++">', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // arrow with parens, single arg assertCodegen( ' current++">', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // arrow with parens, multi args assertCodegen( ' current++">', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // arrow with destructuring assertCodegen( ' current++">', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // arrow single arg no parens assertCodegen( 'current++">', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) // with modifiers assertCodegen( `current++">`, `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"keyup":function($event){return $handleViewEvent($event,{"enter":true})}}})}` ) }) // #3893 it('should not treat handler with unexpected whitespace as inline statement', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}` ) }) it('generate unhandled events', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event)}}})}`, ast => { ast.events.input = undefined } ) }) it('generate multiple event handlers', () => { assertCodegen( '', `with(this){return _c('v-uni-input',{attrs:{"_i":0},on:{"input":function($event){return $handleViewEvent($event,{"stop":true})}}})}` ) }) it('generate component', () => { assertCodegen( 'hello world
{{msg}}
{{msg}}
hello world
', `with(this){return ($r['0']['v-if'])?_c('v-uni-view',{attrs:{"_i":0}},[_v("hello world")]):_e()}`, { // isReservedTag } ) }) // #9142 it('should compile single v-for component inside template', () => { assertCodegen( `