UNPKG

6.59 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. 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 */
8import { getDebugNode } from '@angular/core';
9import { scheduleMicroTask } from './facade/lang';
10/**
11 * Fixture for debugging and testing a component.
12 *
13 * @stable
14 */
15export var ComponentFixture = (function () {
16 function ComponentFixture(componentRef, ngZone, _autoDetect) {
17 var _this = this;
18 this.componentRef = componentRef;
19 this.ngZone = ngZone;
20 this._autoDetect = _autoDetect;
21 this._isStable = true;
22 this._isDestroyed = false;
23 this._promise = null;
24 this._onUnstableSubscription = null;
25 this._onStableSubscription = null;
26 this._onMicrotaskEmptySubscription = null;
27 this._onErrorSubscription = null;
28 this.changeDetectorRef = componentRef.changeDetectorRef;
29 this.elementRef = componentRef.location;
30 this.debugElement = getDebugNode(this.elementRef.nativeElement);
31 this.componentInstance = componentRef.instance;
32 this.nativeElement = this.elementRef.nativeElement;
33 this.componentRef = componentRef;
34 this.ngZone = ngZone;
35 if (ngZone != null) {
36 this._onUnstableSubscription =
37 ngZone.onUnstable.subscribe({ next: function () { _this._isStable = false; } });
38 this._onMicrotaskEmptySubscription = ngZone.onMicrotaskEmpty.subscribe({
39 next: function () {
40 if (_this._autoDetect) {
41 // Do a change detection run with checkNoChanges set to true to check
42 // there are no changes on the second run.
43 _this.detectChanges(true);
44 }
45 }
46 });
47 this._onStableSubscription = ngZone.onStable.subscribe({
48 next: function () {
49 _this._isStable = true;
50 // Check whether there is a pending whenStable() completer to resolve.
51 if (_this._promise !== null) {
52 // If so check whether there are no pending macrotasks before resolving.
53 // Do this check in the next tick so that ngZone gets a chance to update the state of
54 // pending macrotasks.
55 scheduleMicroTask(function () {
56 if (!_this.ngZone.hasPendingMacrotasks) {
57 if (_this._promise !== null) {
58 _this._resolve(true);
59 _this._resolve = null;
60 _this._promise = null;
61 }
62 }
63 });
64 }
65 }
66 });
67 this._onErrorSubscription =
68 ngZone.onError.subscribe({ next: function (error) { throw error; } });
69 }
70 }
71 ComponentFixture.prototype._tick = function (checkNoChanges) {
72 this.changeDetectorRef.detectChanges();
73 if (checkNoChanges) {
74 this.checkNoChanges();
75 }
76 };
77 /**
78 * Trigger a change detection cycle for the component.
79 */
80 ComponentFixture.prototype.detectChanges = function (checkNoChanges) {
81 var _this = this;
82 if (checkNoChanges === void 0) { checkNoChanges = true; }
83 if (this.ngZone != null) {
84 // Run the change detection inside the NgZone so that any async tasks as part of the change
85 // detection are captured by the zone and can be waited for in isStable.
86 this.ngZone.run(function () { _this._tick(checkNoChanges); });
87 }
88 else {
89 // Running without zone. Just do the change detection.
90 this._tick(checkNoChanges);
91 }
92 };
93 /**
94 * Do a change detection run to make sure there were no changes.
95 */
96 ComponentFixture.prototype.checkNoChanges = function () { this.changeDetectorRef.checkNoChanges(); };
97 /**
98 * Set whether the fixture should autodetect changes.
99 *
100 * Also runs detectChanges once so that any existing change is detected.
101 */
102 ComponentFixture.prototype.autoDetectChanges = function (autoDetect) {
103 if (autoDetect === void 0) { autoDetect = true; }
104 if (this.ngZone == null) {
105 throw new Error('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set');
106 }
107 this._autoDetect = autoDetect;
108 this.detectChanges();
109 };
110 /**
111 * Return whether the fixture is currently stable or has async tasks that have not been completed
112 * yet.
113 */
114 ComponentFixture.prototype.isStable = function () { return this._isStable && !this.ngZone.hasPendingMacrotasks; };
115 /**
116 * Get a promise that resolves when the fixture is stable.
117 *
118 * This can be used to resume testing after events have triggered asynchronous activity or
119 * asynchronous change detection.
120 */
121 ComponentFixture.prototype.whenStable = function () {
122 var _this = this;
123 if (this.isStable()) {
124 return Promise.resolve(false);
125 }
126 else if (this._promise !== null) {
127 return this._promise;
128 }
129 else {
130 this._promise = new Promise(function (res) { _this._resolve = res; });
131 return this._promise;
132 }
133 };
134 /**
135 * Trigger component destruction.
136 */
137 ComponentFixture.prototype.destroy = function () {
138 if (!this._isDestroyed) {
139 this.componentRef.destroy();
140 if (this._onUnstableSubscription != null) {
141 this._onUnstableSubscription.unsubscribe();
142 this._onUnstableSubscription = null;
143 }
144 if (this._onStableSubscription != null) {
145 this._onStableSubscription.unsubscribe();
146 this._onStableSubscription = null;
147 }
148 if (this._onMicrotaskEmptySubscription != null) {
149 this._onMicrotaskEmptySubscription.unsubscribe();
150 this._onMicrotaskEmptySubscription = null;
151 }
152 if (this._onErrorSubscription != null) {
153 this._onErrorSubscription.unsubscribe();
154 this._onErrorSubscription = null;
155 }
156 this._isDestroyed = true;
157 }
158 };
159 return ComponentFixture;
160}());
161//# sourceMappingURL=component_fixture.js.map
\No newline at end of file