compile.spec.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { compile } from '../../../packages/weex-template-compiler'
  2. import { strToRegExp } from '../helpers/index'
  3. describe('compile basic', () => {
  4. it('should be compiled', () => {
  5. const { render, staticRenderFns, errors } = compile(`<div>{{hi}}</div>`)
  6. expect(render).toEqual(`with(this){return _c('div',[_v(_s(hi))])}`)
  7. expect(staticRenderFns.length).toBe(0)
  8. expect(errors).toEqual([])
  9. })
  10. it('should compile data bindings', () => {
  11. const { render, staticRenderFns, errors } = compile(`<div :a="b"></div>`)
  12. expect(render).toEqual(`with(this){return _c('div',{attrs:{"a":b}})}`)
  13. expect(staticRenderFns).toEqual([])
  14. expect(errors).toEqual([])
  15. })
  16. it('should compile event bindings', () => {
  17. const { render, staticRenderFns, errors } = compile(`<div @click="x"></div>`)
  18. expect(render).toEqual(`with(this){return _c('div',{on:{"click":x}})}`)
  19. expect(staticRenderFns).toEqual([])
  20. expect(errors).toEqual([])
  21. })
  22. it('should compile data bindings with children', () => {
  23. const { render, staticRenderFns, errors } = compile(`<foo :a="b"><text>Hello</text></foo>`)
  24. expect(render).toEqual(`with(this){return _c('foo',{attrs:{"a":b}},[_c('text',[_v("Hello")])])}`)
  25. expect(staticRenderFns).toEqual([])
  26. expect(errors).toEqual([])
  27. })
  28. it('should compile unary tag', () => {
  29. const inputCase = compile(`<div><input><text>abc</text></div>`)
  30. expect(inputCase.render).toMatch(strToRegExp(`return _m(0)`))
  31. expect(inputCase.staticRenderFns).toMatch(strToRegExp(`_c('div',[_c('input'),_c('text',[_v("abc")])])`))
  32. expect(inputCase.errors).toEqual([])
  33. const imageCase = compile(`<div><image src="path"><text>abc</text></div>`)
  34. expect(imageCase.render).toMatch(strToRegExp(`return _m(0)`))
  35. expect(imageCase.staticRenderFns).toMatch(strToRegExp(`_c('div',[_c('image',{attrs:{"src":"path"}}),_c('text',[_v("abc")])])`))
  36. expect(imageCase.errors).toEqual([])
  37. const complexCase = compile(`
  38. <div>
  39. <image src="path">
  40. <image></image>
  41. <div>
  42. <embed>
  43. <text>start</text>
  44. <input type="text">
  45. <input type="url" />
  46. <text>end</text>
  47. </div>
  48. </div>
  49. `)
  50. expect(complexCase.render).toMatch(strToRegExp(`return _m(0)`))
  51. expect(complexCase.staticRenderFns).toMatch(strToRegExp(`_c('image',{attrs:{"src":"path"}}),_c('image'),_c('div'`))
  52. expect(complexCase.staticRenderFns).toMatch(strToRegExp(`_c('div',[_c('embed'),_c('text',[_v("start")]),_c('input',{attrs:{"type":"text"}}),_c('input',{attrs:{"type":"url"}}),_c('text',[_v("end")])]`))
  53. expect(complexCase.errors).toEqual([])
  54. })
  55. it('should compile more complex situation', () => {
  56. // from examples of https://github.com/alibaba/weex
  57. const { render, staticRenderFns, errors } = compile(`
  58. <refresh class="refresh" @refresh="handleRefresh" :display="displayRefresh"
  59. style="flex-direction:row;">
  60. <loading-indicator></loading-indicator>
  61. <text style="margin-left:36px;color:#eee;">Load more...</text>
  62. </refresh>
  63. `)
  64. expect(render).toEqual(`with(this){return _c('refresh',{staticClass:["refresh"],staticStyle:{flexDirection:"row"},attrs:{"display":displayRefresh},on:{"refresh":handleRefresh}},[_c('loading-indicator'),_c('text',{staticStyle:{marginLeft:"36px",color:"#eee"}},[_v("Load more...")])])}`)
  65. expect(staticRenderFns).toEqual([])
  66. expect(errors).toEqual([])
  67. })
  68. })