Skip to content

Commit b590f7f

Browse files
committed
test: fix tests
1 parent 4873825 commit b590f7f

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

src/material/tabs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ ng_test_library(
9696
"//src/cdk/bidi",
9797
"//src/cdk/keycodes",
9898
"//src/cdk/observers",
99+
"//src/cdk/observers/private",
99100
"//src/cdk/portal",
100101
"//src/cdk/scrolling",
101102
"//src/cdk/testing/private",

src/material/tabs/tab-header.spec.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
import {Dir, Direction} from '@angular/cdk/bidi';
22
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';
35
import {PortalModule} from '@angular/cdk/portal';
46
import {ScrollingModule, ViewportRuler} from '@angular/cdk/scrolling';
57
import {
6-
dispatchFakeEvent,
7-
dispatchKeyboardEvent,
88
createKeyboardEvent,
9-
dispatchEvent,
109
createMouseEvent,
10+
dispatchEvent,
11+
dispatchFakeEvent,
12+
dispatchKeyboardEvent,
1113
} from '@angular/cdk/testing/private';
1214
import {CommonModule} from '@angular/common';
1315
import {Component, ViewChild} from '@angular/core';
1416
import {
15-
waitForAsync,
1617
ComponentFixture,
18+
TestBed,
1719
discardPeriodicTasks,
1820
fakeAsync,
19-
TestBed,
21+
flushMicrotasks,
2022
tick,
23+
waitForAsync,
2124
} from '@angular/core/testing';
2225
import {MatRippleModule} from '@angular/material/core';
2326
import {By} from '@angular/platform-browser';
27+
import {Subject} from 'rxjs';
2428
import {MatTabHeader} from './tab-header';
2529
import {MatTabLabelWrapper} from './tab-label-wrapper';
26-
import {ObserversModule, MutationObserverFactory} from '@angular/cdk/observers';
2730

2831
describe('MDC-based MatTabHeader', () => {
2932
let fixture: ComponentFixture<SimpleTabHeaderApp>;
3033
let appComponent: SimpleTabHeaderApp;
34+
let resizeEvents: Subject<ResizeObserverEntry[]>;
3135

3236
beforeEach(waitForAsync(() => {
3337
TestBed.configureTestingModule({
@@ -45,6 +49,9 @@ describe('MDC-based MatTabHeader', () => {
4549
});
4650

4751
TestBed.compileComponents();
52+
53+
resizeEvents = new Subject();
54+
spyOn(TestBed.inject(SharedResizeObserver), 'observe').and.returnValue(resizeEvents);
4855
}));
4956

5057
describe('focusing', () => {
@@ -650,48 +657,45 @@ describe('MDC-based MatTabHeader', () => {
650657
expect(inkBar.alignToElement).toHaveBeenCalled();
651658
}));
652659

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(() => {
654661
fixture = TestBed.createComponent(SimpleTabHeaderApp);
655662
fixture.detectChanges();
656663

657664
const inkBar = fixture.componentInstance.tabHeader._inkBar;
658665

659666
spyOn(inkBar, 'alignToElement');
660667

661-
dispatchFakeEvent(window, 'resize');
662-
tick(150);
668+
resizeEvents.next([]);
663669
fixture.detectChanges();
670+
flushMicrotasks();
664671

665672
expect(inkBar.alignToElement).toHaveBeenCalled();
666673
discardPeriodicTasks();
667674
}));
668675

669-
it('should update arrows when the window is resized', fakeAsync(() => {
676+
it('should update arrows when the header is resized', fakeAsync(() => {
670677
fixture = TestBed.createComponent(SimpleTabHeaderApp);
671678

672679
const header = fixture.componentInstance.tabHeader;
673680

674681
spyOn(header, '_checkPaginationEnabled');
675682

676-
dispatchFakeEvent(window, 'resize');
677-
tick(10);
683+
resizeEvents.next([]);
678684
fixture.detectChanges();
685+
flushMicrotasks();
679686

680687
expect(header._checkPaginationEnabled).toHaveBeenCalled();
681688
discardPeriodicTasks();
682689
}));
683690

684691
it('should update the pagination state if the content of the labels changes', () => {
685692
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;
693697
},
694-
});
698+
);
695699

696700
fixture = TestBed.createComponent(SimpleTabHeaderApp);
697701
fixture.detectChanges();

src/material/tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1+
import {Direction, Directionality} from '@angular/cdk/bidi';
12
import {ENTER, SPACE} from '@angular/cdk/keycodes';
2-
import {waitForAsync, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
3-
import {Component, QueryList, ViewChild, ViewChildren} from '@angular/core';
4-
import {MAT_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '@angular/material/core';
5-
import {By} from '@angular/platform-browser';
3+
import {SharedResizeObserver} from '@angular/cdk/observers/private';
64
import {
75
dispatchFakeEvent,
86
dispatchKeyboardEvent,
97
dispatchMouseEvent,
108
} from '@angular/cdk/testing/private';
11-
import {Direction, Directionality} from '@angular/cdk/bidi';
9+
import {Component, QueryList, ViewChild, ViewChildren} from '@angular/core';
10+
import {
11+
ComponentFixture,
12+
TestBed,
13+
fakeAsync,
14+
flushMicrotasks,
15+
tick,
16+
waitForAsync,
17+
} from '@angular/core/testing';
18+
import {MAT_RIPPLE_GLOBAL_OPTIONS, RippleGlobalOptions} from '@angular/material/core';
19+
import {By} from '@angular/platform-browser';
20+
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
1221
import {Subject} from 'rxjs';
22+
import {MAT_TABS_CONFIG} from '../index';
1323
import {MatTabsModule} from '../module';
1424
import {MatTabLink, MatTabNav} from './tab-nav-bar';
15-
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
16-
import {MAT_TABS_CONFIG} from '../index';
1725

1826
describe('MDC-based MatTabNavBar', () => {
1927
let dir: Direction = 'ltr';
2028
let dirChange = new Subject();
2129
let globalRippleOptions: RippleGlobalOptions;
30+
let resizeEvents: Subject<ResizeObserverEntry[]>;
2231

2332
beforeEach(waitForAsync(() => {
2433
globalRippleOptions = {};
@@ -37,6 +46,9 @@ describe('MDC-based MatTabNavBar', () => {
3746
});
3847

3948
TestBed.compileComponents();
49+
50+
resizeEvents = new Subject();
51+
spyOn(TestBed.inject(SharedResizeObserver), 'observe').and.returnValue(resizeEvents);
4052
}));
4153

4254
describe('basic behavior', () => {
@@ -174,14 +186,14 @@ describe('MDC-based MatTabNavBar', () => {
174186
expect(spy.calls.any()).toBe(false);
175187
});
176188

177-
it('should re-align the ink bar when the window is resized', fakeAsync(() => {
189+
it('should re-align the ink bar when the nav bar is resized', fakeAsync(() => {
178190
const inkBar = fixture.componentInstance.tabNavBar._inkBar;
179191

180192
spyOn(inkBar, 'alignToElement');
181193

182-
dispatchFakeEvent(window, 'resize');
183-
tick(150);
194+
resizeEvents.next([]);
184195
fixture.detectChanges();
196+
flushMicrotasks();
185197

186198
expect(inkBar.alignToElement).toHaveBeenCalled();
187199
}));

0 commit comments

Comments
 (0)