1
+ import { TestBed , async , inject } from '@angular/core/testing' ;
2
+ import { OverlayModule } from './overlay-directives' ;
1
3
import { DisableBodyScroll } from './disable-body-scroll' ;
2
4
3
5
4
6
describe ( 'DisableBodyScroll' , ( ) => {
5
7
let service : DisableBodyScroll ;
6
- let forceScrollElement : HTMLElement ;
8
+ let startingWindowHeight = window . innerHeight ;
9
+ let forceScrollElement : HTMLElement = document . createElement ( 'div' ) ;
7
10
8
- beforeEach ( ( ) => {
9
- forceScrollElement = document . createElement ( 'div' ) ;
10
- forceScrollElement . style . height = '3000px' ;
11
- document . body . appendChild ( forceScrollElement ) ;
12
- service = new DisableBodyScroll ( ) ;
13
- } ) ;
11
+ forceScrollElement . style . height = '3000px' ;
12
+ forceScrollElement . style . width = '100px' ;
13
+
14
+ beforeEach ( async ( ( ) => {
15
+ TestBed . configureTestingModule ( {
16
+ imports : [ OverlayModule . forRoot ( ) ]
17
+ } ) ;
18
+ } ) ) ;
19
+
20
+ beforeEach ( inject ( [ DisableBodyScroll ] , ( disableBodyScroll : DisableBodyScroll ) => {
21
+ service = disableBodyScroll ;
22
+ } ) ) ;
14
23
15
24
afterEach ( ( ) => {
16
- forceScrollElement . parentNode . removeChild ( forceScrollElement ) ;
17
- forceScrollElement = null ;
25
+ if ( forceScrollElement . parentNode ) {
26
+ forceScrollElement . parentNode . removeChild ( forceScrollElement ) ;
27
+ }
28
+
18
29
service . deactivate ( ) ;
19
30
} ) ;
20
31
21
32
it ( 'should prevent scrolling' , ( ) => {
22
- window . scroll ( 0 , 0 ) ;
33
+ document . body . appendChild ( forceScrollElement ) ;
34
+ window . scrollTo ( 0 , 100 ) ;
35
+
36
+ // In the iOS simulator (BrowserStack & SauceLabs), adding the content to the
37
+ // body causes karma's iframe for the test to stretch to fit that content once we attempt to
38
+ // scroll the page. Setting width / height / maxWidth / maxHeight on the iframe does not
39
+ // successfully constrain its size. As such, skip assertions in environments where the
40
+ // window size has changed since the start of the test.
41
+ if ( window . innerHeight > startingWindowHeight ) {
42
+ return ;
43
+ }
44
+
45
+ window . scrollTo ( 0 , 100 ) ;
23
46
24
47
service . activate ( ) ;
25
48
26
- window . scroll ( 0 , 500 ) ;
49
+ window . scrollTo ( 0 , 500 ) ;
27
50
28
51
expect ( window . pageYOffset ) . toBe ( 0 ) ;
29
52
} ) ;
30
53
31
54
it ( 'should toggle the isActive property' , ( ) => {
55
+ document . body . appendChild ( forceScrollElement ) ;
56
+ window . scrollTo ( 0 , 100 ) ;
57
+
58
+ // In the iOS simulator (BrowserStack & SauceLabs), adding the content to the
59
+ // body causes karma's iframe for the test to stretch to fit that content once we attempt to
60
+ // scroll the page. Setting width / height / maxWidth / maxHeight on the iframe does not
61
+ // successfully constrain its size. As such, skip assertions in environments where the
62
+ // window size has changed since the start of the test.
63
+ if ( window . innerHeight > startingWindowHeight ) {
64
+ return ;
65
+ }
66
+
32
67
service . activate ( ) ;
33
68
expect ( service . isActive ) . toBe ( true ) ;
34
69
@@ -37,16 +72,26 @@ describe('DisableBodyScroll', () => {
37
72
} ) ;
38
73
39
74
it ( 'should not disable scrolling if the content is shorter than the viewport height' , ( ) => {
40
- forceScrollElement . style . height = '0' ;
41
75
service . activate ( ) ;
42
76
expect ( service . isActive ) . toBe ( false ) ;
43
77
} ) ;
44
78
45
79
it ( 'should add the proper inline styles to the <body> and <html> nodes' , ( ) => {
80
+ document . body . appendChild ( forceScrollElement ) ;
81
+ window . scrollTo ( 0 , 500 ) ;
82
+
83
+ // In the iOS simulator (BrowserStack & SauceLabs), adding the content to the
84
+ // body causes karma's iframe for the test to stretch to fit that content once we attempt to
85
+ // scroll the page. Setting width / height / maxWidth / maxHeight on the iframe does not
86
+ // successfully constrain its size. As such, skip assertions in environments where the
87
+ // window size has changed since the start of the test.
88
+ if ( window . innerHeight > startingWindowHeight ) {
89
+ return ;
90
+ }
91
+
46
92
let bodyCSS = document . body . style ;
47
93
let htmlCSS = document . documentElement . style ;
48
94
49
- window . scroll ( 0 , 500 ) ;
50
95
service . activate ( ) ;
51
96
52
97
expect ( bodyCSS . position ) . toBe ( 'fixed' ) ;
@@ -60,6 +105,8 @@ describe('DisableBodyScroll', () => {
60
105
let bodyCSS = document . body . style ;
61
106
let htmlCSS = document . documentElement . style ;
62
107
108
+ document . body . appendChild ( forceScrollElement ) ;
109
+
63
110
bodyCSS . position = 'static' ;
64
111
bodyCSS . width = '1000px' ;
65
112
htmlCSS . overflowY = 'hidden' ;
@@ -76,7 +123,17 @@ describe('DisableBodyScroll', () => {
76
123
} ) ;
77
124
78
125
it ( 'should restore the scroll position when enabling scrolling' , ( ) => {
79
- window . scroll ( 0 , 1000 ) ;
126
+ document . body . appendChild ( forceScrollElement ) ;
127
+ window . scrollTo ( 0 , 1000 ) ;
128
+
129
+ // In the iOS simulator (BrowserStack & SauceLabs), adding the content to the
130
+ // body causes karma's iframe for the test to stretch to fit that content once we attempt to
131
+ // scroll the page. Setting width / height / maxWidth / maxHeight on the iframe does not
132
+ // successfully constrain its size. As such, skip assertions in environments where the
133
+ // window size has changed since the start of the test.
134
+ if ( window . innerHeight > startingWindowHeight ) {
135
+ return ;
136
+ }
80
137
81
138
service . activate ( ) ;
82
139
service . deactivate ( ) ;
0 commit comments