Skip to content

Commit efc3361

Browse files
crisbetommalerba
authored andcommitted
refactor(autocomplete): switch to fakeAsync tests (#8781)
* Switches (almost) all of the autocomplete tests to run in the `fakeAsync` zone. * Adds a `MockNgZone` provider that allows us to flush out the `NgZone.onStable` subscriptions in `fakeAsync` tests. * Fixes the "should close the panel when the user clicks away" test not doing anything due to being wrapped in a `whenStable` callback but not running in an async zone. * Avoids recompiling all of the test components inside a `beforeEach`. The above-mentioned changes cut down the autocomplete testing time from ~30sec to ~10sec.
1 parent 3278931 commit efc3361

File tree

3 files changed

+364
-367
lines changed

3 files changed

+364
-367
lines changed

src/cdk/testing/mock-ng-zone.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {EventEmitter, Injectable, NgZone} from '@angular/core';
10+
11+
12+
/**
13+
* Mock synchronous NgZone implementation that can be used
14+
* to flush out `onStable` subscriptions in tests.
15+
*
16+
* via: https://github.com/angular/angular/blob/master/packages/core/testing/src/ng_zone_mock.ts
17+
* @docs-private
18+
*/
19+
@Injectable()
20+
export class MockNgZone extends NgZone {
21+
onStable: EventEmitter<any> = new EventEmitter(false);
22+
23+
constructor() {
24+
super({enableLongStackTrace: false});
25+
}
26+
27+
run(fn: Function): any {
28+
return fn();
29+
}
30+
31+
runOutsideAngular(fn: Function): any {
32+
return fn();
33+
}
34+
35+
simulateZoneExit(): void {
36+
this.onStable.emit(null);
37+
}
38+
}

src/cdk/testing/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export * from './event-objects';
1111
export * from './type-in-element';
1212
export * from './wrapped-error-message';
1313
export * from './fake-viewport-ruler';
14+
export * from './mock-ng-zone';

0 commit comments

Comments
 (0)