9
9
import { LayoutModule } from './layout-module' ;
10
10
import { BreakpointObserver , BreakpointState } from './breakpoints-observer' ;
11
11
import { MediaMatcher } from './media-matcher' ;
12
- import { fakeAsync , TestBed , inject , flush } from '@angular/core/testing' ;
12
+ import { fakeAsync , TestBed , inject , flush , tick } from '@angular/core/testing' ;
13
13
import { Injectable } from '@angular/core' ;
14
14
import { Subscription } from 'rxjs' ;
15
- import { take } from 'rxjs/operators' ;
15
+ import { skip , take } from 'rxjs/operators' ;
16
16
17
17
describe ( 'BreakpointObserver' , ( ) => {
18
18
let breakpointObserver : BreakpointObserver ;
@@ -93,10 +93,10 @@ describe('BreakpointObserver', () => {
93
93
queryMatchState = state . matches ;
94
94
} ) ;
95
95
96
- flush ( ) ;
96
+ tick ( ) ;
97
97
expect ( queryMatchState ) . toBeTruthy ( ) ;
98
98
mediaMatcher . setMatchesQuery ( query , false ) ;
99
- flush ( ) ;
99
+ tick ( ) ;
100
100
expect ( queryMatchState ) . toBeFalsy ( ) ;
101
101
} ) ) ;
102
102
@@ -108,36 +108,54 @@ describe('BreakpointObserver', () => {
108
108
breakpointObserver . observe ( [ queryOne , queryTwo ] ) . subscribe ( ( breakpoint : BreakpointState ) => {
109
109
state = breakpoint ;
110
110
} ) ;
111
+ expect ( state . breakpoints ) . toEqual ( { [ queryOne ] : true , [ queryTwo ] : true } ) ;
111
112
112
113
mediaMatcher . setMatchesQuery ( queryOne , false ) ;
113
114
mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
114
- flush ( ) ;
115
+ tick ( ) ;
115
116
expect ( state . breakpoints ) . toEqual ( { [ queryOne ] : false , [ queryTwo ] : false } ) ;
116
117
117
118
mediaMatcher . setMatchesQuery ( queryOne , true ) ;
118
119
mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
119
- flush ( ) ;
120
+ tick ( ) ;
120
121
expect ( state . breakpoints ) . toEqual ( { [ queryOne ] : true , [ queryTwo ] : false } ) ;
121
122
} ) ) ;
122
123
123
124
it ( 'emits a true matches state when the query is matched' , fakeAsync ( ( ) => {
124
125
const query = '(width: 999px)' ;
125
126
breakpointObserver . observe ( query ) . subscribe ( ) ;
126
127
mediaMatcher . setMatchesQuery ( query , true ) ;
128
+ tick ( ) ;
127
129
expect ( breakpointObserver . isMatched ( query ) ) . toBeTruthy ( ) ;
128
130
} ) ) ;
129
131
130
132
it ( 'emits a false matches state when the query is not matched' , fakeAsync ( ( ) => {
131
133
const query = '(width: 999px)' ;
132
134
breakpointObserver . observe ( query ) . subscribe ( ) ;
133
135
mediaMatcher . setMatchesQuery ( query , false ) ;
136
+ tick ( ) ;
134
137
expect ( breakpointObserver . isMatched ( query ) ) . toBeFalsy ( ) ;
135
138
} ) ) ;
136
139
140
+ it ( 'emits one event when multiple queries change' , fakeAsync ( ( ) => {
141
+ const observer = jasmine . createSpy ( 'observer' ) ;
142
+ const queryOne = '(width: 700px)' ;
143
+ const queryTwo = '(width: 999px)' ;
144
+ breakpointObserver . observe ( [ queryOne , queryTwo ] )
145
+ . pipe ( skip ( 1 ) )
146
+ . subscribe ( observer ) ;
147
+
148
+ mediaMatcher . setMatchesQuery ( queryOne , false ) ;
149
+ mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
150
+
151
+ tick ( ) ;
152
+ expect ( observer ) . toHaveBeenCalledTimes ( 1 ) ;
153
+ } ) ) ;
154
+
137
155
it ( 'should not complete other subscribers when preceding subscriber completes' , fakeAsync ( ( ) => {
138
156
const queryOne = '(width: 700px)' ;
139
157
const queryTwo = '(width: 999px)' ;
140
- const breakpoint = breakpointObserver . observe ( [ queryOne , queryTwo ] ) ;
158
+ const breakpoint = breakpointObserver . observe ( [ queryOne , queryTwo ] ) . pipe ( skip ( 1 ) ) ;
141
159
const subscriptions : Subscription [ ] = [ ] ;
142
160
let emittedValues : number [ ] = [ ] ;
143
161
@@ -148,14 +166,14 @@ describe('BreakpointObserver', () => {
148
166
149
167
mediaMatcher . setMatchesQuery ( queryOne , true ) ;
150
168
mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
151
- flush ( ) ;
169
+ tick ( ) ;
152
170
153
171
expect ( emittedValues ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
154
172
emittedValues = [ ] ;
155
173
156
174
mediaMatcher . setMatchesQuery ( queryOne , false ) ;
157
175
mediaMatcher . setMatchesQuery ( queryTwo , true ) ;
158
- flush ( ) ;
176
+ tick ( ) ;
159
177
160
178
expect ( emittedValues ) . toEqual ( [ 1 , 3 , 4 ] ) ;
161
179
@@ -172,7 +190,11 @@ export class FakeMediaQueryList {
172
190
/** Toggles the matches state and "emits" a change event. */
173
191
setMatches ( matches : boolean ) {
174
192
this . matches = matches ;
175
- this . _listeners . forEach ( listener => listener ( this as any ) ) ;
193
+
194
+ /** Simulate an asynchronous task. */
195
+ setTimeout ( ( ) => {
196
+ this . _listeners . forEach ( listener => listener ( this as any ) ) ;
197
+ } ) ;
176
198
}
177
199
178
200
/** Registers a callback method for change events. */
0 commit comments