123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
- import { getElementVisibleHeight } from './dom'; // 假设函数位于 utils.ts 中
- describe('getElementVisibleHeight', () => {
- // Mocking the getBoundingClientRect method
- const mockGetBoundingClientRect = vi.fn();
- const originalGetBoundingClientRect = Element.prototype.getBoundingClientRect;
- beforeAll(() => {
- // Mock getBoundingClientRect method
- Element.prototype.getBoundingClientRect = mockGetBoundingClientRect;
- });
- afterAll(() => {
- // Restore original getBoundingClientRect method
- Element.prototype.getBoundingClientRect = originalGetBoundingClientRect;
- });
- it('should return 0 if the element is null or undefined', () => {
- expect(getElementVisibleHeight(null)).toBe(0);
- expect(getElementVisibleHeight()).toBe(0);
- });
- it('should return the visible height of the element', () => {
- // Mock the getBoundingClientRect return value
- mockGetBoundingClientRect.mockReturnValue({
- bottom: 500,
- height: 400,
- left: 0,
- right: 0,
- toJSON: () => ({}),
- top: 100,
- width: 0,
- x: 0,
- y: 0,
- });
- const mockElement = document.createElement('div');
- document.body.append(mockElement);
- // Mocking window.innerHeight and document.documentElement.clientHeight
- const originalInnerHeight = window.innerHeight;
- const originalClientHeight = document.documentElement.clientHeight;
- Object.defineProperty(window, 'innerHeight', {
- value: 600,
- writable: true,
- });
- Object.defineProperty(document.documentElement, 'clientHeight', {
- value: 600,
- writable: true,
- });
- expect(getElementVisibleHeight(mockElement)).toBe(400);
- // Restore original values
- Object.defineProperty(window, 'innerHeight', {
- value: originalInnerHeight,
- writable: true,
- });
- Object.defineProperty(document.documentElement, 'clientHeight', {
- value: originalClientHeight,
- writable: true,
- });
- mockElement.remove();
- });
- it('should return the visible height when element is partially out of viewport', () => {
- // Mock the getBoundingClientRect return value
- mockGetBoundingClientRect.mockReturnValue({
- bottom: 300,
- height: 400,
- left: 0,
- right: 0,
- toJSON: () => ({}),
- top: -100,
- width: 0,
- x: 0,
- y: 0,
- });
- const mockElement = document.createElement('div');
- document.body.append(mockElement);
- // Mocking window.innerHeight and document.documentElement.clientHeight
- const originalInnerHeight = window.innerHeight;
- const originalClientHeight = document.documentElement.clientHeight;
- Object.defineProperty(window, 'innerHeight', {
- value: 600,
- writable: true,
- });
- Object.defineProperty(document.documentElement, 'clientHeight', {
- value: 600,
- writable: true,
- });
- expect(getElementVisibleHeight(mockElement)).toBe(300);
- // Restore original values
- Object.defineProperty(window, 'innerHeight', {
- value: originalInnerHeight,
- writable: true,
- });
- Object.defineProperty(document.documentElement, 'clientHeight', {
- value: originalClientHeight,
- writable: true,
- });
- mockElement.remove();
- });
- it('should return 0 if the element is completely out of viewport', () => {
- // Mock the getBoundingClientRect return value
- mockGetBoundingClientRect.mockReturnValue({
- bottom: -100,
- height: 400,
- left: 0,
- right: 0,
- toJSON: () => ({}),
- top: -500,
- width: 0,
- x: 0,
- y: 0,
- });
- const mockElement = document.createElement('div');
- document.body.append(mockElement);
- expect(getElementVisibleHeight(mockElement)).toBe(0);
- mockElement.remove();
- });
- });
|