1
1
import { Dir , Direction } from '@angular/cdk/bidi' ;
2
2
import { END , ENTER , HOME , LEFT_ARROW , RIGHT_ARROW , SPACE } from '@angular/cdk/keycodes' ;
3
+ import { MutationObserverFactory , ObserversModule } from '@angular/cdk/observers' ;
4
+ import { SharedResizeObserver } from '@angular/cdk/observers/private' ;
3
5
import { PortalModule } from '@angular/cdk/portal' ;
4
6
import { ScrollingModule , ViewportRuler } from '@angular/cdk/scrolling' ;
5
7
import {
6
- dispatchFakeEvent ,
7
- dispatchKeyboardEvent ,
8
8
createKeyboardEvent ,
9
- dispatchEvent ,
10
9
createMouseEvent ,
10
+ dispatchEvent ,
11
+ dispatchFakeEvent ,
12
+ dispatchKeyboardEvent ,
11
13
} from '@angular/cdk/testing/private' ;
12
14
import { CommonModule } from '@angular/common' ;
13
15
import { Component , ViewChild } from '@angular/core' ;
14
16
import {
15
- waitForAsync ,
16
17
ComponentFixture ,
18
+ TestBed ,
17
19
discardPeriodicTasks ,
18
20
fakeAsync ,
19
- TestBed ,
21
+ flushMicrotasks ,
20
22
tick ,
23
+ waitForAsync ,
21
24
} from '@angular/core/testing' ;
22
25
import { MatRippleModule } from '@angular/material/core' ;
23
26
import { By } from '@angular/platform-browser' ;
27
+ import { Subject } from 'rxjs' ;
24
28
import { MatTabHeader } from './tab-header' ;
25
29
import { MatTabLabelWrapper } from './tab-label-wrapper' ;
26
- import { ObserversModule , MutationObserverFactory } from '@angular/cdk/observers' ;
27
30
28
31
describe ( 'MDC-based MatTabHeader' , ( ) => {
29
32
let fixture : ComponentFixture < SimpleTabHeaderApp > ;
30
33
let appComponent : SimpleTabHeaderApp ;
34
+ let resizeEvents : Subject < ResizeObserverEntry [ ] > ;
31
35
32
36
beforeEach ( waitForAsync ( ( ) => {
33
37
TestBed . configureTestingModule ( {
@@ -45,6 +49,9 @@ describe('MDC-based MatTabHeader', () => {
45
49
} ) ;
46
50
47
51
TestBed . compileComponents ( ) ;
52
+
53
+ resizeEvents = new Subject ( ) ;
54
+ spyOn ( TestBed . inject ( SharedResizeObserver ) , 'observe' ) . and . returnValue ( resizeEvents ) ;
48
55
} ) ) ;
49
56
50
57
describe ( 'focusing' , ( ) => {
@@ -650,48 +657,45 @@ describe('MDC-based MatTabHeader', () => {
650
657
expect ( inkBar . alignToElement ) . toHaveBeenCalled ( ) ;
651
658
} ) ) ;
652
659
653
- it ( 'should re-align the ink bar when the window is resized' , fakeAsync ( ( ) => {
660
+ it ( 'should re-align the ink bar when the header is resized' , fakeAsync ( ( ) => {
654
661
fixture = TestBed . createComponent ( SimpleTabHeaderApp ) ;
655
662
fixture . detectChanges ( ) ;
656
663
657
664
const inkBar = fixture . componentInstance . tabHeader . _inkBar ;
658
665
659
666
spyOn ( inkBar , 'alignToElement' ) ;
660
667
661
- dispatchFakeEvent ( window , 'resize' ) ;
662
- tick ( 150 ) ;
668
+ resizeEvents . next ( [ ] ) ;
663
669
fixture . detectChanges ( ) ;
670
+ flushMicrotasks ( ) ;
664
671
665
672
expect ( inkBar . alignToElement ) . toHaveBeenCalled ( ) ;
666
673
discardPeriodicTasks ( ) ;
667
674
} ) ) ;
668
675
669
- it ( 'should update arrows when the window is resized' , fakeAsync ( ( ) => {
676
+ it ( 'should update arrows when the header is resized' , fakeAsync ( ( ) => {
670
677
fixture = TestBed . createComponent ( SimpleTabHeaderApp ) ;
671
678
672
679
const header = fixture . componentInstance . tabHeader ;
673
680
674
681
spyOn ( header , '_checkPaginationEnabled' ) ;
675
682
676
- dispatchFakeEvent ( window , 'resize' ) ;
677
- tick ( 10 ) ;
683
+ resizeEvents . next ( [ ] ) ;
678
684
fixture . detectChanges ( ) ;
685
+ flushMicrotasks ( ) ;
679
686
680
687
expect ( header . _checkPaginationEnabled ) . toHaveBeenCalled ( ) ;
681
688
discardPeriodicTasks ( ) ;
682
689
} ) ) ;
683
690
684
691
it ( 'should update the pagination state if the content of the labels changes' , ( ) => {
685
692
const mutationCallbacks : Function [ ] = [ ] ;
686
- TestBed . overrideProvider ( MutationObserverFactory , {
687
- useValue : {
688
- // Stub out the MutationObserver since the native one is async.
689
- create : function ( callback : Function ) {
690
- mutationCallbacks . push ( callback ) ;
691
- return { observe : ( ) => { } , disconnect : ( ) => { } } ;
692
- } ,
693
+ spyOn ( TestBed . inject ( MutationObserverFactory ) , 'create' ) . and . callFake (
694
+ ( callback : Function ) => {
695
+ mutationCallbacks . push ( callback ) ;
696
+ return { observe : ( ) => { } , disconnect : ( ) => { } } as any ;
693
697
} ,
694
- } ) ;
698
+ ) ;
695
699
696
700
fixture = TestBed . createComponent ( SimpleTabHeaderApp ) ;
697
701
fixture . detectChanges ( ) ;
0 commit comments