affix.test.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
  4. import defineGetter from '@element-plus/test-utils/define-getter'
  5. import makeScroll from '@element-plus/test-utils/make-scroll'
  6. import Affix from '../src/affix.vue'
  7. import type { VNode } from 'vue'
  8. let clientHeightRestore: () => void
  9. const _mount = (render: () => VNode) => {
  10. return mount(render, { attachTo: document.body })
  11. }
  12. const AXIOM = 'Rem is the best girl'
  13. beforeAll(() => {
  14. clientHeightRestore = defineGetter(
  15. window.HTMLElement.prototype,
  16. 'clientHeight',
  17. 1000,
  18. 0
  19. )
  20. })
  21. afterAll(() => {
  22. clientHeightRestore()
  23. })
  24. describe('Affix.vue', () => {
  25. test('render test', async () => {
  26. const wrapper = _mount(() => <Affix>{AXIOM}</Affix>)
  27. await nextTick()
  28. expect(wrapper.text()).toEqual(AXIOM)
  29. const mockAffixRect = vi
  30. .spyOn(wrapper.find('.el-affix').element, 'getBoundingClientRect')
  31. .mockReturnValue({
  32. height: 40,
  33. width: 1000,
  34. top: -100,
  35. bottom: -80,
  36. } as DOMRect)
  37. const mockDocumentRect = vi
  38. .spyOn(document.documentElement, 'getBoundingClientRect')
  39. .mockReturnValue({
  40. height: 200,
  41. width: 1000,
  42. top: 0,
  43. bottom: 200,
  44. } as DOMRect)
  45. expect(wrapper.find('.el-affix--fixed').exists()).toBe(false)
  46. await makeScroll(document.documentElement, 'scrollTop', 200)
  47. expect(wrapper.find('.el-affix--fixed').exists()).toBe(true)
  48. mockAffixRect.mockRestore()
  49. mockDocumentRect.mockRestore()
  50. })
  51. test('should render offset props', async () => {
  52. const wrapper = _mount(() => <Affix offset={30}>{AXIOM}</Affix>)
  53. await nextTick()
  54. const mockAffixRect = vi
  55. .spyOn(wrapper.find('.el-affix').element, 'getBoundingClientRect')
  56. .mockReturnValue({
  57. height: 40,
  58. width: 1000,
  59. top: -100,
  60. bottom: -80,
  61. } as DOMRect)
  62. const mockDocumentRect = vi
  63. .spyOn(document.documentElement, 'getBoundingClientRect')
  64. .mockReturnValue({
  65. height: 200,
  66. width: 1000,
  67. top: 0,
  68. bottom: 200,
  69. } as DOMRect)
  70. await makeScroll(document.documentElement, 'scrollTop', 200)
  71. expect(wrapper.find('.el-affix--fixed').exists()).toBe(true)
  72. expect(wrapper.find('.el-affix--fixed').attributes('style')).toContain(
  73. 'top: 30px;'
  74. )
  75. mockAffixRect.mockRestore()
  76. mockDocumentRect.mockRestore()
  77. })
  78. test('should render position props', async () => {
  79. const wrapper = _mount(() => (
  80. <Affix position="bottom" offset={20}>
  81. {AXIOM}
  82. </Affix>
  83. ))
  84. await nextTick()
  85. const mockAffixRect = vi
  86. .spyOn(wrapper.find('.el-affix').element, 'getBoundingClientRect')
  87. .mockReturnValue({
  88. height: 40,
  89. width: 1000,
  90. top: 2000,
  91. bottom: 2040,
  92. } as DOMRect)
  93. const mockDocumentRect = vi
  94. .spyOn(document.documentElement, 'getBoundingClientRect')
  95. .mockReturnValue({
  96. height: 200,
  97. width: 1000,
  98. top: 0,
  99. bottom: 200,
  100. } as DOMRect)
  101. await makeScroll(document.documentElement, 'scrollTop', 0)
  102. expect(wrapper.find('.el-affix--fixed').exists()).toBe(true)
  103. expect(wrapper.find('.el-affix--fixed').attributes('style')).toContain(
  104. 'bottom: 20px;'
  105. )
  106. mockAffixRect.mockRestore()
  107. mockDocumentRect.mockRestore()
  108. })
  109. test('should render target props', async () => {
  110. const wrapper = _mount(() => (
  111. <>
  112. <div class="target" style="height: 200px">
  113. <Affix target=".target">{AXIOM}</Affix>
  114. </div>
  115. <div style="height: 1000px"></div>
  116. </>
  117. ))
  118. await nextTick()
  119. const mockAffixRect = vi
  120. .spyOn(wrapper.find('.el-affix').element, 'getBoundingClientRect')
  121. .mockReturnValue({
  122. height: 40,
  123. width: 1000,
  124. top: -100,
  125. bottom: -60,
  126. } as DOMRect)
  127. const mockTargetRect = vi
  128. .spyOn(wrapper.find('.target').element, 'getBoundingClientRect')
  129. .mockReturnValue({
  130. height: 200,
  131. width: 1000,
  132. top: -100,
  133. bottom: 100,
  134. } as DOMRect)
  135. await makeScroll(document.documentElement, 'scrollTop', 100)
  136. expect(wrapper.find('.el-affix--fixed').exists()).toBe(true)
  137. mockAffixRect.mockReturnValue({
  138. height: 40,
  139. width: 1000,
  140. top: -300,
  141. bottom: -260,
  142. } as DOMRect)
  143. mockTargetRect.mockReturnValue({
  144. height: 40,
  145. width: 1000,
  146. top: -300,
  147. bottom: -260,
  148. } as DOMRect)
  149. await makeScroll(document.documentElement, 'scrollTop', 300)
  150. expect(wrapper.find('.el-affix--fixed').exists()).toBe(false)
  151. mockAffixRect.mockRestore()
  152. mockTargetRect.mockRestore()
  153. })
  154. test('should render z-index props', async () => {
  155. const wrapper = _mount(() => <Affix zIndex={1000}>{AXIOM}</Affix>)
  156. await nextTick()
  157. const mockAffixRect = vi
  158. .spyOn(wrapper.find('.el-affix').element, 'getBoundingClientRect')
  159. .mockReturnValue({
  160. height: 40,
  161. width: 1000,
  162. top: -100,
  163. bottom: -80,
  164. } as DOMRect)
  165. const mockDocumentRect = vi
  166. .spyOn(document.documentElement, 'getBoundingClientRect')
  167. .mockReturnValue({
  168. height: 200,
  169. width: 1000,
  170. top: 0,
  171. bottom: 200,
  172. } as DOMRect)
  173. await makeScroll(document.documentElement, 'scrollTop', 200)
  174. expect(wrapper.find('.el-affix--fixed').exists()).toBe(true)
  175. expect(wrapper.find('.el-affix--fixed').attributes('style')).toContain(
  176. 'z-index: 1000;'
  177. )
  178. mockAffixRect.mockRestore()
  179. mockDocumentRect.mockRestore()
  180. })
  181. })