@@ -3,17 +3,11 @@ import {ScrollingModule} from './public-api';
3
3
import { ViewportRuler } from './viewport-ruler' ;
4
4
import { dispatchFakeEvent } from '@angular/cdk/testing/private' ;
5
5
import { NgZone } from '@angular/core' ;
6
-
7
- // For all tests, we assume the browser window is 1024x786 (outerWidth x outerHeight).
8
- // The karma config has been set to this for local tests, and it is the default size
9
- // for tests on CI (both SauceLabs and Browserstack).
10
-
11
- // While we know the *outer* window width/height, the innerWidth and innerHeight depend on the
12
- // the size of the individual browser's chrome, so we have to use window.innerWidth and
13
- // window.innerHeight in the unit test instead of hard-coded values.
6
+ import { Subscription } from 'rxjs' ;
14
7
15
8
describe ( 'ViewportRuler' , ( ) => {
16
- let ruler : ViewportRuler ;
9
+ let viewportRuler : ViewportRuler ;
10
+ let ngZone : NgZone ;
17
11
18
12
let startingWindowWidth = window . innerWidth ;
19
13
let startingWindowHeight = window . innerHeight ;
@@ -28,23 +22,24 @@ describe('ViewportRuler', () => {
28
22
providers : [ ViewportRuler ]
29
23
} ) ) ;
30
24
31
- beforeEach ( inject ( [ ViewportRuler ] , ( viewportRuler : ViewportRuler ) => {
32
- ruler = viewportRuler ;
25
+ beforeEach ( inject ( [ ViewportRuler , NgZone ] , ( v : ViewportRuler , n : NgZone ) => {
26
+ viewportRuler = v ;
27
+ ngZone = n ;
33
28
scrollTo ( 0 , 0 ) ;
34
29
} ) ) ;
35
30
36
31
afterEach ( ( ) => {
37
- ruler . ngOnDestroy ( ) ;
32
+ viewportRuler . ngOnDestroy ( ) ;
38
33
} ) ;
39
34
40
35
it ( 'should get the viewport size' , ( ) => {
41
- let size = ruler . getViewportSize ( ) ;
36
+ let size = viewportRuler . getViewportSize ( ) ;
42
37
expect ( size . width ) . toBe ( window . innerWidth ) ;
43
38
expect ( size . height ) . toBe ( window . innerHeight ) ;
44
39
} ) ;
45
40
46
41
it ( 'should get the viewport bounds when the page is not scrolled' , ( ) => {
47
- let bounds = ruler . getViewportRect ( ) ;
42
+ let bounds = viewportRuler . getViewportRect ( ) ;
48
43
expect ( bounds . top ) . toBe ( 0 ) ;
49
44
expect ( bounds . left ) . toBe ( 0 ) ;
50
45
expect ( bounds . bottom ) . toBe ( window . innerHeight ) ;
@@ -56,7 +51,7 @@ describe('ViewportRuler', () => {
56
51
57
52
scrollTo ( 1500 , 2000 ) ;
58
53
59
- let bounds = ruler . getViewportRect ( ) ;
54
+ let bounds = viewportRuler . getViewportRect ( ) ;
60
55
61
56
// In the iOS simulator (BrowserStack & SauceLabs), adding the content to the
62
57
// body causes karma's iframe for the test to stretch to fit that content once we attempt to
@@ -82,7 +77,7 @@ describe('ViewportRuler', () => {
82
77
} ) ;
83
78
84
79
it ( 'should get the scroll position when the page is not scrolled' , ( ) => {
85
- let scrollPos = ruler . getViewportScrollPosition ( ) ;
80
+ let scrollPos = viewportRuler . getViewportScrollPosition ( ) ;
86
81
expect ( scrollPos . top ) . toBe ( 0 ) ;
87
82
expect ( scrollPos . left ) . toBe ( 0 ) ;
88
83
} ) ;
@@ -102,7 +97,7 @@ describe('ViewportRuler', () => {
102
97
return ;
103
98
}
104
99
105
- let scrollPos = ruler . getViewportScrollPosition ( ) ;
100
+ let scrollPos = viewportRuler . getViewportScrollPosition ( ) ;
106
101
expect ( scrollPos . top ) . toBe ( 2000 ) ;
107
102
expect ( scrollPos . left ) . toBe ( 1500 ) ;
108
103
@@ -112,7 +107,7 @@ describe('ViewportRuler', () => {
112
107
describe ( 'changed event' , ( ) => {
113
108
it ( 'should dispatch an event when the window is resized' , ( ) => {
114
109
const spy = jasmine . createSpy ( 'viewport changed spy' ) ;
115
- const subscription = ruler . change ( 0 ) . subscribe ( spy ) ;
110
+ const subscription = viewportRuler . change ( 0 ) . subscribe ( spy ) ;
116
111
117
112
dispatchFakeEvent ( window , 'resize' ) ;
118
113
expect ( spy ) . toHaveBeenCalled ( ) ;
@@ -121,7 +116,7 @@ describe('ViewportRuler', () => {
121
116
122
117
it ( 'should dispatch an event when the orientation is changed' , ( ) => {
123
118
const spy = jasmine . createSpy ( 'viewport changed spy' ) ;
124
- const subscription = ruler . change ( 0 ) . subscribe ( spy ) ;
119
+ const subscription = viewportRuler . change ( 0 ) . subscribe ( spy ) ;
125
120
126
121
dispatchFakeEvent ( window , 'orientationchange' ) ;
127
122
expect ( spy ) . toHaveBeenCalled ( ) ;
@@ -130,7 +125,7 @@ describe('ViewportRuler', () => {
130
125
131
126
it ( 'should be able to throttle the callback' , fakeAsync ( ( ) => {
132
127
const spy = jasmine . createSpy ( 'viewport changed spy' ) ;
133
- const subscription = ruler . change ( 1337 ) . subscribe ( spy ) ;
128
+ const subscription = viewportRuler . change ( 1337 ) . subscribe ( spy ) ;
134
129
135
130
dispatchFakeEvent ( window , 'resize' ) ;
136
131
expect ( spy ) . not . toHaveBeenCalled ( ) ;
@@ -143,12 +138,25 @@ describe('ViewportRuler', () => {
143
138
144
139
it ( 'should run the resize event outside the NgZone' , ( ) => {
145
140
const spy = jasmine . createSpy ( 'viewport changed spy' ) ;
146
- const subscription = ruler . change ( 0 ) . subscribe ( ( ) => spy ( NgZone . isInAngularZone ( ) ) ) ;
141
+ const subscription = viewportRuler . change ( 0 ) . subscribe ( ( ) => spy ( NgZone . isInAngularZone ( ) ) ) ;
147
142
148
143
dispatchFakeEvent ( window , 'resize' ) ;
149
144
expect ( spy ) . toHaveBeenCalledWith ( false ) ;
150
145
subscription . unsubscribe ( ) ;
151
146
} ) ;
152
147
148
+ it ( 'should run events outside of the NgZone, even if the subcription is from inside' , ( ) => {
149
+ const spy = jasmine . createSpy ( 'viewport changed spy' ) ;
150
+ let subscription : Subscription ;
151
+
152
+ ngZone . run ( ( ) => {
153
+ subscription = viewportRuler . change ( 0 ) . subscribe ( ( ) => spy ( NgZone . isInAngularZone ( ) ) ) ;
154
+ dispatchFakeEvent ( window , 'resize' ) ;
155
+ } ) ;
156
+
157
+ expect ( spy ) . toHaveBeenCalledWith ( false ) ;
158
+ subscription ! . unsubscribe ( ) ;
159
+ } ) ;
160
+
153
161
} ) ;
154
162
} ) ;
0 commit comments