class.spec.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import {
  2. compileAndStringify,
  3. prepareRuntime,
  4. resetRuntime,
  5. createInstance
  6. } from '../helpers/index'
  7. describe('generate class', () => {
  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 class="a b c">Hello World</text>
  20. </div>
  21. `)
  22. const instance = createInstance(runtime, `
  23. new Vue({
  24. render: ${render},
  25. staticRenderFns: ${staticRenderFns},
  26. style: {
  27. a: {
  28. fontSize: '100'
  29. },
  30. b: {
  31. color: '#ff0000'
  32. },
  33. c: {
  34. fontWeight: 'bold'
  35. }
  36. },
  37. el: 'body'
  38. })
  39. `)
  40. expect(instance.getRealRoot()).toEqual({
  41. type: 'div',
  42. children: [
  43. { type: 'text', style: { fontSize: '100', color: '#ff0000', fontWeight: 'bold' }, attr: { value: 'Hello World' }}
  44. ]
  45. })
  46. })
  47. it('should be updated', (done) => {
  48. const { render, staticRenderFns } = compileAndStringify(`
  49. <div>
  50. <text :class="['a', x]" @click="foo">Hello World</text>
  51. </div>
  52. `)
  53. const instance = createInstance(runtime, `
  54. new Vue({
  55. data: {
  56. x: 'b'
  57. },
  58. render: ${render},
  59. staticRenderFns: ${staticRenderFns},
  60. style: {
  61. a: {
  62. fontSize: '100'
  63. },
  64. b: {
  65. color: '#ff0000'
  66. },
  67. c: {
  68. fontWeight: 'bold'
  69. },
  70. d: {
  71. color: '#0000ff',
  72. fontWeight: 'bold'
  73. }
  74. },
  75. methods: {
  76. foo: function () {
  77. this.x = 'd'
  78. }
  79. },
  80. el: 'body'
  81. })
  82. `)
  83. expect(instance.getRealRoot()).toEqual({
  84. type: 'div',
  85. children: [
  86. {
  87. type: 'text',
  88. event: ['click'],
  89. style: { fontSize: '100', color: '#ff0000' },
  90. attr: { value: 'Hello World' }
  91. }
  92. ]
  93. })
  94. instance.$fireEvent(instance.doc.body.children[0].ref, 'click', {})
  95. setTimeout(() => {
  96. expect(instance.getRealRoot()).toEqual({
  97. type: 'div',
  98. children: [
  99. {
  100. type: 'text',
  101. event: ['click'],
  102. style: { fontSize: '100', color: '#0000ff', fontWeight: 'bold' },
  103. attr: { value: 'Hello World' }
  104. }
  105. ]
  106. })
  107. done()
  108. })
  109. })
  110. it('should be applied in order', (done) => {
  111. const { render, staticRenderFns } = compileAndStringify(`
  112. <div>
  113. <text :class="arr" @click="foo">Hello World</text>
  114. </div>
  115. `)
  116. const instance = createInstance(runtime, `
  117. new Vue({
  118. data: {
  119. arr: ['b', 'a']
  120. },
  121. render: ${render},
  122. staticRenderFns: ${staticRenderFns},
  123. style: {
  124. a: {
  125. color: '#ff0000'
  126. },
  127. b: {
  128. color: '#00ff00'
  129. },
  130. c: {
  131. color: '#0000ff'
  132. }
  133. },
  134. methods: {
  135. foo: function () {
  136. this.arr.push('c')
  137. }
  138. },
  139. el: 'body'
  140. })
  141. `)
  142. expect(instance.getRealRoot()).toEqual({
  143. type: 'div',
  144. children: [
  145. {
  146. type: 'text',
  147. event: ['click'],
  148. style: { color: '#ff0000' },
  149. attr: { value: 'Hello World' }
  150. }
  151. ]
  152. })
  153. instance.$fireEvent(instance.doc.body.children[0].ref, 'click', {})
  154. setTimeout(() => {
  155. expect(instance.getRealRoot()).toEqual({
  156. type: 'div',
  157. children: [
  158. {
  159. type: 'text',
  160. event: ['click'],
  161. style: { color: '#0000ff' },
  162. attr: { value: 'Hello World' }
  163. }
  164. ]
  165. })
  166. done()
  167. })
  168. })
  169. it('should be cleared', (done) => {
  170. const { render, staticRenderFns } = compileAndStringify(`
  171. <div>
  172. <text :class="['a', x]" @click="foo">Hello World</text>
  173. </div>
  174. `)
  175. const instance = createInstance(runtime, `
  176. new Vue({
  177. data: {
  178. x: 'b'
  179. },
  180. render: ${render},
  181. staticRenderFns: ${staticRenderFns},
  182. style: {
  183. a: {
  184. fontSize: '100'
  185. },
  186. b: {
  187. color: '#ff0000'
  188. },
  189. c: {
  190. fontWeight: 'bold'
  191. }
  192. },
  193. methods: {
  194. foo: function () {
  195. this.x = 'c'
  196. }
  197. },
  198. el: 'body'
  199. })
  200. `)
  201. expect(instance.getRealRoot()).toEqual({
  202. type: 'div',
  203. children: [
  204. {
  205. type: 'text',
  206. event: ['click'],
  207. style: { fontSize: '100', color: '#ff0000' },
  208. attr: { value: 'Hello World' }
  209. }
  210. ]
  211. })
  212. instance.$fireEvent(instance.doc.body.children[0].ref, 'click', {})
  213. setTimeout(() => {
  214. expect(instance.getRealRoot()).toEqual({
  215. type: 'div',
  216. children: [
  217. {
  218. type: 'text',
  219. event: ['click'],
  220. style: { fontSize: '100', color: '', fontWeight: 'bold' },
  221. attr: { value: 'Hello World' }
  222. }
  223. ]
  224. })
  225. done()
  226. })
  227. })
  228. })