-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathgetHeightOfHeaderAndFooter.test.ts
More file actions
164 lines (134 loc) · 4.58 KB
/
getHeightOfHeaderAndFooter.test.ts
File metadata and controls
164 lines (134 loc) · 4.58 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
import '@testing-library/jest-dom';
import { describe, expect, test, beforeEach } from '@jest/globals';
import { getHeightOfHeaderAndFooter } from '@/utils/functions/getHeightOfHeaderAndFooter';
describe('getHeightOfHeaderAndFooter', () => {
beforeEach(() => {
// Clear the document body before each test
document.body.innerHTML = '';
});
test('should return 0 when no elements with specified classes exist', () => {
// Given...
// Empty document body
// When...
const totalHeight = getHeightOfHeaderAndFooter();
// Then...
expect(totalHeight).toBe(0);
});
test('should return the height of a single element with class "toolbar"', () => {
// Given...
const toolbarElement = document.createElement('div');
toolbarElement.className = 'toolbar';
Object.defineProperty(toolbarElement, 'offsetHeight', {
configurable: true,
value: 64,
});
document.body.appendChild(toolbarElement);
// When...
const totalHeight = getHeightOfHeaderAndFooter();
// Then...
expect(totalHeight).toBe(64);
});
test('should accumulate heights from multiple different class elements', () => {
// Given...
const toolbarElement = document.createElement('div');
toolbarElement.className = 'toolbar';
Object.defineProperty(toolbarElement, 'offsetHeight', {
configurable: true,
value: 64,
});
const crumbElement = document.createElement('div');
crumbElement.className = 'crumbContainer';
Object.defineProperty(crumbElement, 'offsetHeight', {
configurable: true,
value: 48,
});
const headerElement = document.createElement('div');
headerElement.className = 'cds--header__global';
Object.defineProperty(headerElement, 'offsetHeight', {
configurable: true,
value: 48,
});
const footerElement = document.createElement('div');
footerElement.className = 'footer';
Object.defineProperty(footerElement, 'offsetHeight', {
configurable: true,
value: 80,
});
document.body.appendChild(toolbarElement);
document.body.appendChild(crumbElement);
document.body.appendChild(headerElement);
document.body.appendChild(footerElement);
// When...
const totalHeight = getHeightOfHeaderAndFooter();
// Then...
expect(totalHeight).toBe(240); // 64 + 48 + 48 + 80
});
test('should accumulate heights from multiple elements with the same class', () => {
// Given...
const toolbar1 = document.createElement('div');
toolbar1.className = 'toolbar';
Object.defineProperty(toolbar1, 'offsetHeight', {
configurable: true,
value: 64,
});
const toolbar2 = document.createElement('div');
toolbar2.className = 'toolbar';
Object.defineProperty(toolbar2, 'offsetHeight', {
configurable: true,
value: 32,
});
document.body.appendChild(toolbar1);
document.body.appendChild(toolbar2);
// When...
const totalHeight = getHeightOfHeaderAndFooter();
// Then...
expect(totalHeight).toBe(96); // 64 + 32
});
test('should handle elements with zero height', () => {
// Given...
const toolbarElement = document.createElement('div');
toolbarElement.className = 'toolbar';
Object.defineProperty(toolbarElement, 'offsetHeight', {
configurable: true,
value: 0,
});
const footerElement = document.createElement('div');
footerElement.className = 'footer';
Object.defineProperty(footerElement, 'offsetHeight', {
configurable: true,
value: 80,
});
document.body.appendChild(toolbarElement);
document.body.appendChild(footerElement);
// When...
const totalHeight = getHeightOfHeaderAndFooter();
// Then...
expect(totalHeight).toBe(80); // 0 + 80
});
test('should only count elements with exact class names', () => {
// Given...
const toolbarElement = document.createElement('div');
toolbarElement.className = 'toolbar';
Object.defineProperty(toolbarElement, 'offsetHeight', {
configurable: true,
value: 64,
});
const notToolbarElement = document.createElement('div');
notToolbarElement.className = 'toolbar-extra';
Object.defineProperty(notToolbarElement, 'offsetHeight', {
configurable: true,
value: 100,
});
document.body.appendChild(toolbarElement);
document.body.appendChild(notToolbarElement);
// When...
const totalHeight = getHeightOfHeaderAndFooter();
// Then...
expect(totalHeight).toBe(64); // Only the exact 'toolbar' class
});
});