dom.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
  2. import { getElementVisibleHeight } from './dom'; // 假设函数位于 utils.ts 中
  3. describe('getElementVisibleHeight', () => {
  4. // Mocking the getBoundingClientRect method
  5. const mockGetBoundingClientRect = vi.fn();
  6. const originalGetBoundingClientRect = Element.prototype.getBoundingClientRect;
  7. beforeAll(() => {
  8. // Mock getBoundingClientRect method
  9. Element.prototype.getBoundingClientRect = mockGetBoundingClientRect;
  10. });
  11. afterAll(() => {
  12. // Restore original getBoundingClientRect method
  13. Element.prototype.getBoundingClientRect = originalGetBoundingClientRect;
  14. });
  15. it('should return 0 if the element is null or undefined', () => {
  16. expect(getElementVisibleHeight(null)).toBe(0);
  17. expect(getElementVisibleHeight()).toBe(0);
  18. });
  19. it('should return the visible height of the element', () => {
  20. // Mock the getBoundingClientRect return value
  21. mockGetBoundingClientRect.mockReturnValue({
  22. bottom: 500,
  23. height: 400,
  24. left: 0,
  25. right: 0,
  26. toJSON: () => ({}),
  27. top: 100,
  28. width: 0,
  29. x: 0,
  30. y: 0,
  31. });
  32. const mockElement = document.createElement('div');
  33. document.body.append(mockElement);
  34. // Mocking window.innerHeight and document.documentElement.clientHeight
  35. const originalInnerHeight = window.innerHeight;
  36. const originalClientHeight = document.documentElement.clientHeight;
  37. Object.defineProperty(window, 'innerHeight', {
  38. value: 600,
  39. writable: true,
  40. });
  41. Object.defineProperty(document.documentElement, 'clientHeight', {
  42. value: 600,
  43. writable: true,
  44. });
  45. expect(getElementVisibleHeight(mockElement)).toBe(400);
  46. // Restore original values
  47. Object.defineProperty(window, 'innerHeight', {
  48. value: originalInnerHeight,
  49. writable: true,
  50. });
  51. Object.defineProperty(document.documentElement, 'clientHeight', {
  52. value: originalClientHeight,
  53. writable: true,
  54. });
  55. mockElement.remove();
  56. });
  57. it('should return the visible height when element is partially out of viewport', () => {
  58. // Mock the getBoundingClientRect return value
  59. mockGetBoundingClientRect.mockReturnValue({
  60. bottom: 300,
  61. height: 400,
  62. left: 0,
  63. right: 0,
  64. toJSON: () => ({}),
  65. top: -100,
  66. width: 0,
  67. x: 0,
  68. y: 0,
  69. });
  70. const mockElement = document.createElement('div');
  71. document.body.append(mockElement);
  72. // Mocking window.innerHeight and document.documentElement.clientHeight
  73. const originalInnerHeight = window.innerHeight;
  74. const originalClientHeight = document.documentElement.clientHeight;
  75. Object.defineProperty(window, 'innerHeight', {
  76. value: 600,
  77. writable: true,
  78. });
  79. Object.defineProperty(document.documentElement, 'clientHeight', {
  80. value: 600,
  81. writable: true,
  82. });
  83. expect(getElementVisibleHeight(mockElement)).toBe(300);
  84. // Restore original values
  85. Object.defineProperty(window, 'innerHeight', {
  86. value: originalInnerHeight,
  87. writable: true,
  88. });
  89. Object.defineProperty(document.documentElement, 'clientHeight', {
  90. value: originalClientHeight,
  91. writable: true,
  92. });
  93. mockElement.remove();
  94. });
  95. it('should return 0 if the element is completely out of viewport', () => {
  96. // Mock the getBoundingClientRect return value
  97. mockGetBoundingClientRect.mockReturnValue({
  98. bottom: -100,
  99. height: 400,
  100. left: 0,
  101. right: 0,
  102. toJSON: () => ({}),
  103. top: -500,
  104. width: 0,
  105. x: 0,
  106. y: 0,
  107. });
  108. const mockElement = document.createElement('div');
  109. document.body.append(mockElement);
  110. expect(getElementVisibleHeight(mockElement)).toBe(0);
  111. mockElement.remove();
  112. });
  113. });