88
99import { BreakpointObserver , BreakpointState } from './breakpoints-observer' ;
1010import { MediaMatcher } from './media-matcher' ;
11- import { fakeAsync , TestBed , flush , tick } from '@angular/core/testing' ;
11+ import { TestBed } from '@angular/core/testing' ;
1212import { Service } from '@angular/core' ;
1313import { Subscription } from 'rxjs' ;
1414import { skip , take } from 'rxjs/operators' ;
1515
1616describe ( 'BreakpointObserver' , ( ) => {
17+ const debounceTime = 20 ;
1718 let breakpointObserver : BreakpointObserver ;
1819 let mediaMatcher : FakeMediaMatcher ;
1920
20- beforeEach ( fakeAsync ( ( ) => {
21+ beforeEach ( ( ) => {
2122 TestBed . configureTestingModule ( {
2223 providers : [ { provide : MediaMatcher , useClass : FakeMediaMatcher } ] ,
2324 } ) ;
2425 breakpointObserver = TestBed . inject ( BreakpointObserver ) ;
2526 mediaMatcher = TestBed . inject ( MediaMatcher ) as unknown as FakeMediaMatcher ;
26- } ) ) ;
27+ } ) ;
2728
2829 afterEach ( ( ) => {
2930 mediaMatcher . clear ( ) ;
3031 } ) ;
3132
32- it ( 'retrieves the whether a query is currently matched' , fakeAsync ( ( ) => {
33+ function wait ( milliseconds : number ) {
34+ return new Promise ( resolve => setTimeout ( resolve , milliseconds ) ) ;
35+ }
36+
37+ it ( 'retrieves the whether a query is currently matched' , ( ) => {
3338 const query = 'everything starts as true in the FakeMediaMatcher' ;
3439 expect ( breakpointObserver . isMatched ( query ) ) . toBeTruthy ( ) ;
35- } ) ) ;
40+ } ) ;
3641
37- it ( 'reuses the same MediaQueryList for matching queries' , fakeAsync ( ( ) => {
42+ it ( 'reuses the same MediaQueryList for matching queries' , ( ) => {
3843 expect ( mediaMatcher . queryCount ) . toBe ( 0 ) ;
3944 breakpointObserver . observe ( 'query1' ) ;
4045 expect ( mediaMatcher . queryCount ) . toBe ( 1 ) ;
@@ -44,56 +49,54 @@ describe('BreakpointObserver', () => {
4449 expect ( mediaMatcher . queryCount ) . toBe ( 2 ) ;
4550 breakpointObserver . observe ( 'query1' ) ;
4651 expect ( mediaMatcher . queryCount ) . toBe ( 2 ) ;
47- } ) ) ;
52+ } ) ;
4853
49- it ( 'splits combined query strings into individual matchMedia listeners' , fakeAsync ( ( ) => {
54+ it ( 'splits combined query strings into individual matchMedia listeners' , ( ) => {
5055 expect ( mediaMatcher . queryCount ) . toBe ( 0 ) ;
5156 breakpointObserver . observe ( 'query1, query2' ) ;
5257 expect ( mediaMatcher . queryCount ) . toBe ( 2 ) ;
5358 breakpointObserver . observe ( 'query1' ) ;
5459 expect ( mediaMatcher . queryCount ) . toBe ( 2 ) ;
5560 breakpointObserver . observe ( 'query2, query3' ) ;
5661 expect ( mediaMatcher . queryCount ) . toBe ( 3 ) ;
57- } ) ) ;
62+ } ) ;
5863
59- it ( 'accepts an array of queries' , fakeAsync ( ( ) => {
64+ it ( 'accepts an array of queries' , ( ) => {
6065 const queries = [ '1 query' , '2 query' , 'red query' , 'blue query' ] ;
6166 breakpointObserver . observe ( queries ) ;
6267 expect ( mediaMatcher . queryCount ) . toBe ( queries . length ) ;
63- } ) ) ;
68+ } ) ;
6469
65- it ( 'completes all events when the breakpoint manager is destroyed' , fakeAsync ( ( ) => {
70+ it ( 'completes all events when the breakpoint manager is destroyed' , ( ) => {
6671 const firstTest = jasmine . createSpy ( 'test1' ) ;
6772 breakpointObserver . observe ( 'test1' ) . subscribe ( { complete : firstTest } ) ;
6873 const secondTest = jasmine . createSpy ( 'test2' ) ;
6974 breakpointObserver . observe ( 'test2' ) . subscribe ( { complete : secondTest } ) ;
7075
71- flush ( ) ;
7276 expect ( firstTest ) . not . toHaveBeenCalled ( ) ;
7377 expect ( secondTest ) . not . toHaveBeenCalled ( ) ;
7478
7579 breakpointObserver . ngOnDestroy ( ) ;
76- flush ( ) ;
7780
7881 expect ( firstTest ) . toHaveBeenCalled ( ) ;
7982 expect ( secondTest ) . toHaveBeenCalled ( ) ;
80- } ) ) ;
83+ } ) ;
8184
82- it ( 'emits an event on the observable when values change' , fakeAsync ( ( ) => {
85+ it ( 'emits an event on the observable when values change' , async ( ) => {
8386 const query = '(width: 999px)' ;
8487 let queryMatchState = false ;
8588 breakpointObserver . observe ( query ) . subscribe ( ( state : BreakpointState ) => {
8689 queryMatchState = state . matches ;
8790 } ) ;
8891
89- tick ( ) ;
92+ await wait ( debounceTime ) ;
9093 expect ( queryMatchState ) . toBeTruthy ( ) ;
9194 mediaMatcher . setMatchesQuery ( query , false ) ;
92- tick ( ) ;
95+ await wait ( debounceTime ) ;
9396 expect ( queryMatchState ) . toBeFalsy ( ) ;
94- } ) ) ;
97+ } ) ;
9598
96- it ( 'emits an event on the observable with the matching state of all queries provided' , fakeAsync ( ( ) => {
99+ it ( 'emits an event on the observable with the matching state of all queries provided' , async ( ) => {
97100 const queryOne = '(width: 999px)' ;
98101 const queryTwo = '(width: 700px)' ;
99102 let state : BreakpointState = { matches : false , breakpoints : { } } ;
@@ -104,32 +107,31 @@ describe('BreakpointObserver', () => {
104107
105108 mediaMatcher . setMatchesQuery ( queryOne , false ) ;
106109 mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
107- tick ( ) ;
110+ await wait ( debounceTime ) ;
108111 expect ( state . breakpoints ) . toEqual ( { [ queryOne ] : false , [ queryTwo ] : false } ) ;
109112
110113 mediaMatcher . setMatchesQuery ( queryOne , true ) ;
111114 mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
112- tick ( ) ;
115+ await wait ( debounceTime ) ;
113116 expect ( state . breakpoints ) . toEqual ( { [ queryOne ] : true , [ queryTwo ] : false } ) ;
114- } ) ) ;
117+ } ) ;
115118
116- it ( 'emits a true matches state when the query is matched' , fakeAsync ( ( ) => {
119+ it ( 'emits a true matches state when the query is matched' , ( ) => {
117120 const query = '(width: 999px)' ;
118121 breakpointObserver . observe ( query ) . subscribe ( ) ;
119122 mediaMatcher . setMatchesQuery ( query , true ) ;
120- tick ( ) ;
121123 expect ( breakpointObserver . isMatched ( query ) ) . toBeTruthy ( ) ;
122- } ) ) ;
124+ } ) ;
123125
124- it ( 'emits a false matches state when the query is not matched' , fakeAsync ( ( ) => {
126+ it ( 'emits a false matches state when the query is not matched' , async ( ) => {
125127 const query = '(width: 999px)' ;
126128 breakpointObserver . observe ( query ) . subscribe ( ) ;
127129 mediaMatcher . setMatchesQuery ( query , false ) ;
128- tick ( ) ;
130+ await wait ( debounceTime ) ;
129131 expect ( breakpointObserver . isMatched ( query ) ) . toBeFalsy ( ) ;
130- } ) ) ;
132+ } ) ;
131133
132- it ( 'emits one event when multiple queries change' , fakeAsync ( ( ) => {
134+ it ( 'emits one event when multiple queries change' , async ( ) => {
133135 const observer = jasmine . createSpy ( 'observer' ) ;
134136 const queryOne = '(width: 700px)' ;
135137 const queryTwo = '(width: 999px)' ;
@@ -138,11 +140,11 @@ describe('BreakpointObserver', () => {
138140 mediaMatcher . setMatchesQuery ( queryOne , false ) ;
139141 mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
140142
141- tick ( ) ;
143+ await wait ( debounceTime ) ;
142144 expect ( observer ) . toHaveBeenCalledTimes ( 1 ) ;
143- } ) ) ;
145+ } ) ;
144146
145- it ( 'should not complete other subscribers when preceding subscriber completes' , fakeAsync ( ( ) => {
147+ it ( 'should not complete other subscribers when preceding subscriber completes' , async ( ) => {
146148 const queryOne = '(width: 700px)' ;
147149 const queryTwo = '(width: 999px)' ;
148150 const breakpoint = breakpointObserver . observe ( [ queryOne , queryTwo ] ) . pipe ( skip ( 1 ) ) ;
@@ -156,19 +158,19 @@ describe('BreakpointObserver', () => {
156158
157159 mediaMatcher . setMatchesQuery ( queryOne , true ) ;
158160 mediaMatcher . setMatchesQuery ( queryTwo , false ) ;
159- tick ( ) ;
161+ await wait ( debounceTime ) ;
160162
161163 expect ( emittedValues ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
162164 emittedValues = [ ] ;
163165
164166 mediaMatcher . setMatchesQuery ( queryOne , false ) ;
165167 mediaMatcher . setMatchesQuery ( queryTwo , true ) ;
166- tick ( ) ;
168+ await wait ( debounceTime ) ;
167169
168170 expect ( emittedValues ) . toEqual ( [ 1 , 3 , 4 ] ) ;
169171
170172 subscriptions . forEach ( subscription => subscription . unsubscribe ( ) ) ;
171- } ) ) ;
173+ } ) ;
172174} ) ;
173175
174176export class FakeMediaQueryList {
0 commit comments