class.spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { getRoot, fireEvent, compileAndExecute } from '../helpers/index'
  2. describe('generate class', () => {
  3. it('should be generated', () => {
  4. compileAndExecute(`
  5. <div>
  6. <text class="a b c">Hello World</text>
  7. </div>
  8. `, `
  9. style: {
  10. a: { fontSize: '100' },
  11. b: { color: '#ff0000' },
  12. c: { fontWeight: 'bold' }
  13. }
  14. `).then(instance => {
  15. expect(getRoot(instance)).toEqual({
  16. type: 'div',
  17. children: [{
  18. type: 'text',
  19. classList: ['a', 'b', 'c'],
  20. attr: { value: 'Hello World' }
  21. }]
  22. })
  23. })
  24. })
  25. it('should be updated', (done) => {
  26. compileAndExecute(`
  27. <div @click="foo">
  28. <text :class="['a', x]">Hello World</text>
  29. </div>
  30. `, `
  31. data: { x: 'b' },
  32. style: {
  33. a: { fontSize: '100' },
  34. b: { color: '#ff0000' },
  35. c: { fontWeight: 'bold' },
  36. d: {
  37. color: '#0000ff',
  38. fontWeight: 'bold'
  39. }
  40. },
  41. methods: {
  42. foo: function () {
  43. this.x = 'd'
  44. }
  45. }
  46. `).then(instance => {
  47. expect(getRoot(instance)).toEqual({
  48. type: 'div',
  49. event: ['click'],
  50. children: [{
  51. type: 'text',
  52. classList: ['a', 'b'],
  53. attr: { value: 'Hello World' }
  54. }]
  55. })
  56. fireEvent(instance, '_root', 'click')
  57. return instance
  58. }).then(instance => {
  59. expect(getRoot(instance)).toEqual({
  60. type: 'div',
  61. event: ['click'],
  62. children: [{
  63. type: 'text',
  64. classList: ['a', 'd'],
  65. attr: { value: 'Hello World' }
  66. }]
  67. })
  68. done()
  69. })
  70. })
  71. it('should be applied in order', (done) => {
  72. compileAndExecute(`
  73. <div @click="foo">
  74. <text :class="arr">Hello World</text>
  75. </div>
  76. `, `
  77. data: {
  78. arr: ['b', 'a']
  79. },
  80. style: {
  81. a: { color: '#ff0000' },
  82. b: { color: '#00ff00' },
  83. c: { color: '#0000ff' }
  84. },
  85. methods: {
  86. foo: function () {
  87. this.arr.push('c')
  88. }
  89. }
  90. `).then(instance => {
  91. expect(getRoot(instance)).toEqual({
  92. type: 'div',
  93. event: ['click'],
  94. children: [{
  95. type: 'text',
  96. classList: ['b', 'a'],
  97. attr: { value: 'Hello World' }
  98. }]
  99. })
  100. fireEvent(instance, '_root', 'click')
  101. return instance
  102. }).then(instance => {
  103. expect(getRoot(instance)).toEqual({
  104. type: 'div',
  105. event: ['click'],
  106. children: [{
  107. type: 'text',
  108. classList: ['b', 'a', 'c'],
  109. attr: { value: 'Hello World' }
  110. }]
  111. })
  112. done()
  113. })
  114. })
  115. it('should be cleared', (done) => {
  116. compileAndExecute(`
  117. <div @click="foo">
  118. <text :class="['a', x]">Hello World</text>
  119. </div>
  120. `, `
  121. data: { x: 'b' },
  122. style: {
  123. a: { fontSize: '100' },
  124. b: { color: '#ff0000' },
  125. c: { fontWeight: 'bold' }
  126. },
  127. methods: {
  128. foo: function () {
  129. this.x = 'c'
  130. }
  131. }
  132. `).then(instance => {
  133. expect(getRoot(instance)).toEqual({
  134. type: 'div',
  135. event: ['click'],
  136. children: [{
  137. type: 'text',
  138. classList: ['a', 'b'],
  139. attr: { value: 'Hello World' }
  140. }]
  141. })
  142. fireEvent(instance, '_root', 'click')
  143. return instance
  144. }).then(instance => {
  145. expect(getRoot(instance)).toEqual({
  146. type: 'div',
  147. event: ['click'],
  148. children: [{
  149. type: 'text',
  150. classList: ['a', 'c'],
  151. attr: { value: 'Hello World' }
  152. }]
  153. })
  154. done()
  155. })
  156. })
  157. })