2
0

style.spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {
  2. compileAndStringify,
  3. prepareRuntime,
  4. resetRuntime,
  5. createInstance
  6. } from '../helpers/index'
  7. describe('generate style', () => {
  8. let runtime
  9. beforeAll(() => {
  10. runtime = prepareRuntime()
  11. })
  12. afterAll(() => {
  13. resetRuntime()
  14. runtime = null
  15. })
  16. it('should be generated', () => {
  17. const { render, staticRenderFns } = compileAndStringify(`
  18. <div>
  19. <text style="font-size: 100">Hello World</text>
  20. </div>
  21. `)
  22. const instance = createInstance(runtime, `
  23. new Vue({
  24. render: ${render},
  25. staticRenderFns: ${staticRenderFns},
  26. el: 'body'
  27. })
  28. `)
  29. expect(instance.getRealRoot()).toEqual({
  30. type: 'div',
  31. children: [
  32. { type: 'text', style: { fontSize: '100' }, attr: { value: 'Hello World' }}
  33. ]
  34. })
  35. })
  36. it('should be generated by array binding', (done) => {
  37. const { render, staticRenderFns } = compileAndStringify(`
  38. <div>
  39. <text :style="[x, y]" @click="foo">Hello {{z}}</text>
  40. </div>
  41. `)
  42. const instance = createInstance(runtime, `
  43. new Vue({
  44. data: {
  45. x: { fontSize: 100, color: '#00ff00' },
  46. y: { color: '#ff0000', fontWeight: 'bold' },
  47. z: 'World'
  48. },
  49. methods: {
  50. foo: function () {
  51. this.x.fontSize = 200
  52. this.x.color = '#0000ff'
  53. Vue.delete(this.y, 'fontWeight')
  54. this.z = 'Weex'
  55. }
  56. },
  57. render: ${render},
  58. staticRenderFns: ${staticRenderFns},
  59. el: 'body'
  60. })
  61. `)
  62. expect(instance.getRealRoot()).toEqual({
  63. type: 'div',
  64. children: [
  65. {
  66. type: 'text',
  67. event: ['click'],
  68. style: { fontSize: 100, color: '#ff0000', fontWeight: 'bold' },
  69. attr: { value: 'Hello World' }
  70. }
  71. ]
  72. })
  73. instance.$fireEvent(instance.doc.body.children[0].ref, 'click', {})
  74. setTimeout(() => {
  75. expect(instance.getRealRoot()).toEqual({
  76. type: 'div',
  77. children: [
  78. {
  79. type: 'text',
  80. event: ['click'],
  81. style: { fontSize: 200, color: '#ff0000', fontWeight: '' },
  82. attr: { value: 'Hello Weex' }
  83. }
  84. ]
  85. })
  86. done()
  87. })
  88. })
  89. it('should be generated by map binding', (done) => {
  90. const { render, staticRenderFns } = compileAndStringify(`
  91. <div>
  92. <text :style="{ fontSize: x, color: '#00ff00' }" @click="foo">Hello</text>
  93. <text :style="y">{{z}}</text>
  94. </div>
  95. `)
  96. const instance = createInstance(runtime, `
  97. new Vue({
  98. data: {
  99. x: 100,
  100. y: { color: '#ff0000', fontWeight: 'bold' },
  101. z: 'World'
  102. },
  103. methods: {
  104. foo: function () {
  105. this.x = 200
  106. this.y.color = '#0000ff'
  107. Vue.delete(this.y, 'fontWeight')
  108. this.z = 'Weex'
  109. }
  110. },
  111. render: ${render},
  112. staticRenderFns: ${staticRenderFns},
  113. el: 'body'
  114. })
  115. `)
  116. expect(instance.getRealRoot()).toEqual({
  117. type: 'div',
  118. children: [
  119. {
  120. type: 'text',
  121. event: ['click'],
  122. style: { fontSize: 100, color: '#00ff00' },
  123. attr: { value: 'Hello' }
  124. },
  125. {
  126. type: 'text',
  127. style: { color: '#ff0000', fontWeight: 'bold' },
  128. attr: { value: 'World' }
  129. }
  130. ]
  131. })
  132. instance.$fireEvent(instance.doc.body.children[0].ref, 'click', {})
  133. setTimeout(() => {
  134. expect(instance.getRealRoot()).toEqual({
  135. type: 'div',
  136. children: [
  137. {
  138. type: 'text',
  139. event: ['click'],
  140. style: { fontSize: 200, color: '#00ff00' },
  141. attr: { value: 'Hello' }
  142. },
  143. {
  144. type: 'text',
  145. style: { color: '#0000ff', fontWeight: '' },
  146. attr: { value: 'Weex' }
  147. }
  148. ]
  149. })
  150. done()
  151. })
  152. })
  153. })