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