style.spec.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { compile } from '../../../packages/weex-template-compiler'
  2. import { strToRegExp } from '../helpers/index'
  3. describe('compile style', () => {
  4. it('should be compiled', () => {
  5. const { render, staticRenderFns, errors } = compile(`<div style="a: x; b: y"></div>`)
  6. expect(render).not.toBeUndefined()
  7. expect(staticRenderFns).not.toBeUndefined()
  8. expect(staticRenderFns.length).toEqual(0)
  9. expect(render).toMatch(strToRegExp(`staticStyle:{a:"x",b:"y"}`))
  10. expect(errors).toEqual([])
  11. })
  12. it('should compile empty style value', () => {
  13. const { render, staticRenderFns, errors } = compile(`<div style=""></div>`)
  14. expect(render).not.toBeUndefined()
  15. expect(staticRenderFns).not.toBeUndefined()
  16. expect(staticRenderFns.length).toEqual(0)
  17. expect(render).toMatch(/[(^style|^staticStyle)]/)
  18. expect(errors).toEqual([])
  19. })
  20. it('should compile style value with trailing semicolon', () => {
  21. const { render, staticRenderFns, errors } = compile(`<div style="a: x; b: y;"></div>`)
  22. expect(render).not.toBeUndefined()
  23. expect(staticRenderFns).not.toBeUndefined()
  24. expect(staticRenderFns.length).toEqual(0)
  25. expect(render).toMatch(strToRegExp(`staticStyle:{a:"x",b:"y"}`))
  26. expect(errors).toEqual([])
  27. })
  28. it('should compile hyphenated style name & value', () => {
  29. const { render, staticRenderFns, errors } = compile(`<div style="-abc-def: x-y; abc-def: x-y"></div>`)
  30. expect(render).not.toBeUndefined()
  31. expect(staticRenderFns).not.toBeUndefined()
  32. expect(staticRenderFns.length).toEqual(0)
  33. expect(render).toMatch(strToRegExp(`staticStyle:{AbcDef:"x-y",abcDef:"x-y"}`))
  34. expect(errors).toEqual([])
  35. })
  36. it('should compile dynamic style', () => {
  37. const { render, staticRenderFns, errors } = compile(`<div style="a: x; b: {{y}}"></div>`)
  38. expect(render).not.toBeUndefined()
  39. expect(staticRenderFns).toEqual([])
  40. expect(render).toMatch(strToRegExp(`style:{a:"x",b:_s(y)}`))
  41. expect(errors).not.toBeUndefined()
  42. expect(errors.length).toEqual(1)
  43. expect(errors[0]).toMatch(strToRegExp(`b: {{y}}`))
  44. expect(errors[0]).toMatch(strToRegExp(`v-bind`))
  45. })
  46. it('should compile style binding of array', () => {
  47. const { render, staticRenderFns, errors } = compile(`<div v-bind:style="[a, b, c]"></div>`)
  48. expect(render).not.toBeUndefined()
  49. expect(staticRenderFns).toEqual([])
  50. expect(render).toMatch(strToRegExp(`style:[a, b, c]`))
  51. expect(errors).toEqual([])
  52. })
  53. it('should compile style binding of map', () => {
  54. const { render, staticRenderFns, errors } = compile(`<div v-bind:style="{ a: x, b: 'y' + z }"></div>`)
  55. expect(render).not.toBeUndefined()
  56. expect(staticRenderFns).toEqual([])
  57. expect(render).toMatch(strToRegExp(`style:{ a: x, b: 'y' + z }`))
  58. expect(errors).toEqual([])
  59. })
  60. it('should compile style binding of a variable', () => {
  61. const { render, staticRenderFns, errors } = compile(`<div v-bind:style="x"></div>`)
  62. expect(render).not.toBeUndefined()
  63. expect(staticRenderFns).toEqual([])
  64. expect(render).toMatch(strToRegExp(`style:x`))
  65. expect(errors).toEqual([])
  66. })
  67. it('should compile style binding by shorthand', () => {
  68. const { render, staticRenderFns, errors } = compile(`<div :style="[a, b, c]"></div>`)
  69. expect(render).not.toBeUndefined()
  70. expect(staticRenderFns).toEqual([])
  71. expect(render).toMatch(strToRegExp(`style:[a, b, c]`))
  72. expect(errors).toEqual([])
  73. })
  74. })