style.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { describe, expect, it } from 'vitest'
  2. import { addClass, hasClass, removeClass } from '../..'
  3. const getClass = (el: Element) => {
  4. if (!el) return ''
  5. return el.getAttribute('class')
  6. }
  7. describe('dom style', () => {
  8. describe('hasClass', () => {
  9. it('Judge whether a Element has a class', () => {
  10. const div = document.createElement('div')
  11. div.className = 'a b cc'
  12. expect(hasClass(div, 'a')).toBe(true)
  13. expect(hasClass(div, 'ab')).toBe(false)
  14. try {
  15. expect(hasClass(div, 'a b'))
  16. } catch (error: any) {
  17. expect(error.message).toEqual('className should not contain space.')
  18. }
  19. const canvas = document.createElement('canvas')
  20. canvas.className = 'canvas-a canvas-b cc'
  21. expect(hasClass(canvas, 'a')).toBe(false)
  22. expect(hasClass(canvas, 'canvas-a')).toBe(true)
  23. // remove classList
  24. canvas.setAttribute('classList', '')
  25. expect(hasClass(canvas, 'b')).toBe(false)
  26. expect(hasClass(canvas, 'canvas-b')).toBe(true)
  27. const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  28. svg.setAttribute('class', 'svg-a svg-b')
  29. expect(hasClass(svg, 'a')).toBe(false)
  30. expect(hasClass(svg, 'svg-a')).toBe(true)
  31. // remove classList
  32. svg.setAttribute('classList', '')
  33. expect(hasClass(svg, 'b')).toBe(false)
  34. expect(hasClass(svg, 'svg-b')).toBe(true)
  35. const path = document.createElementNS(
  36. 'http://www.w3.org/2000/svg',
  37. 'path'
  38. )
  39. path.setAttribute('class', 'path-a path-b')
  40. expect(hasClass(path, 'a')).toBe(false)
  41. expect(hasClass(path, 'path-a')).toBe(true)
  42. // remove classList
  43. path.setAttribute('classList', '')
  44. expect(hasClass(path, 'b')).toBe(false)
  45. expect(hasClass(path, 'path-b')).toBe(true)
  46. })
  47. })
  48. describe('addClass', () => {
  49. it('Add class to element', () => {
  50. const div = document.createElement('div')
  51. addClass(div, 'div-abc abc')
  52. expect(hasClass(div, 'abc')).toBe(true)
  53. expect(hasClass(div, 'div-abc')).toBe(true)
  54. expect(hasClass(div, 'div')).toBe(false)
  55. addClass(div, 'abc')
  56. expect(getClass(div)).toEqual('div-abc abc')
  57. // remove classList
  58. div.setAttribute('classList', '')
  59. addClass(div, 'div-box con')
  60. expect(hasClass(div, 'con')).toBe(true)
  61. expect(hasClass(div, 'div-box')).toBe(true)
  62. expect(hasClass(div, 'box')).toBe(false)
  63. addClass(div, 'con')
  64. expect(getClass(div)).toEqual('div-abc abc div-box con')
  65. const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  66. addClass(svg, 'svg-abc svg')
  67. expect(hasClass(svg, 'svg')).toBe(true)
  68. expect(hasClass(svg, 'svg-abc')).toBe(true)
  69. expect(hasClass(svg, 'abc')).toBe(false)
  70. expect(hasClass(svg, 'sv')).toBe(false)
  71. addClass(svg, 'svg')
  72. expect(getClass(svg)).toEqual('svg-abc svg')
  73. addClass(svg, 'svg-aa space')
  74. expect(getClass(svg)).toEqual('svg-abc svg svg-aa space')
  75. // remove classList
  76. svg.setAttribute('classList', '')
  77. addClass(svg, 'svg-abc-a svg-b')
  78. expect(hasClass(svg, 'svg')).toBe(true)
  79. expect(hasClass(svg, 'svg-abc')).toBe(true)
  80. expect(hasClass(svg, 'abc')).toBe(false)
  81. expect(hasClass(svg, 'sv')).toBe(false)
  82. expect(hasClass(svg, 'svg-abc-a')).toBe(true)
  83. expect(hasClass(svg, 'svg-b')).toBe(true)
  84. expect(hasClass(svg, 'a')).toBe(false)
  85. const path = document.createElementNS(
  86. 'http://www.w3.org/2000/svg',
  87. 'path'
  88. )
  89. addClass(path, 'path-abc path')
  90. expect(hasClass(path, 'path')).toBe(true)
  91. expect(hasClass(path, 'path-abc')).toBe(true)
  92. expect(hasClass(path, 'abc')).toBe(false)
  93. expect(hasClass(path, 'pa')).toBe(false)
  94. addClass(path, 'path')
  95. expect(getClass(path)).toEqual('path-abc path')
  96. // remove classList
  97. path.setAttribute('classList', '')
  98. expect(hasClass(path, 'path')).toBe(true)
  99. expect(hasClass(path, 'path-abc')).toBe(true)
  100. expect(hasClass(path, 'abc')).toBe(false)
  101. expect(hasClass(path, 'pa')).toBe(false)
  102. })
  103. })
  104. describe('removeClass', () => {
  105. it('Remove class on element', () => {
  106. const div = document.createElement('div')
  107. addClass(div, 'div-abc abc ab bb cc')
  108. removeClass(div, 'abc')
  109. expect(hasClass(div, 'abc')).toBe(false)
  110. expect(hasClass(div, 'bb')).toBe(true)
  111. expect(hasClass(div, 'cc')).toBe(true)
  112. removeClass(div, 'bb cc')
  113. expect(hasClass(div, 'bb')).toBe(false)
  114. expect(hasClass(div, 'cc')).toBe(false)
  115. expect(getClass(div)).toEqual('div-abc ab')
  116. // remove classList
  117. div.setAttribute('classList', '')
  118. addClass(div, 'div-box con')
  119. removeClass(div, 'div-box con')
  120. expect(hasClass(div, 'con')).toBe(false)
  121. expect(hasClass(div, 'div-box')).toBe(false)
  122. expect(getClass(div)).toBe('div-abc ab')
  123. const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
  124. addClass(svg, 'svg-abc svg')
  125. removeClass(svg, 'svg-abc svg')
  126. expect(hasClass(svg, 'svg-abc')).toBe(false)
  127. expect(hasClass(svg, 'abc')).toBe(false)
  128. expect(getClass(svg)).toEqual('')
  129. // remove classList
  130. svg.setAttribute('classList', '')
  131. addClass(svg, 'svg-abc-a svg-b')
  132. expect(hasClass(svg, 'svg')).toBe(false)
  133. expect(hasClass(svg, 'svg-abc')).toBe(false)
  134. expect(hasClass(svg, 'abc')).toBe(false)
  135. expect(hasClass(svg, 'svg-abc-a')).toBe(true)
  136. expect(hasClass(svg, 'svg-b')).toBe(true)
  137. removeClass(svg, 'svg')
  138. expect(getClass(svg)).toEqual('svg-abc-a svg-b')
  139. removeClass(svg, 'svg-abc-a')
  140. expect(hasClass(svg, 'svg-abc-a')).toBe(false)
  141. expect(hasClass(svg, 'svg-b')).toBe(true)
  142. const path = document.createElementNS(
  143. 'http://www.w3.org/2000/svg',
  144. 'path'
  145. )
  146. addClass(path, 'path-abc path')
  147. expect(hasClass(path, 'path')).toBe(true)
  148. expect(hasClass(path, 'path-abc')).toBe(true)
  149. expect(hasClass(path, 'abc')).toBe(false)
  150. expect(hasClass(path, 'pa')).toBe(false)
  151. removeClass(path, 'path')
  152. expect(hasClass(path, 'path')).toBe(false)
  153. expect(hasClass(path, 'path-abc')).toBe(true)
  154. // remove classList
  155. path.setAttribute('classList', '')
  156. addClass(path, 'path path-1 path2')
  157. expect(hasClass(path, 'path')).toBe(true)
  158. expect(hasClass(path, 'path-1')).toBe(true)
  159. expect(getClass(path)).toEqual('path-abc path path-1 path2')
  160. removeClass(path, 'path')
  161. expect(hasClass(path, 'path')).toBe(false)
  162. expect(getClass(path)).toEqual('path-abc path-1 path2')
  163. removeClass(path, 'path-abc path-1 path2')
  164. expect(getClass(path)).toEqual('')
  165. })
  166. })
  167. })