Skip to content

Commit d559a1b

Browse files
committed
test(material/table): fix tests
1 parent af57fc2 commit d559a1b

File tree

4 files changed

+73
-66
lines changed

4 files changed

+73
-66
lines changed

src/cdk-experimental/table-scroll-container/table-scroll-container.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {CollectionViewer, DataSource} from '@angular/cdk/collections';
22
import {Component, Type, ViewChild} from '@angular/core';
3-
import {ComponentFixture, fakeAsync, flushMicrotasks, TestBed} from '@angular/core/testing';
3+
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
44
import {BehaviorSubject, combineLatest} from 'rxjs';
55
import {map} from 'rxjs/operators';
66
import {CdkTable, CdkTableModule} from '@angular/cdk/table';
@@ -48,10 +48,10 @@ describe('CdkTableScrollContainer', () => {
4848
dataRows = getRows(tableElement);
4949
});
5050

51-
it('sets scrollbar track margin for sticky headers', fakeAsync(() => {
51+
it('sets scrollbar track margin for sticky headers', waitForAsync(async () => {
5252
component.stickyHeaders = ['header-1', 'header-3'];
5353
fixture.detectChanges();
54-
flushMicrotasks();
54+
await new Promise(r => setTimeout(r));
5555

5656
if (platform.FIREFOX) {
5757
// ::-webkit-scrollbar-track is not recognized by Firefox.
@@ -66,18 +66,18 @@ describe('CdkTableScrollContainer', () => {
6666

6767
component.stickyHeaders = [];
6868
fixture.detectChanges();
69-
flushMicrotasks();
69+
await new Promise(r => setTimeout(r));
7070

7171
expect(scrollerStyle.getPropertyValue('margin-top')).toBe('0px');
7272
expect(scrollerStyle.getPropertyValue('margin-right')).toBe('0px');
7373
expect(scrollerStyle.getPropertyValue('margin-bottom')).toBe('0px');
7474
expect(scrollerStyle.getPropertyValue('margin-left')).toBe('0px');
7575
}));
7676

77-
it('sets scrollbar track margin for sticky footers', fakeAsync(() => {
77+
it('sets scrollbar track margin for sticky footers', waitForAsync(async () => {
7878
component.stickyFooters = ['footer-1', 'footer-3'];
7979
fixture.detectChanges();
80-
flushMicrotasks();
80+
await new Promise(r => setTimeout(r));
8181

8282
if (platform.FIREFOX) {
8383
// ::-webkit-scrollbar-track is not recognized by Firefox.
@@ -92,18 +92,18 @@ describe('CdkTableScrollContainer', () => {
9292

9393
component.stickyFooters = [];
9494
fixture.detectChanges();
95-
flushMicrotasks();
95+
await new Promise(r => setTimeout(r));
9696

9797
expect(scrollerStyle.getPropertyValue('margin-top')).toBe('0px');
9898
expect(scrollerStyle.getPropertyValue('margin-right')).toBe('0px');
9999
expect(scrollerStyle.getPropertyValue('margin-bottom')).toBe('0px');
100100
expect(scrollerStyle.getPropertyValue('margin-left')).toBe('0px');
101101
}));
102102

103-
it('sets scrollbar track margin for sticky start columns', fakeAsync(() => {
103+
it('sets scrollbar track margin for sticky start columns', waitForAsync(async () => {
104104
component.stickyStartColumns = ['column-1', 'column-3'];
105105
fixture.detectChanges();
106-
flushMicrotasks();
106+
await new Promise(r => setTimeout(r));
107107

108108
if (platform.FIREFOX) {
109109
// ::-webkit-scrollbar-track is not recognized by Firefox.
@@ -120,18 +120,18 @@ describe('CdkTableScrollContainer', () => {
120120

121121
component.stickyStartColumns = [];
122122
fixture.detectChanges();
123-
flushMicrotasks();
123+
await new Promise(r => setTimeout(r));
124124

125125
expect(scrollerStyle.getPropertyValue('margin-top')).toBe('0px');
126126
expect(scrollerStyle.getPropertyValue('margin-right')).toBe('0px');
127127
expect(scrollerStyle.getPropertyValue('margin-bottom')).toBe('0px');
128128
expect(scrollerStyle.getPropertyValue('margin-left')).toBe('0px');
129129
}));
130130

131-
it('sets scrollbar track margin for sticky end columns', fakeAsync(() => {
131+
it('sets scrollbar track margin for sticky end columns', waitForAsync(async () => {
132132
component.stickyEndColumns = ['column-4', 'column-6'];
133133
fixture.detectChanges();
134-
flushMicrotasks();
134+
await new Promise(r => setTimeout(r));
135135

136136
if (platform.FIREFOX) {
137137
// ::-webkit-scrollbar-track is not recognized by Firefox.
@@ -148,21 +148,21 @@ describe('CdkTableScrollContainer', () => {
148148

149149
component.stickyEndColumns = [];
150150
fixture.detectChanges();
151-
flushMicrotasks();
151+
await new Promise(r => setTimeout(r));
152152

153153
expect(scrollerStyle.getPropertyValue('margin-top')).toBe('0px');
154154
expect(scrollerStyle.getPropertyValue('margin-right')).toBe('0px');
155155
expect(scrollerStyle.getPropertyValue('margin-bottom')).toBe('0px');
156156
expect(scrollerStyle.getPropertyValue('margin-left')).toBe('0px');
157157
}));
158158

159-
it('sets scrollbar track margin for a combination of sticky rows and columns', fakeAsync(() => {
159+
it('sets scrollbar track margin for a combination of sticky rows and columns', waitForAsync(async () => {
160160
component.stickyHeaders = ['header-1'];
161161
component.stickyFooters = ['footer-3'];
162162
component.stickyStartColumns = ['column-1'];
163163
component.stickyEndColumns = ['column-6'];
164164
fixture.detectChanges();
165-
flushMicrotasks();
165+
await new Promise(r => setTimeout(r));
166166

167167
if (platform.FIREFOX) {
168168
// ::-webkit-scrollbar-track is not recognized by Firefox.
@@ -184,7 +184,7 @@ describe('CdkTableScrollContainer', () => {
184184
component.stickyStartColumns = [];
185185
component.stickyEndColumns = [];
186186
fixture.detectChanges();
187-
flushMicrotasks();
187+
await new Promise(r => setTimeout(r));
188188

189189
expect(scrollerStyle.getPropertyValue('margin-top')).toBe('0px');
190190
expect(scrollerStyle.getPropertyValue('margin-right')).toBe('0px');

src/cdk/table/coalesced-style-scheduler.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
inject,
1616
Injector,
1717
} from '@angular/core';
18-
import {from, Subject} from 'rxjs';
19-
import {take, takeUntil} from 'rxjs/operators';
18+
import {Subject} from 'rxjs';
2019

2120
/**
2221
* @docs-private
@@ -42,7 +41,7 @@ export const _COALESCED_STYLE_SCHEDULER = new InjectionToken<_CoalescedStyleSche
4241
export class _CoalescedStyleScheduler implements OnDestroy {
4342
private _currentSchedule: _Schedule | null = null;
4443
private readonly _destroyed = new Subject<void>();
45-
44+
private _isDestroyed = false;
4645
private _injector = inject(Injector);
4746

4847
constructor(private readonly _ngZone: NgZone) {}
@@ -70,6 +69,7 @@ export class _CoalescedStyleScheduler implements OnDestroy {
7069
ngOnDestroy() {
7170
this._destroyed.next();
7271
this._destroyed.complete();
72+
this._isDestroyed = true;
7373
}
7474

7575
private _createScheduleIfNeeded() {
@@ -80,7 +80,11 @@ export class _CoalescedStyleScheduler implements OnDestroy {
8080
this._currentSchedule = new _Schedule();
8181

8282
this._ngZone.run(() =>
83-
queueMicrotask(() =>
83+
queueMicrotask(() => {
84+
if (this._isDestroyed) {
85+
return;
86+
}
87+
8488
afterNextRender(
8589
() => {
8690
while (this._currentSchedule!.tasks.length || this._currentSchedule!.endTasks.length) {
@@ -101,8 +105,8 @@ export class _CoalescedStyleScheduler implements OnDestroy {
101105
this._currentSchedule = null;
102106
},
103107
{injector: this._injector},
104-
),
105-
),
108+
);
109+
}),
106110
);
107111
}
108112
}

0 commit comments

Comments
 (0)