-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathblock-scroll-strategy.spec.ts
271 lines (222 loc) · 8.74 KB
/
block-scroll-strategy.spec.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import {Component} from '@angular/core';
import {waitForAsync, inject, TestBed} from '@angular/core/testing';
import {ComponentPortal, PortalModule} from '../../portal';
import {Platform} from '../../platform';
import {ViewportRuler} from '../../scrolling';
import {Overlay, OverlayContainer, OverlayModule, OverlayRef, OverlayConfig} from '../index';
describe('BlockScrollStrategy', () => {
let platform: Platform;
let viewport: ViewportRuler;
let documentElement: HTMLElement;
let overlayRef: OverlayRef;
let componentPortal: ComponentPortal<FocacciaMsg>;
let forceScrollElement: HTMLElement;
beforeEach(waitForAsync(() => {
documentElement = document.documentElement!;
// Ensure a clean state for every test.
documentElement.classList.remove('cdk-global-scrollblock');
TestBed.configureTestingModule({
imports: [OverlayModule, PortalModule, FocacciaMsg],
});
}));
beforeEach(inject(
[Overlay, ViewportRuler, Platform],
(overlay: Overlay, viewportRuler: ViewportRuler, _platform: Platform) => {
let overlayConfig = new OverlayConfig({scrollStrategy: overlay.scrollStrategies.block()});
overlayRef = overlay.create(overlayConfig);
componentPortal = new ComponentPortal(FocacciaMsg);
viewport = viewportRuler;
forceScrollElement = document.createElement('div');
document.body.appendChild(forceScrollElement);
forceScrollElement.style.width = '100px';
forceScrollElement.style.height = '3000px';
forceScrollElement.style.background = 'rebeccapurple';
platform = _platform;
},
));
afterEach(inject([OverlayContainer], (container: OverlayContainer) => {
overlayRef.dispose();
forceScrollElement.remove();
window.scroll(0, 0);
container.getContainerElement().remove();
}));
it(
'should toggle scroll blocking along the y axis',
skipIOS(() => {
window.scroll(0, 100);
expect(viewport.getViewportScrollPosition().top)
.withContext('Expected viewport to be scrollable initially.')
.toBe(100);
overlayRef.attach(componentPortal);
expect(documentElement.style.top)
.withContext('Expected <html> element to be offset by the previous scroll amount.')
.toBe('-100px');
window.scroll(0, 300);
expect(viewport.getViewportScrollPosition().top)
.withContext('Expected the viewport not to scroll.')
.toBe(100);
overlayRef.detach();
expect(viewport.getViewportScrollPosition().top)
.withContext('Expected old scroll position to have bee restored after disabling.')
.toBe(100);
window.scroll(0, 300);
expect(viewport.getViewportScrollPosition().top)
.withContext('Expected user to be able to scroll after disabling.')
.toBe(300);
}),
);
it(
'should toggle scroll blocking along the x axis',
skipIOS(() => {
forceScrollElement.style.height = '100px';
forceScrollElement.style.width = '3000px';
window.scroll(100, 0);
expect(viewport.getViewportScrollPosition().left)
.withContext('Expected viewport to be scrollable initially.')
.toBe(100);
overlayRef.attach(componentPortal);
expect(documentElement.style.left)
.withContext('Expected <html> element to be offset by the previous scroll amount.')
.toBe('-100px');
window.scroll(300, 0);
expect(viewport.getViewportScrollPosition().left)
.withContext('Expected the viewport not to scroll.')
.toBe(100);
overlayRef.detach();
expect(viewport.getViewportScrollPosition().left)
.withContext('Expected old scroll position to have bee restored after disabling.')
.toBe(100);
window.scroll(300, 0);
expect(viewport.getViewportScrollPosition().left)
.withContext('Expected user to be able to scroll after disabling.')
.toBe(300);
}),
);
it(
'should toggle the `cdk-global-scrollblock` class',
skipIOS(() => {
expect(documentElement.classList).not.toContain('cdk-global-scrollblock');
overlayRef.attach(componentPortal);
expect(documentElement.classList).toContain('cdk-global-scrollblock');
overlayRef.detach();
expect(documentElement.classList).not.toContain('cdk-global-scrollblock');
}),
);
it(
'should restore any previously-set inline styles',
skipIOS(() => {
const root = documentElement;
root.style.top = '13px';
root.style.left = '37px';
overlayRef.attach(componentPortal);
expect(root.style.top).not.toBe('13px');
expect(root.style.left).not.toBe('37px');
overlayRef.detach();
expect(root.style.top).toBe('13px');
expect(root.style.left).toBe('37px');
root.style.top = '';
root.style.left = '';
}),
);
it(
`should't do anything if the page isn't scrollable`,
skipIOS(() => {
forceScrollElement.style.display = 'none';
overlayRef.attach(componentPortal);
expect(documentElement.classList).not.toContain('cdk-global-scrollblock');
}),
);
it(
`should't do anything if the page isn't scrollable while zoomed out`,
skipIOS(() => {
if (platform.FIREFOX) {
// style.zoom is only supported from Firefox 126
return;
}
forceScrollElement.style.display = 'none';
document.body.style.zoom = '75%';
overlayRef.attach(componentPortal);
expect(document.body.scrollWidth).toBeGreaterThan(window.innerWidth);
expect(documentElement.classList).not.toContain('cdk-global-scrollblock');
overlayRef.detach();
document.body.style.zoom = '100%';
document.documentElement.style.zoom = '75%';
overlayRef.attach(componentPortal);
expect(document.body.scrollWidth).toBeGreaterThan(window.innerWidth);
expect(documentElement.classList).not.toContain('cdk-global-scrollblock');
document.documentElement.style.zoom = '100%';
}),
);
it(
`should add cdk-global-scrollblock while zoomed in`,
skipIOS(() => {
if (platform.FIREFOX) {
// style.zoom is only supported from Firefox 126
return;
}
forceScrollElement.style.width = window.innerWidth - 20 + 'px';
forceScrollElement.style.height = window.innerHeight - 20 + 'px';
overlayRef.attach(componentPortal);
expect(documentElement.classList).not.toContain('cdk-global-scrollblock');
overlayRef.detach();
document.body.style.zoom = '200%';
overlayRef.attach(componentPortal);
expect(documentElement.classList).toContain('cdk-global-scrollblock');
document.body.style.zoom = '100%';
overlayRef.detach();
document.documentElement.style.zoom = '200%';
overlayRef.attach(componentPortal);
expect(documentElement.classList).toContain('cdk-global-scrollblock');
document.documentElement.style.zoom = '100%';
}),
);
it('should keep the content width', () => {
forceScrollElement.style.width = '100px';
const previousContentWidth = documentElement.getBoundingClientRect().width;
overlayRef.attach(componentPortal);
expect(documentElement.getBoundingClientRect().width).toBe(previousContentWidth);
});
it(
'should not clobber user-defined scroll-behavior',
skipIOS(() => {
const root = documentElement;
const body = document.body;
const rootStyle = root.style as CSSStyleDeclaration & {scrollBehavior: string};
const bodyStyle = body.style as CSSStyleDeclaration & {scrollBehavior: string};
rootStyle.scrollBehavior = bodyStyle.scrollBehavior = 'smooth';
// Get the value via the style declaration in order to
// handle browsers that don't support the property yet.
const initialRootValue = rootStyle.scrollBehavior;
const initialBodyValue = rootStyle.scrollBehavior;
overlayRef.attach(componentPortal);
overlayRef.detach();
expect(rootStyle.scrollBehavior).toBe(initialRootValue);
expect(bodyStyle.scrollBehavior).toBe(initialBodyValue);
// Avoid bleeding styles into other tests.
rootStyle.scrollBehavior = bodyStyle.scrollBehavior = '';
}),
);
/**
* Skips the specified test, if it is being executed on iOS. This is necessary, because
* programmatic scrolling inside the Karma iframe doesn't work on iOS, which renders these
* tests unusable. For example, something as basic as the following won't work:
* ```
* window.scroll(0, 100);
* expect(viewport.getViewportScrollPosition().top).toBe(100);
* ```
* @param spec Test to be executed or skipped.
*/
function skipIOS(spec: Function) {
return () => {
if (!platform.IOS) {
spec();
}
};
}
});
/** Simple component that we can attach to the overlay. */
@Component({
template: '<p>Focaccia</p>',
imports: [OverlayModule, PortalModule],
})
class FocacciaMsg {}