scrollbar.test.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import { nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { describe, expect, test } from 'vitest'
  4. import makeScroll from '@element-plus/test-utils/make-scroll'
  5. import defineGetter from '@element-plus/test-utils/define-getter'
  6. import Scrollbar from '../src/scrollbar.vue'
  7. describe('ScrollBar', () => {
  8. test('vertical', async () => {
  9. const outerHeight = 204
  10. const innerHeight = 500
  11. const wrapper = mount(() => (
  12. <Scrollbar style={`height: ${outerHeight}px;`}>
  13. <div style={`height: ${innerHeight}px;`}></div>
  14. </Scrollbar>
  15. ))
  16. const scrollDom = wrapper.find('.el-scrollbar__wrap').element
  17. const offsetHeightRestore = defineGetter(
  18. scrollDom,
  19. 'offsetHeight',
  20. outerHeight
  21. )
  22. const scrollHeightRestore = defineGetter(
  23. scrollDom,
  24. 'scrollHeight',
  25. innerHeight
  26. )
  27. await makeScroll(scrollDom, 'scrollTop', 100)
  28. expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
  29. 'height: 80px; transform: translateY(50%);'
  30. )
  31. await makeScroll(scrollDom, 'scrollTop', 300)
  32. expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
  33. 'height: 80px; transform: translateY(150%);'
  34. )
  35. offsetHeightRestore()
  36. scrollHeightRestore()
  37. })
  38. test('horizontal', async () => {
  39. const outerWidth = 204
  40. const innerWidth = 500
  41. const wrapper = mount(() => (
  42. <Scrollbar style={`height: 100px; width: ${outerWidth}px;`}>
  43. <div style={`height: 100px; width: ${innerWidth}px;`}></div>
  44. </Scrollbar>
  45. ))
  46. const scrollDom = wrapper.find('.el-scrollbar__wrap').element
  47. const offsetWidthRestore = defineGetter(
  48. scrollDom,
  49. 'offsetWidth',
  50. outerWidth
  51. )
  52. const scrollWidthRestore = defineGetter(
  53. scrollDom,
  54. 'scrollWidth',
  55. innerWidth
  56. )
  57. await makeScroll(scrollDom, 'scrollLeft', 100)
  58. expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
  59. 'width: 80px; transform: translateX(50%);'
  60. )
  61. await makeScroll(scrollDom, 'scrollLeft', 300)
  62. expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
  63. 'width: 80px; transform: translateX(150%);'
  64. )
  65. offsetWidthRestore()
  66. scrollWidthRestore()
  67. })
  68. test('both vertical and horizontal', async () => {
  69. const outerHeight = 204
  70. const innerHeight = 500
  71. const outerWidth = 204
  72. const innerWidth = 500
  73. const wrapper = mount(() => (
  74. <Scrollbar style={`height: ${outerHeight}px; width: ${outerWidth}px;`}>
  75. <div style={`height: ${innerHeight}px; width: ${innerWidth}px;`}></div>
  76. </Scrollbar>
  77. ))
  78. const scrollDom = wrapper.find('.el-scrollbar__wrap').element
  79. const offsetHeightRestore = defineGetter(
  80. scrollDom,
  81. 'offsetHeight',
  82. outerHeight
  83. )
  84. const scrollHeightRestore = defineGetter(
  85. scrollDom,
  86. 'scrollHeight',
  87. innerHeight
  88. )
  89. const offsetWidthRestore = defineGetter(
  90. scrollDom,
  91. 'offsetWidth',
  92. outerWidth
  93. )
  94. const scrollWidthRestore = defineGetter(
  95. scrollDom,
  96. 'scrollWidth',
  97. innerWidth
  98. )
  99. await makeScroll(scrollDom, 'scrollTop', 100)
  100. await makeScroll(scrollDom, 'scrollLeft', 100)
  101. expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
  102. 'height: 80px; transform: translateY(50%);'
  103. )
  104. expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
  105. 'width: 80px; transform: translateX(50%);'
  106. )
  107. await makeScroll(scrollDom, 'scrollTop', 300)
  108. await makeScroll(scrollDom, 'scrollLeft', 300)
  109. expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
  110. 'height: 80px; transform: translateY(150%);'
  111. )
  112. expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
  113. 'width: 80px; transform: translateX(150%);'
  114. )
  115. offsetHeightRestore()
  116. scrollHeightRestore()
  117. offsetWidthRestore()
  118. scrollWidthRestore()
  119. })
  120. test('should render height props', async () => {
  121. const outerHeight = 204
  122. const innerHeight = 500
  123. const wrapper = mount(() => (
  124. <Scrollbar height={`${outerHeight}px`}>
  125. <div style={`height: ${innerHeight}px;`}></div>
  126. </Scrollbar>
  127. ))
  128. expect(wrapper.find('.el-scrollbar__wrap').attributes('style')).toContain(
  129. 'height: 204px;'
  130. )
  131. })
  132. test('should render max-height props', async () => {
  133. const outerHeight = 204
  134. const innerHeight = 100
  135. const wrapper = mount(() => (
  136. <Scrollbar max-height={`${outerHeight}px`}>
  137. <div style={`height: ${innerHeight}px;`}></div>
  138. </Scrollbar>
  139. ))
  140. expect(wrapper.find('.el-scrollbar__wrap').attributes('style')).toContain(
  141. 'max-height: 204px;'
  142. )
  143. })
  144. test('should render always props', async () => {
  145. const outerHeight = 204
  146. const innerHeight = 500
  147. const wrapper = mount(() => (
  148. <Scrollbar height={`${outerHeight}px`} always>
  149. <div style={`height: ${innerHeight}px;`}></div>
  150. </Scrollbar>
  151. ))
  152. expect(wrapper.find('.el-scrollbar__bar').attributes('style')).toBeFalsy()
  153. })
  154. test('set scrollTop & scrollLeft', async () => {
  155. const outerHeight = 204
  156. const innerHeight = 500
  157. const outerWidth = 204
  158. const innerWidth = 500
  159. const wrapper = mount({
  160. setup() {
  161. return () => (
  162. <Scrollbar
  163. ref="scrollbar"
  164. style={`height: ${outerHeight}px; width: ${outerWidth}px;`}
  165. always
  166. >
  167. <div
  168. style={`height: ${innerHeight}px; width: ${innerWidth}px;`}
  169. ></div>
  170. </Scrollbar>
  171. )
  172. },
  173. })
  174. const scrollbar = wrapper.findComponent({ ref: 'scrollbar' }).vm
  175. const scrollDom = wrapper.find('.el-scrollbar__wrap').element
  176. const offsetHeightRestore = defineGetter(
  177. scrollDom,
  178. 'offsetHeight',
  179. outerHeight
  180. )
  181. const scrollHeightRestore = defineGetter(
  182. scrollDom,
  183. 'scrollHeight',
  184. innerHeight
  185. )
  186. const offsetWidthRestore = defineGetter(
  187. scrollDom,
  188. 'offsetWidth',
  189. outerWidth
  190. )
  191. const scrollWidthRestore = defineGetter(
  192. scrollDom,
  193. 'scrollWidth',
  194. innerWidth
  195. )
  196. scrollbar.setScrollTop(100)
  197. await nextTick()
  198. scrollbar.setScrollLeft(100)
  199. await nextTick()
  200. expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
  201. 'height: 80px; transform: translateY(0%);'
  202. )
  203. expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
  204. 'width: 80px; transform: translateX(0%);'
  205. )
  206. offsetHeightRestore()
  207. scrollHeightRestore()
  208. offsetWidthRestore()
  209. scrollWidthRestore()
  210. })
  211. test('should render min-size props', async () => {
  212. const outerHeight = 204
  213. const innerHeight = 10000
  214. const wrapper = mount(() => (
  215. <Scrollbar style={`height: ${outerHeight}px;`}>
  216. <div style={`height: ${innerHeight}px;`}></div>
  217. </Scrollbar>
  218. ))
  219. const scrollDom = wrapper.find('.el-scrollbar__wrap').element
  220. const offsetHeightRestore = defineGetter(
  221. scrollDom,
  222. 'offsetHeight',
  223. outerHeight
  224. )
  225. const scrollHeightRestore = defineGetter(
  226. scrollDom,
  227. 'scrollHeight',
  228. innerHeight
  229. )
  230. await makeScroll(scrollDom, 'scrollTop', 0)
  231. expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
  232. 'height: 20px; transform: translateY(0%);'
  233. )
  234. offsetHeightRestore()
  235. scrollHeightRestore()
  236. })
  237. test('should render tag props', async () => {
  238. const wrapper = mount(() => (
  239. <Scrollbar tag="ul">
  240. {[1, 2, 3].map((item) => (
  241. <li>{item}</li>
  242. ))}
  243. </Scrollbar>
  244. ))
  245. expect(
  246. wrapper.find('.el-scrollbar__view').element instanceof HTMLUListElement
  247. ).toBeTruthy()
  248. })
  249. test('should render wrap-style props', async () => {
  250. const wrapStyle = 'background: red;'
  251. const wrapper = mount(() => <Scrollbar wrap-style={wrapStyle} />)
  252. expect(wrapper.find('.el-scrollbar__wrap').attributes('style')).toContain(
  253. wrapStyle
  254. )
  255. })
  256. test('should render wrap-class props', async () => {
  257. const wrapClass = 'test-wrap-class'
  258. const wrapper = mount(() => <Scrollbar wrap-class={wrapClass} />)
  259. expect(wrapper.find('.el-scrollbar__wrap').classes()).toContain(wrapClass)
  260. })
  261. test('should render view-style props', async () => {
  262. const viewStyle = 'display: inline-block;'
  263. const wrapper = mount(() => <Scrollbar view-style={viewStyle} />)
  264. expect(wrapper.find('.el-scrollbar__view').attributes('style')).toContain(
  265. viewStyle
  266. )
  267. })
  268. test('should render view-class props', async () => {
  269. const viewClass = 'test-view-class'
  270. const wrapper = mount(() => <Scrollbar view-class={viewClass} />)
  271. expect(wrapper.find('.el-scrollbar__view').classes()).toContain(viewClass)
  272. })
  273. })