6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { TestBed , inject } from '@angular/core/testing' ;
9
+ import { BreakPoint } from '@angular/flex-layout/core' ;
10
+ import { Subscription } from 'rxjs' ;
9
11
10
12
import { MediaChange } from '../media-change' ;
11
13
import { MockMatchMedia , MockMatchMediaProvider } from './mock/mock-match-media' ;
@@ -16,7 +18,6 @@ import {MediaObserver} from '../media-observer/media-observer';
16
18
describe ( 'match-media' , ( ) => {
17
19
let breakPoints : BreakPointRegistry ;
18
20
let mediaController : MockMatchMedia ;
19
- let mediaObserver : MediaObserver ;
20
21
21
22
beforeEach ( ( ) => {
22
23
// Configure testbed to prepare services
@@ -31,11 +32,11 @@ describe('match-media', () => {
31
32
_breakPoints : BreakPointRegistry ) => {
32
33
breakPoints = _breakPoints ;
33
34
mediaController = _matchMedia ; // inject only to manually activate mediaQuery ranges
34
- mediaObserver = _mediaObserver ;
35
35
} ) ) ;
36
36
37
37
afterEach ( ( ) => {
38
- mediaController . clearAll ( ) ;
38
+ mediaController . clearAll ( ) ;
39
+ mediaController . useOverlaps = false ;
39
40
} ) ;
40
41
41
42
it ( 'can observe the initial, default activation for mediaQuery == "all". ' , ( ) => {
@@ -101,38 +102,41 @@ describe('match-media', () => {
101
102
} ) ;
102
103
103
104
describe ( 'match-media-observable' , ( ) => {
105
+ const watchMedia = ( alias : string , observer : ( value : MediaChange ) => void ) : Subscription => {
106
+ return mediaController
107
+ . observe ( alias ? [ alias ] : [ ] )
108
+ . subscribe ( observer ) ;
109
+ } ;
104
110
105
111
it ( 'can observe an existing activation' , ( ) => {
106
112
let current : MediaChange = new MediaChange ( ) ;
107
113
let bp = breakPoints . findByAlias ( 'md' ) ! ;
108
- mediaController . activate ( bp . mediaQuery ) ;
109
- let subscription = mediaObserver . media$ . subscribe ( ( change : MediaChange ) => {
110
- current = change ;
111
- } ) ;
114
+ const onChange = ( change : MediaChange ) => current = change ;
115
+ const subscription = watchMedia ( 'md' , onChange ) ;
112
116
117
+ mediaController . activate ( bp . mediaQuery ) ;
113
118
expect ( current . mediaQuery ) . toEqual ( bp . mediaQuery ) ;
114
119
subscription . unsubscribe ( ) ;
115
120
} ) ;
116
121
117
122
it ( 'can observe the initial, default activation for mediaQuery == "all". ' , ( ) => {
118
123
let current : MediaChange = new MediaChange ( ) ;
119
- let subscription = mediaObserver . media$ . subscribe ( ( change : MediaChange ) => {
120
- current = change ;
121
- } ) ;
124
+ const onChange = ( change : MediaChange ) => current = change ;
125
+ const subscription = watchMedia ( '' , onChange ) ;
122
126
123
127
expect ( current . mediaQuery ) . toEqual ( 'all' ) ;
124
128
subscription . unsubscribe ( ) ;
125
129
} ) ;
126
130
127
131
it ( 'can observe custom mediaQuery ranges' , ( ) => {
128
132
let current : MediaChange = new MediaChange ( ) ;
129
- let customQuery = 'screen and (min-width: 610px) and (max-width: 620px)' ;
130
- let subscription = mediaObserver . media$ . subscribe ( ( change : MediaChange ) => {
131
- current = change ;
132
- } ) ;
133
+ const customQuery = 'screen and (min-width: 610px) and (max-width: 620px)' ;
134
+ const onChange = ( change : MediaChange ) => current = change ;
135
+ const subscription = watchMedia ( customQuery , onChange ) ;
133
136
134
137
mediaController . useOverlaps = true ;
135
- let activated = mediaController . activate ( customQuery ) ;
138
+ const activated = mediaController . activate ( customQuery ) ;
139
+
136
140
expect ( activated ) . toEqual ( true ) ;
137
141
expect ( current . mediaQuery ) . toEqual ( customQuery ) ;
138
142
@@ -141,46 +145,47 @@ describe('match-media', () => {
141
145
142
146
it ( 'can observe registered breakpoint activations' , ( ) => {
143
147
let current : MediaChange = new MediaChange ( ) ;
144
- let bp = breakPoints . findByAlias ( 'md' ) ! ;
145
- let subscription = mediaObserver . media$ . subscribe ( ( change : MediaChange ) => {
146
- current = change ;
147
- } ) ;
148
+ const onChange = ( change : MediaChange ) => current = change ;
149
+ const subscription = watchMedia ( 'md' , onChange ) ;
148
150
151
+ let bp = breakPoints . findByAlias ( 'md' ) ! ;
149
152
let activated = mediaController . activate ( bp . mediaQuery ) ;
150
- expect ( activated ) . toEqual ( true ) ;
151
153
154
+ expect ( activated ) . toEqual ( true ) ;
152
155
expect ( current . mediaQuery ) . toEqual ( bp . mediaQuery ) ;
153
156
154
157
subscription . unsubscribe ( ) ;
155
158
} ) ;
156
159
157
160
/**
158
- * Only the MediaObserver ignores de-activations;
159
161
* MediaMonitor and MatchMedia report both activations and de-activations!
162
+ * Only the MediaObserver ignores de-activations;
160
163
*/
161
- it ( 'ignores mediaQuery de-activations' , ( ) => {
162
- let activationCount = 0 ;
163
- let deactivationCount = 0 ;
164
-
165
- mediaObserver . filterOverlaps = false ;
166
- let subscription = mediaObserver . media$ . subscribe ( ( change : MediaChange ) => {
164
+ it ( 'reports mediaQuery de-activations' , ( ) => {
165
+ const lookupMediaQuery = ( alias : string ) => {
166
+ const bp : BreakPoint = breakPoints . findByAlias ( alias ) as BreakPoint ;
167
+ return bp . mediaQuery ;
168
+ } ;
169
+ let activationCount = 0 , deactivationCount = 0 ;
170
+ let subscription = watchMedia ( '' , ( change : MediaChange ) => {
167
171
if ( change . matches ) {
168
- ++ activationCount ;
172
+ activationCount += 1 ;
169
173
} else {
170
- ++ deactivationCount ;
174
+ deactivationCount += 1 ;
171
175
}
172
176
} ) ;
173
177
174
- mediaController . activate ( breakPoints . findByAlias ( 'md' ) ! . mediaQuery ) ;
175
- mediaController . activate ( breakPoints . findByAlias ( 'gt-md' ) ! . mediaQuery ) ;
176
- mediaController . activate ( breakPoints . findByAlias ( 'lg' ) ! . mediaQuery ) ;
178
+ mediaController . activate ( lookupMediaQuery ( 'md' ) ) ;
179
+ mediaController . activate ( lookupMediaQuery ( 'gt-md' ) ) ;
180
+ mediaController . activate ( lookupMediaQuery ( 'lg' ) ) ;
177
181
178
182
// 'all' mediaQuery is already active; total count should be (3)
179
183
expect ( activationCount ) . toEqual ( 4 ) ;
180
- expect ( deactivationCount ) . toEqual ( 0 ) ;
184
+ expect ( deactivationCount ) . toEqual ( 2 ) ;
181
185
182
186
subscription . unsubscribe ( ) ;
183
187
} ) ;
184
188
185
189
} ) ;
186
190
} ) ;
191
+
0 commit comments