compile.spec.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { compile } from '../../../packages/weex-template-compiler'
  2. describe('compile basic', () => {
  3. it('should be compiled', () => {
  4. const { render, staticRenderFns, errors } = compile(`<div>{{hi}}</div>`)
  5. expect(render).toEqual(`with(this){return _c('div',[_v(_s(hi))])}`)
  6. expect(staticRenderFns.length).toBe(0)
  7. expect(errors).toEqual([])
  8. })
  9. it('should compile data bindings', () => {
  10. const { render, staticRenderFns, errors } = compile(`<div :a="b"></div>`)
  11. expect(render).toEqual(`with(this){return _c('div',{attrs:{"a":b}})}`)
  12. expect(staticRenderFns).toEqual([])
  13. expect(errors).toEqual([])
  14. })
  15. it('should compile event bindings', () => {
  16. const { render, staticRenderFns, errors } = compile(`<div @click="x"></div>`)
  17. expect(render).toEqual(`with(this){return _c('div',{on:{"click":x}})}`)
  18. expect(staticRenderFns).toEqual([])
  19. expect(errors).toEqual([])
  20. })
  21. it('should compile data bindings with children', () => {
  22. const { render, staticRenderFns, errors } = compile(`<foo :a="b"><text>Hello</text></foo>`)
  23. expect(render).toEqual(`with(this){return _c('foo',{attrs:{"a":b}},[_c('text',[_v("Hello")])])}`)
  24. expect(staticRenderFns).toEqual([])
  25. expect(errors).toEqual([])
  26. })
  27. it('should compile more complex situation', () => {
  28. // from examples of https://github.com/alibaba/weex
  29. const { render, staticRenderFns, errors } = compile(`
  30. <refresh class="refresh" @refresh="handleRefresh" :display="displayRefresh"
  31. style="flex-direction:row;">
  32. <loading-indicator></loading-indicator>
  33. <text style="margin-left:36px;color:#eee;">Load more...</text>
  34. </refresh>
  35. `)
  36. 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...")])],1)}`)
  37. expect(staticRenderFns).toEqual([])
  38. expect(errors).toEqual([])
  39. })
  40. })