class.spec.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { compile } from '../../../packages/weex-template-compiler'
  2. import { strToRegExp } from '../helpers/index'
  3. describe('compile class', () => {
  4. it('should be compiled', () => {
  5. const { render, staticRenderFns, errors } = compile(`<div class="a b c"></div>`)
  6. expect(render).not.toBeUndefined()
  7. expect(staticRenderFns).not.toBeUndefined()
  8. expect(staticRenderFns.length).toEqual(0)
  9. expect(render).toMatch(strToRegExp(`staticClass:["a","b","c"]`))
  10. expect(errors).toEqual([])
  11. })
  12. it('should compile dynamic class', () => {
  13. const { render, staticRenderFns, errors } = compile(`<div class="a {{b}} c"></div>`)
  14. expect(render).not.toBeUndefined()
  15. expect(staticRenderFns).toEqual([])
  16. expect(render).toMatch(strToRegExp(`class:["a",_s(b),"c"]`))
  17. expect(errors).not.toBeUndefined()
  18. expect(errors.length).toEqual(1)
  19. expect(errors[0]).toMatch(strToRegExp(`a {{b}} c`))
  20. expect(errors[0]).toMatch(strToRegExp(`v-bind`))
  21. })
  22. it('should compile class binding of array', () => {
  23. const { render, staticRenderFns, errors } = compile(`<div v-bind:class="['a', 'b', c]"></div>`)
  24. expect(render).not.toBeUndefined()
  25. expect(staticRenderFns).toEqual([])
  26. expect(render).toMatch(strToRegExp(`class:['a', 'b', c]`))
  27. expect(errors).toEqual([])
  28. })
  29. it('should compile class binding of map', () => {
  30. const { render, staticRenderFns, errors } = compile(`<div v-bind:class="{ a: true, b: x }"></div>`)
  31. expect(render).not.toBeUndefined()
  32. expect(staticRenderFns).toEqual([])
  33. expect(render).toMatch(strToRegExp(`class:{ a: true, b: x }`))
  34. expect(errors).toEqual([])
  35. })
  36. it('should compile class binding of a variable', () => {
  37. const { render, staticRenderFns, errors } = compile(`<div v-bind:class="x"></div>`)
  38. expect(render).not.toBeUndefined()
  39. expect(staticRenderFns).toEqual([])
  40. expect(render).toMatch(strToRegExp(`class:x`))
  41. expect(errors).toEqual([])
  42. })
  43. it('should compile class binding by shorthand', () => {
  44. const { render, staticRenderFns, errors } = compile(`<div :class="['a', 'b', c]"></div>`)
  45. expect(render).not.toBeUndefined()
  46. expect(staticRenderFns).toEqual([])
  47. expect(render).toMatch(strToRegExp(`class:['a', 'b', c]`))
  48. expect(errors).toEqual([])
  49. })
  50. })