From e34eb4520f5db9e551f3520a2df3c6a9c7c571b9 Mon Sep 17 00:00:00 2001 From: vikerman Date: Fri, 22 Jul 2016 16:07:11 -0700 Subject: [PATCH] fix(testing): ComponentFixture - Avoid extra scheduleMicrotask (#10223) Don't schedule microtask to check for pending macrotasks when no one is waiting for a whenStable(). --- .../core/testing/component_fixture.ts | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/modules/@angular/core/testing/component_fixture.ts b/modules/@angular/core/testing/component_fixture.ts index cb7910e38e..4cb90592f8 100644 --- a/modules/@angular/core/testing/component_fixture.ts +++ b/modules/@angular/core/testing/component_fixture.ts @@ -87,16 +87,20 @@ export class ComponentFixture { }); this._onStableSubscription = ObservableWrapper.subscribe(ngZone.onStable, (_) => { this._isStable = true; - // Check whether there are no pending macrotasks in a microtask so that ngZone gets a chance - // to update the state of pending macrotasks. - scheduleMicroTask(() => { - if (!this.ngZone.hasPendingMacrotasks) { - if (this._completer != null) { - this._completer.resolve(true); - this._completer = null; + // Check whether there is a pending whenStable() completer to resolve. + if (this._completer !== null) { + // If so check whether there are no pending macrotasks before resolving. + // Do this check in the next tick so that ngZone gets a chance to update the state of + // pending macrotasks. + scheduleMicroTask(() => { + if (!this.ngZone.hasPendingMacrotasks) { + if (this._completer !== null) { + this._completer.resolve(true); + this._completer = null; + } } - } - }); + }); + } }); this._onErrorSubscription = ObservableWrapper.subscribe(