@@ -10,12 +10,12 @@ describe('BlockScrollStrategy', () => {
10
10
let forceScrollElement : HTMLElement ;
11
11
12
12
beforeEach ( async ( ( ) => {
13
- TestBed . configureTestingModule ( { imports : [ OverlayModule ] } ) . compileComponents ( ) ;
13
+ TestBed . configureTestingModule ( { imports : [ OverlayModule ] } ) . compileComponents ( ) ;
14
14
} ) ) ;
15
15
16
- beforeEach ( inject ( [ ViewportRuler ] , ( _viewportRuler : ViewportRuler ) => {
17
- strategy = new BlockScrollStrategy ( _viewportRuler ) ;
18
- viewport = _viewportRuler ;
16
+ beforeEach ( inject ( [ ViewportRuler ] , ( viewportRuler : ViewportRuler ) => {
17
+ strategy = new BlockScrollStrategy ( viewportRuler ) ;
18
+ viewport = viewportRuler ;
19
19
forceScrollElement = document . createElement ( 'div' ) ;
20
20
document . body . appendChild ( forceScrollElement ) ;
21
21
forceScrollElement . style . width = '100px' ;
@@ -27,59 +27,56 @@ describe('BlockScrollStrategy', () => {
27
27
setScrollPosition ( 0 , 0 ) ;
28
28
} ) ;
29
29
30
- it ( 'should toggle scroll blocking along the y axis' , skipUnreliableBrowser ( ( ) => {
31
- forceScrollElement . style . height = '3000px' ;
32
-
30
+ it ( 'should toggle scroll blocking along the y axis' , skipIOS ( ( ) => {
33
31
setScrollPosition ( 0 , 100 ) ;
34
- expect ( viewport . getViewportScrollPosition ( ) . top ) . toBe ( 100 ,
35
- 'Expected viewport to be scrollable initially.' ) ;
32
+ expect ( viewport . getViewportScrollPosition ( ) . top )
33
+ . toBe ( 100 , 'Expected viewport to be scrollable initially.' ) ;
36
34
37
35
strategy . enable ( ) ;
38
- expect ( document . documentElement . style . top ) . toBe ( '-100px' ,
39
- ' Expected <html> element to be offset by the previous scroll amount along the y axis .') ;
36
+ expect ( document . documentElement . style . top )
37
+ . toBe ( '-100px' , ' Expected <html> element to be offset by the previous scroll amount.') ;
40
38
41
39
setScrollPosition ( 0 , 300 ) ;
42
- expect ( viewport . getViewportScrollPosition ( ) . top ) . toBe ( 100 ,
43
- 'Expected the viewport not to scroll.' ) ;
40
+ expect ( viewport . getViewportScrollPosition ( ) . top )
41
+ . toBe ( 100 , 'Expected the viewport not to scroll.' ) ;
44
42
45
43
strategy . disable ( ) ;
46
- expect ( viewport . getViewportScrollPosition ( ) . top ) . toBe ( 100 ,
47
- 'Expected old scroll position to have bee restored after disabling.' ) ;
44
+ expect ( viewport . getViewportScrollPosition ( ) . top )
45
+ . toBe ( 100 , 'Expected old scroll position to have bee restored after disabling.' ) ;
48
46
49
47
setScrollPosition ( 0 , 300 ) ;
50
- expect ( viewport . getViewportScrollPosition ( ) . top ) . toBe ( 300 ,
51
- 'Expected user to be able to scroll after disabling.' ) ;
48
+ expect ( viewport . getViewportScrollPosition ( ) . top )
49
+ . toBe ( 300 , 'Expected user to be able to scroll after disabling.' ) ;
52
50
} ) ) ;
53
51
54
52
55
- it ( 'should toggle scroll blocking along the x axis' , skipUnreliableBrowser ( ( ) => {
53
+ it ( 'should toggle scroll blocking along the x axis' , skipIOS ( ( ) => {
54
+ forceScrollElement . style . height = '100px' ;
56
55
forceScrollElement . style . width = '3000px' ;
57
56
58
57
setScrollPosition ( 100 , 0 ) ;
59
- expect ( viewport . getViewportScrollPosition ( ) . left ) . toBe ( 100 ,
60
- 'Expected viewport to be scrollable initially.' ) ;
58
+ expect ( viewport . getViewportScrollPosition ( ) . left )
59
+ . toBe ( 100 , 'Expected viewport to be scrollable initially.' ) ;
61
60
62
61
strategy . enable ( ) ;
63
- expect ( document . documentElement . style . left ) . toBe ( '-100px' ,
64
- ' Expected <html> element to be offset by the previous scroll amount along the x axis .') ;
62
+ expect ( document . documentElement . style . left )
63
+ . toBe ( '-100px' , ' Expected <html> element to be offset by the previous scroll amount.') ;
65
64
66
65
setScrollPosition ( 300 , 0 ) ;
67
- expect ( viewport . getViewportScrollPosition ( ) . left ) . toBe ( 100 ,
68
- 'Expected the viewport not to scroll.' ) ;
66
+ expect ( viewport . getViewportScrollPosition ( ) . left )
67
+ . toBe ( 100 , 'Expected the viewport not to scroll.' ) ;
69
68
70
69
strategy . disable ( ) ;
71
- expect ( viewport . getViewportScrollPosition ( ) . left ) . toBe ( 100 ,
72
- 'Expected old scroll position to have bee restored after disabling.' ) ;
70
+ expect ( viewport . getViewportScrollPosition ( ) . left )
71
+ . toBe ( 100 , 'Expected old scroll position to have bee restored after disabling.' ) ;
73
72
74
73
setScrollPosition ( 300 , 0 ) ;
75
- expect ( viewport . getViewportScrollPosition ( ) . left ) . toBe ( 300 ,
76
- 'Expected user to be able to scroll after disabling.' ) ;
74
+ expect ( viewport . getViewportScrollPosition ( ) . left )
75
+ . toBe ( 300 , 'Expected user to be able to scroll after disabling.' ) ;
77
76
} ) ) ;
78
77
79
78
80
- it ( 'should toggle the `cdk-global-scrollblock` class' , skipUnreliableBrowser ( ( ) => {
81
- forceScrollElement . style . height = '3000px' ;
82
-
79
+ it ( 'should toggle the `cdk-global-scrollblock` class' , skipIOS ( ( ) => {
83
80
expect ( document . documentElement . classList ) . not . toContain ( 'cdk-global-scrollblock' ) ;
84
81
85
82
strategy . enable ( ) ;
@@ -89,10 +86,9 @@ describe('BlockScrollStrategy', () => {
89
86
expect ( document . documentElement . classList ) . not . toContain ( 'cdk-global-scrollblock' ) ;
90
87
} ) ) ;
91
88
92
- it ( 'should restore any previously-set inline styles' , skipUnreliableBrowser ( ( ) => {
89
+ it ( 'should restore any previously-set inline styles' , skipIOS ( ( ) => {
93
90
const root = document . documentElement ;
94
91
95
- forceScrollElement . style . height = '3000px' ;
96
92
root . style . top = '13px' ;
97
93
root . style . left = '37px' ;
98
94
@@ -107,24 +103,37 @@ describe('BlockScrollStrategy', () => {
107
103
expect ( root . style . left ) . toBe ( '37px' ) ;
108
104
} ) ) ;
109
105
110
- it ( `should't do anything if the page isn't scrollable` , skipUnreliableBrowser ( ( ) => {
106
+ it ( `should't do anything if the page isn't scrollable` , skipIOS ( ( ) => {
111
107
forceScrollElement . style . display = 'none' ;
112
108
strategy . enable ( ) ;
113
109
expect ( document . documentElement . classList ) . not . toContain ( 'cdk-global-scrollblock' ) ;
114
110
} ) ) ;
115
111
116
112
117
- // In the iOS simulator (BrowserStack & SauceLabs), adding content to the
118
- // body causes karma's iframe for the test to stretch to fit that content,
119
- // in addition to not allowing us to set the scroll position programmatically.
120
- // This renders the tests unusable and since we can't really do anything about it,
121
- // we have to skip them on iOS.
122
- function skipUnreliableBrowser ( spec : Function ) {
113
+ /**
114
+ * Skips the specified test, if it is being executed on iOS. This is necessary, because
115
+ * programmatic scrolling inside the Karma iframe doesn't work on iOS, which renders these
116
+ * tests unusable. For example, something as basic as the following won't work:
117
+ * ```
118
+ * window.scroll(0, 100);
119
+ * viewport._cacheViewportGeometry();
120
+ * expect(viewport.getViewportScrollPosition().top).toBe(100);
121
+ * ```
122
+ * @param spec Test to be executed or skipped.
123
+ */
124
+ function skipIOS ( spec : Function ) {
123
125
return ( ) => {
124
- if ( ! platform . IOS ) { spec ( ) ; }
126
+ if ( ! platform . IOS ) {
127
+ spec ( ) ;
128
+ }
125
129
} ;
126
130
}
127
131
132
+ /**
133
+ * Scrolls the viewport and clears the cache.
134
+ * @param x Amount to scroll along the x axis.
135
+ * @param y Amount to scroll along the y axis.
136
+ */
128
137
function setScrollPosition ( x : number , y : number ) {
129
138
window . scroll ( x , y ) ;
130
139
viewport . _cacheViewportGeometry ( ) ;
0 commit comments