dom.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { beforeEach, describe, expect, it, vi } from 'vitest';
  2. import { getElementVisibleRect } from './dom'; // 假设函数位于 utils.ts 中
  3. describe('getElementVisibleRect', () => {
  4. // 设置浏览器视口尺寸的 mock
  5. beforeEach(() => {
  6. vi.spyOn(document.documentElement, 'clientHeight', 'get').mockReturnValue(
  7. 800,
  8. );
  9. vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(800);
  10. vi.spyOn(document.documentElement, 'clientWidth', 'get').mockReturnValue(
  11. 1000,
  12. );
  13. vi.spyOn(window, 'innerWidth', 'get').mockReturnValue(1000);
  14. });
  15. it('should return default rect if element is undefined', () => {
  16. expect(getElementVisibleRect()).toEqual({
  17. bottom: 0,
  18. height: 0,
  19. left: 0,
  20. right: 0,
  21. top: 0,
  22. width: 0,
  23. });
  24. });
  25. it('should return default rect if element is null', () => {
  26. expect(getElementVisibleRect(null)).toEqual({
  27. bottom: 0,
  28. height: 0,
  29. left: 0,
  30. right: 0,
  31. top: 0,
  32. width: 0,
  33. });
  34. });
  35. it('should return correct visible rect when element is fully visible', () => {
  36. const element = {
  37. getBoundingClientRect: () => ({
  38. bottom: 400,
  39. height: 300,
  40. left: 200,
  41. right: 600,
  42. top: 100,
  43. width: 400,
  44. }),
  45. } as HTMLElement;
  46. expect(getElementVisibleRect(element)).toEqual({
  47. bottom: 400,
  48. height: 300,
  49. left: 200,
  50. right: 600,
  51. top: 100,
  52. width: 400,
  53. });
  54. });
  55. it('should return correct visible rect when element is partially off-screen at the top', () => {
  56. const element = {
  57. getBoundingClientRect: () => ({
  58. bottom: 200,
  59. height: 250,
  60. left: 100,
  61. right: 500,
  62. top: -50,
  63. width: 400,
  64. }),
  65. } as HTMLElement;
  66. expect(getElementVisibleRect(element)).toEqual({
  67. bottom: 200,
  68. height: 200,
  69. left: 100,
  70. right: 500,
  71. top: 0,
  72. width: 400,
  73. });
  74. });
  75. it('should return correct visible rect when element is partially off-screen at the right', () => {
  76. const element = {
  77. getBoundingClientRect: () => ({
  78. bottom: 400,
  79. height: 300,
  80. left: 800,
  81. right: 1200,
  82. top: 100,
  83. width: 400,
  84. }),
  85. } as HTMLElement;
  86. expect(getElementVisibleRect(element)).toEqual({
  87. bottom: 400,
  88. height: 300,
  89. left: 800,
  90. right: 1000,
  91. top: 100,
  92. width: 200,
  93. });
  94. });
  95. it('should return all zeros when element is completely off-screen', () => {
  96. const element = {
  97. getBoundingClientRect: () => ({
  98. bottom: 1200,
  99. height: 300,
  100. left: 1100,
  101. right: 1400,
  102. top: 900,
  103. width: 300,
  104. }),
  105. } as HTMLElement;
  106. expect(getElementVisibleRect(element)).toEqual({
  107. bottom: 800,
  108. height: 0,
  109. left: 1100,
  110. right: 1000,
  111. top: 900,
  112. width: 0,
  113. });
  114. });
  115. });