Skip to content

Commit 4db68f6

Browse files
committed
test(sidenav): refactor flaky tests in Edge
Refactors some tests to (hopefully) be less flaky in Edge. In two of them I've replaced calls to `getComputedStyle` to a check against a class in the DOM. On other one I've introduced a delay to allow the browser more time to recalculate the element dimensions.
1 parent 88631b9 commit 4db68f6

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

src/material/sidenav/drawer.spec.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,34 @@ describe('MatDrawer', () => {
4141
describe('methods', () => {
4242
it('should be able to open', fakeAsync(() => {
4343
const fixture = TestBed.createComponent(BasicTestApp);
44-
4544
fixture.detectChanges();
4645

4746
const testComponent: BasicTestApp = fixture.debugElement.componentInstance;
48-
const drawer = fixture.debugElement.query(By.directive(MatDrawer));
49-
const drawerBackdropElement = fixture.debugElement.query(By.css('.mat-drawer-backdrop'));
47+
const container = fixture.debugElement.query(By.css('mat-drawer-container')).nativeElement;
5048

51-
drawerBackdropElement.nativeElement.style.transition = 'none';
5249
fixture.debugElement.query(By.css('.open')).nativeElement.click();
5350
fixture.detectChanges();
5451

5552
expect(testComponent.openCount).toBe(0);
5653
expect(testComponent.openStartCount).toBe(0);
54+
expect(container.classList).not.toContain('mat-drawer-opened');
5755

5856
tick();
5957
expect(testComponent.openStartCount).toBe(1);
6058
fixture.detectChanges();
6159

6260
expect(testComponent.openCount).toBe(1);
6361
expect(testComponent.openStartCount).toBe(1);
64-
expect(getComputedStyle(drawer.nativeElement).visibility).toBe('visible');
65-
expect(getComputedStyle(drawerBackdropElement.nativeElement).visibility).toBe('visible');
62+
expect(container.classList).toContain('mat-drawer-opened');
6663
}));
6764

6865
it('should be able to close', fakeAsync(() => {
6966
const fixture = TestBed.createComponent(BasicTestApp);
70-
7167
fixture.detectChanges();
7268

7369
const testComponent: BasicTestApp = fixture.debugElement.componentInstance;
74-
const drawer = fixture.debugElement.query(By.directive(MatDrawer));
75-
const drawerBackdropElement = fixture.debugElement.query(By.css('.mat-drawer-backdrop'));
70+
const container = fixture.debugElement.query(By.css('mat-drawer-container')).nativeElement;
7671

77-
drawerBackdropElement.nativeElement.style.transition = 'none';
7872
fixture.debugElement.query(By.css('.open')).nativeElement.click();
7973
fixture.detectChanges();
8074
flush();
@@ -85,15 +79,15 @@ describe('MatDrawer', () => {
8579

8680
expect(testComponent.closeCount).toBe(0);
8781
expect(testComponent.closeStartCount).toBe(0);
82+
expect(container.classList).toContain('mat-drawer-opened');
8883

8984
flush();
9085
expect(testComponent.closeStartCount).toBe(1);
9186
fixture.detectChanges();
9287

9388
expect(testComponent.closeCount).toBe(1);
9489
expect(testComponent.closeStartCount).toBe(1);
95-
expect(getComputedStyle(drawer.nativeElement).visibility).toBe('hidden');
96-
expect(getComputedStyle(drawerBackdropElement.nativeElement).visibility).toBe('hidden');
90+
expect(container.classList).not.toContain('mat-drawer-opened');
9791
}));
9892

9993
it('should resolve the open method promise with the new state of the drawer', fakeAsync(() => {
@@ -642,7 +636,7 @@ describe('MatDrawerContainer', () => {
642636
expect(parseInt(contentElement.style.marginRight)).toBe(margin);
643637
}));
644638

645-
it('should not animate when the sidenav is open on load ', fakeAsync(() => {
639+
it('should not animate when the sidenav is open on load', fakeAsync(() => {
646640
TestBed
647641
.resetTestingModule()
648642
.configureTestingModule({
@@ -661,29 +655,32 @@ describe('MatDrawerContainer', () => {
661655
expect(container.classList).not.toContain('mat-drawer-transition');
662656
}));
663657

664-
it('should recalculate the margin if a drawer changes size while open in autosize mode',
665-
fakeAsync(() => {
666-
const fixture = TestBed.createComponent(AutosizeDrawer);
658+
it('should recalculate the margin if drawer changes size while open in autosize mode', done => {
659+
const fixture = TestBed.createComponent(AutosizeDrawer);
667660

668-
fixture.detectChanges();
669-
fixture.componentInstance.drawer.open();
670-
fixture.detectChanges();
671-
tick();
672-
fixture.detectChanges();
661+
fixture.detectChanges();
662+
fixture.componentInstance.drawer.open();
663+
fixture.detectChanges();
673664

665+
fixture.whenStable().then(() => {
666+
fixture.detectChanges();
674667
const contentEl = fixture.debugElement.nativeElement.querySelector('.mat-drawer-content');
675668
const initialMargin = parseInt(contentEl.style.marginLeft);
676669

677670
expect(initialMargin).toBeGreaterThan(0);
678671

679672
fixture.componentInstance.fillerWidth = 200;
680673
fixture.detectChanges();
681-
tick(10);
682-
fixture.detectChanges();
683674

684-
expect(parseInt(contentEl.style.marginLeft)).toBeGreaterThan(initialMargin);
685-
discardPeriodicTasks();
686-
}));
675+
// Note that these 15ms are very slow for a unit test, but that how much the autosize
676+
// is debounced and we need to give the browser some time to recalculate the styles.
677+
setTimeout(() => {
678+
fixture.detectChanges();
679+
expect(parseInt(contentEl.style.marginLeft)).toBeGreaterThan(initialMargin);
680+
done();
681+
}, 15);
682+
});
683+
});
687684

688685
it('should not set a style property if it would be zero', fakeAsync(() => {
689686
const fixture = TestBed.createComponent(AutosizeDrawer);
@@ -945,10 +942,10 @@ class DrawerContainerStateChangesTestApp {
945942

946943
@Component({
947944
template: `
948-
<mat-drawer-container autosize>
945+
<mat-drawer-container autosize style="min-height: 200px;">
949946
<mat-drawer mode="push" [position]="drawer1Position">
950947
Text
951-
<div [style.width.px]="fillerWidth"></div>
948+
<div [style.width.px]="fillerWidth" style="height: 200px; background: red;"></div>
952949
</mat-drawer>
953950
</mat-drawer-container>`,
954951
})

0 commit comments

Comments
 (0)