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