-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathgetScrollBarSize.test.ts
More file actions
184 lines (154 loc) · 5.16 KB
/
getScrollBarSize.test.ts
File metadata and controls
184 lines (154 loc) · 5.16 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import getScrollBarSize, {
getTargetScrollBarSize,
} from '../src/getScrollBarSize';
import { spyElementPrototypes } from '../src/test/domHook';
import * as dynamicCSS from '../src/Dom/dynamicCSS';
const DEFAULT_SIZE = 16;
describe('getScrollBarSize', () => {
let defaultSize = DEFAULT_SIZE;
beforeAll(() => {
spyElementPrototypes(HTMLElement, {
offsetWidth: {
get: () => {
return 100;
},
},
clientWidth: {
get: () => {
return 100 - defaultSize;
},
},
});
});
beforeEach(() => {
defaultSize = DEFAULT_SIZE;
});
it('basicSize', () => {
expect(getScrollBarSize()).toEqual(16);
});
it('fresh it', () => {
expect(getScrollBarSize(true)).toEqual(16);
defaultSize = 33;
expect(getScrollBarSize(true)).toEqual(33);
});
describe('getTargetScrollBarSize', () => {
it('invalidate', () => {
expect(
getTargetScrollBarSize({ notValidateObject: true } as any),
).toEqual({
width: 0,
height: 0,
});
});
});
describe('nonce support', () => {
let updateCSSSpy: jest.SpyInstance;
beforeEach(() => {
updateCSSSpy = jest.spyOn(dynamicCSS, 'updateCSS');
});
afterEach(() => {
updateCSSSpy.mockRestore();
});
it('should pass nonce to updateCSS when provided in getScrollBarSize', () => {
const testNonce = 'test-nonce-123';
// We need to test with getTargetScrollBarSize since getScrollBarSize
// without an element doesn't trigger updateCSS
const mockElement = document.createElement('div');
document.body.appendChild(mockElement);
// Mock getComputedStyle to return webkit scrollbar dimensions
const originalGetComputedStyle = window.getComputedStyle;
window.getComputedStyle = jest
.fn()
.mockImplementation((element, pseudoElement) => {
if (pseudoElement === '::-webkit-scrollbar') {
return {
width: '10px',
height: '10px',
};
}
return {
scrollbarColor: '',
scrollbarWidth: '',
overflow: 'scroll',
};
}) as any;
// This will trigger updateCSS with webkit styles
getTargetScrollBarSize(mockElement, testNonce);
// Restore original
window.getComputedStyle = originalGetComputedStyle;
document.body.removeChild(mockElement);
// Check if updateCSS was called with nonce
const updateCSSCalls = updateCSSSpy.mock.calls;
const callWithNonce = updateCSSCalls.find(
call => call[2]?.csp?.nonce === testNonce,
);
expect(callWithNonce).toBeDefined();
});
it('should pass nonce to updateCSS when provided in getTargetScrollBarSize', () => {
const testNonce = 'target-nonce-456';
const mockElement = document.createElement('div');
document.body.appendChild(mockElement);
// Mock getComputedStyle to return webkit scrollbar dimensions
const originalGetComputedStyle = window.getComputedStyle;
window.getComputedStyle = jest
.fn()
.mockImplementation((element, pseudoElement) => {
if (pseudoElement === '::-webkit-scrollbar') {
return {
width: '12px',
height: '12px',
};
}
return {
scrollbarColor: '',
scrollbarWidth: '',
overflow: 'scroll',
};
}) as any;
getTargetScrollBarSize(mockElement, testNonce);
// Restore original
window.getComputedStyle = originalGetComputedStyle;
document.body.removeChild(mockElement);
// Check if updateCSS was called with nonce
const updateCSSCalls = updateCSSSpy.mock.calls;
const callWithNonce = updateCSSCalls.find(
call => call[2]?.csp?.nonce === testNonce,
);
expect(callWithNonce).toBeDefined();
});
it('should not pass nonce to updateCSS when not provided', () => {
const mockElement = document.createElement('div');
document.body.appendChild(mockElement);
// Mock getComputedStyle to return webkit scrollbar dimensions
const originalGetComputedStyle = window.getComputedStyle;
window.getComputedStyle = jest
.fn()
.mockImplementation((element, pseudoElement) => {
if (pseudoElement === '::-webkit-scrollbar') {
return {
width: '8px',
height: '8px',
};
}
return {
scrollbarColor: '',
scrollbarWidth: '',
overflow: 'scroll',
};
}) as any;
// Clear previous calls
updateCSSSpy.mockClear();
// Call without nonce
getTargetScrollBarSize(mockElement);
// Restore original
window.getComputedStyle = originalGetComputedStyle;
document.body.removeChild(mockElement);
// Check if updateCSS was called without nonce (undefined as third parameter)
const updateCSSCalls = updateCSSSpy.mock.calls;
if (updateCSSCalls.length > 0) {
const lastCall = updateCSSCalls[updateCSSCalls.length - 1];
expect(lastCall[2]).toBeUndefined();
}
});
});
});