Skip to content

Commit 043b288

Browse files
committed
test(cdk-experimental/scrolling): switch tests away from fakeAsync (#33685)
Reworks the tests in `cdk-experimental` not to depend on `fakeAsync`. (cherry picked from commit 4fcb434)
1 parent fd561f2 commit 043b288

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

src/cdk-experimental/scrolling/virtual-scroll-viewport.spec.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {CdkVirtualScrollViewport, ScrollingModule} from '@angular/cdk/scrolling';
22
import {Component, ViewChild, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
3-
import {ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
3+
import {ComponentFixture, TestBed} from '@angular/core/testing';
44
import {ScrollingModule as ExperimentalScrollingModule} from './scrolling-module';
55

66
describe('CdkVirtualScrollViewport', () => {
@@ -15,20 +15,20 @@ describe('CdkVirtualScrollViewport', () => {
1515
viewport = testComponent.viewport;
1616
});
1717

18-
it('should render initial state for uniform items', fakeAsync(() => {
19-
finishInit(fixture);
18+
it('should render initial state for uniform items', async () => {
19+
await finishInit(fixture);
2020

2121
const contentWrapper = viewport.elementRef.nativeElement.querySelector(
2222
'.cdk-virtual-scroll-content-wrapper',
2323
)!;
2424
expect(contentWrapper.children.length)
2525
.withContext('should render 4 50px items to fill 200px space')
2626
.toBe(4);
27-
}));
27+
});
2828

29-
it('should render extra content if first item is smaller than average', fakeAsync(() => {
29+
it('should render extra content if first item is smaller than average', async () => {
3030
testComponent.items = [50, 200, 200, 200, 200, 200];
31-
finishInit(fixture);
31+
await finishInit(fixture);
3232

3333
const contentWrapper = viewport.elementRef.nativeElement.querySelector(
3434
'.cdk-virtual-scroll-content-wrapper',
@@ -38,32 +38,35 @@ describe('CdkVirtualScrollViewport', () => {
3838
'should render 4 items to fill 200px space based on 50px ' + 'estimate from first item',
3939
)
4040
.toBe(4);
41-
}));
42-
43-
it('should throw if maxBufferPx is less than minBufferPx', fakeAsync(() => {
44-
expect(() => {
45-
testComponent.minBufferPx = 100;
46-
testComponent.maxBufferPx = 99;
47-
finishInit(fixture);
48-
}).toThrowError(
41+
});
42+
43+
it('should throw if maxBufferPx is less than minBufferPx', async () => {
44+
testComponent.minBufferPx = 100;
45+
testComponent.maxBufferPx = 99;
46+
await expectAsync(finishInit(fixture)).toBeRejectedWithError(
4947
'CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx',
5048
);
51-
}));
49+
});
5250

5351
// TODO(mmalerba): Add test that it corrects the initial render if it didn't render enough,
5452
// once it actually does that.
5553
});
5654
});
5755

5856
/** Finish initializing the virtual scroll component at the beginning of a test. */
59-
function finishInit(fixture: ComponentFixture<any>) {
57+
async function finishInit(fixture: ComponentFixture<any>) {
6058
// On the first cycle we render and measure the viewport.
6159
fixture.detectChanges();
62-
flush();
60+
await fixture.whenStable();
6361

6462
// On the second cycle we render the items.
6563
fixture.detectChanges();
66-
flush();
64+
await fixture.whenStable();
65+
66+
// Flush the initial fake scroll event.
67+
await new Promise(resolve => requestAnimationFrame(resolve));
68+
await fixture.whenStable();
69+
fixture.detectChanges();
6770
}
6871

6972
@Component({

0 commit comments

Comments
 (0)