1
|
import * as tslib_1 from "tslib";
|
2
|
/**
|
3
|
* @license Angular v4.4.6
|
4
|
* (c) 2010-2017 Google, Inc. https://angular.io/
|
5
|
* License: MIT
|
6
|
*/
|
7
|
import { ApplicationInitStatus, Compiler, InjectionToken, Injector, NgModule, NgZone, Optional, ReflectiveInjector, RendererFactory2, SkipSelf, getDebugNode, ɵERROR_COMPONENT_TYPE, ɵclearProviderOverrides, ɵoverrideProvider, ɵstringify } from '@angular/core';
|
8
|
/**
|
9
|
* @license
|
10
|
* Copyright Google Inc. All Rights Reserved.
|
11
|
*
|
12
|
* Use of this source code is governed by an MIT-style license that can be
|
13
|
* found in the LICENSE file at https://angular.io/license
|
14
|
*/
|
15
|
var _global = (typeof window === 'undefined' ? global : window);
|
16
|
/**
|
17
|
* Wraps a test function in an asynchronous test zone. The test will automatically
|
18
|
* complete when all asynchronous calls within this zone are done. Can be used
|
19
|
* to wrap an {@link inject} call.
|
20
|
*
|
21
|
* Example:
|
22
|
*
|
23
|
* ```
|
24
|
* it('...', async(inject([AClass], (object) => {
|
25
|
* object.doSomething.then(() => {
|
26
|
* expect(...);
|
27
|
* })
|
28
|
* });
|
29
|
* ```
|
30
|
*
|
31
|
* @stable
|
32
|
*/
|
33
|
function async(fn) {
|
34
|
// If we're running using the Jasmine test framework, adapt to call the 'done'
|
35
|
// function when asynchronous activity is finished.
|
36
|
if (_global.jasmine) {
|
37
|
// Not using an arrow function to preserve context passed from call site
|
38
|
return function (done) {
|
39
|
if (!done) {
|
40
|
// if we run beforeEach in @angular/core/testing/testing_internal then we get no done
|
41
|
// fake it here and assume sync.
|
42
|
done = function () { };
|
43
|
done.fail = function (e) { throw e; };
|
44
|
}
|
45
|
runInTestZone(fn, this, done, function (err) {
|
46
|
if (typeof err === 'string') {
|
47
|
return done.fail(new Error(err));
|
48
|
}
|
49
|
else {
|
50
|
done.fail(err);
|
51
|
}
|
52
|
});
|
53
|
};
|
54
|
}
|
55
|
// Otherwise, return a promise which will resolve when asynchronous activity
|
56
|
// is finished. This will be correctly consumed by the Mocha framework with
|
57
|
// it('...', async(myFn)); or can be used in a custom framework.
|
58
|
// Not using an arrow function to preserve context passed from call site
|
59
|
return function () {
|
60
|
var _this = this;
|
61
|
return new Promise(function (finishCallback, failCallback) {
|
62
|
runInTestZone(fn, _this, finishCallback, failCallback);
|
63
|
});
|
64
|
};
|
65
|
}
|
66
|
function runInTestZone(fn, context, finishCallback, failCallback) {
|
67
|
var currentZone = Zone.current;
|
68
|
var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
|
69
|
if (AsyncTestZoneSpec === undefined) {
|
70
|
throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
|
71
|
'Please make sure that your environment includes zone.js/dist/async-test.js');
|
72
|
}
|
73
|
var ProxyZoneSpec = Zone['ProxyZoneSpec'];
|
74
|
if (ProxyZoneSpec === undefined) {
|
75
|
throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
|
76
|
'Please make sure that your environment includes zone.js/dist/proxy.js');
|
77
|
}
|
78
|
var proxyZoneSpec = ProxyZoneSpec.get();
|
79
|
ProxyZoneSpec.assertPresent();
|
80
|
// We need to create the AsyncTestZoneSpec outside the ProxyZone.
|
81
|
// If we do it in ProxyZone then we will get to infinite recursion.
|
82
|
var proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
|
83
|
var previousDelegate = proxyZoneSpec.getDelegate();
|
84
|
proxyZone.parent.run(function () {
|
85
|
var testZoneSpec = new AsyncTestZoneSpec(function () {
|
86
|
// Need to restore the original zone.
|
87
|
currentZone.run(function () {
|
88
|
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
89
|
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
90
|
proxyZoneSpec.setDelegate(previousDelegate);
|
91
|
}
|
92
|
finishCallback();
|
93
|
});
|
94
|
}, function (error) {
|
95
|
// Need to restore the original zone.
|
96
|
currentZone.run(function () {
|
97
|
if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
98
|
// Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
99
|
proxyZoneSpec.setDelegate(previousDelegate);
|
100
|
}
|
101
|
failCallback(error);
|
102
|
});
|
103
|
}, 'test');
|
104
|
proxyZoneSpec.setDelegate(testZoneSpec);
|
105
|
});
|
106
|
return Zone.current.runGuarded(fn, context);
|
107
|
}
|
108
|
/**
|
109
|
* @license
|
110
|
* Copyright Google Inc. All Rights Reserved.
|
111
|
*
|
112
|
* Use of this source code is governed by an MIT-style license that can be
|
113
|
* found in the LICENSE file at https://angular.io/license
|
114
|
*/
|
115
|
/**
|
116
|
* Fixture for debugging and testing a component.
|
117
|
*
|
118
|
* @stable
|
119
|
*/
|
120
|
var ComponentFixture = (function () {
|
121
|
function ComponentFixture(componentRef, ngZone, _autoDetect) {
|
122
|
var _this = this;
|
123
|
this.componentRef = componentRef;
|
124
|
this.ngZone = ngZone;
|
125
|
this._autoDetect = _autoDetect;
|
126
|
this._isStable = true;
|
127
|
this._isDestroyed = false;
|
128
|
this._resolve = null;
|
129
|
this._promise = null;
|
130
|
this._onUnstableSubscription = null;
|
131
|
this._onStableSubscription = null;
|
132
|
this._onMicrotaskEmptySubscription = null;
|
133
|
this._onErrorSubscription = null;
|
134
|
this.changeDetectorRef = componentRef.changeDetectorRef;
|
135
|
this.elementRef = componentRef.location;
|
136
|
this.debugElement = getDebugNode(this.elementRef.nativeElement);
|
137
|
this.componentInstance = componentRef.instance;
|
138
|
this.nativeElement = this.elementRef.nativeElement;
|
139
|
this.componentRef = componentRef;
|
140
|
this.ngZone = ngZone;
|
141
|
if (ngZone) {
|
142
|
// Create subscriptions outside the NgZone so that the callbacks run oustide
|
143
|
// of NgZone.
|
144
|
ngZone.runOutsideAngular(function () {
|
145
|
_this._onUnstableSubscription =
|
146
|
ngZone.onUnstable.subscribe({ next: function () { _this._isStable = false; } });
|
147
|
_this._onMicrotaskEmptySubscription = ngZone.onMicrotaskEmpty.subscribe({
|
148
|
next: function () {
|
149
|
if (_this._autoDetect) {
|
150
|
// Do a change detection run with checkNoChanges set to true to check
|
151
|
// there are no changes on the second run.
|
152
|
_this.detectChanges(true);
|
153
|
}
|
154
|
}
|
155
|
});
|
156
|
_this._onStableSubscription = ngZone.onStable.subscribe({
|
157
|
next: function () {
|
158
|
_this._isStable = true;
|
159
|
// Check whether there is a pending whenStable() completer to resolve.
|
160
|
if (_this._promise !== null) {
|
161
|
// If so check whether there are no pending macrotasks before resolving.
|
162
|
// Do this check in the next tick so that ngZone gets a chance to update the state of
|
163
|
// pending macrotasks.
|
164
|
scheduleMicroTask(function () {
|
165
|
if (!ngZone.hasPendingMacrotasks) {
|
166
|
if (_this._promise !== null) {
|
167
|
_this._resolve(true);
|
168
|
_this._resolve = null;
|
169
|
_this._promise = null;
|
170
|
}
|
171
|
}
|
172
|
});
|
173
|
}
|
174
|
}
|
175
|
});
|
176
|
_this._onErrorSubscription =
|
177
|
ngZone.onError.subscribe({ next: function (error) { throw error; } });
|
178
|
});
|
179
|
}
|
180
|
}
|
181
|
ComponentFixture.prototype._tick = function (checkNoChanges) {
|
182
|
this.changeDetectorRef.detectChanges();
|
183
|
if (checkNoChanges) {
|
184
|
this.checkNoChanges();
|
185
|
}
|
186
|
};
|
187
|
/**
|
188
|
* Trigger a change detection cycle for the component.
|
189
|
*/
|
190
|
ComponentFixture.prototype.detectChanges = function (checkNoChanges) {
|
191
|
var _this = this;
|
192
|
if (checkNoChanges === void 0) { checkNoChanges = true; }
|
193
|
if (this.ngZone != null) {
|
194
|
// Run the change detection inside the NgZone so that any async tasks as part of the change
|
195
|
// detection are captured by the zone and can be waited for in isStable.
|
196
|
this.ngZone.run(function () { _this._tick(checkNoChanges); });
|
197
|
}
|
198
|
else {
|
199
|
// Running without zone. Just do the change detection.
|
200
|
this._tick(checkNoChanges);
|
201
|
}
|
202
|
};
|
203
|
/**
|
204
|
* Do a change detection run to make sure there were no changes.
|
205
|
*/
|
206
|
ComponentFixture.prototype.checkNoChanges = function () { this.changeDetectorRef.checkNoChanges(); };
|
207
|
/**
|
208
|
* Set whether the fixture should autodetect changes.
|
209
|
*
|
210
|
* Also runs detectChanges once so that any existing change is detected.
|
211
|
*/
|
212
|
ComponentFixture.prototype.autoDetectChanges = function (autoDetect) {
|
213
|
if (autoDetect === void 0) { autoDetect = true; }
|
214
|
if (this.ngZone == null) {
|
215
|
throw new Error('Cannot call autoDetectChanges when ComponentFixtureNoNgZone is set');
|
216
|
}
|
217
|
this._autoDetect = autoDetect;
|
218
|
this.detectChanges();
|
219
|
};
|
220
|
/**
|
221
|
* Return whether the fixture is currently stable or has async tasks that have not been completed
|
222
|
* yet.
|
223
|
*/
|
224
|
ComponentFixture.prototype.isStable = function () { return this._isStable && !this.ngZone.hasPendingMacrotasks; };
|
225
|
/**
|
226
|
* Get a promise that resolves when the fixture is stable.
|
227
|
*
|
228
|
* This can be used to resume testing after events have triggered asynchronous activity or
|
229
|
* asynchronous change detection.
|
230
|
*/
|
231
|
ComponentFixture.prototype.whenStable = function () {
|
232
|
var _this = this;
|
233
|
if (this.isStable()) {
|
234
|
return Promise.resolve(false);
|
235
|
}
|
236
|
else if (this._promise !== null) {
|
237
|
return this._promise;
|
238
|
}
|
239
|
else {
|
240
|
this._promise = new Promise(function (res) { _this._resolve = res; });
|
241
|
return this._promise;
|
242
|
}
|
243
|
};
|
244
|
ComponentFixture.prototype._getRenderer = function () {
|
245
|
if (this._renderer === undefined) {
|
246
|
this._renderer = this.componentRef.injector.get(RendererFactory2, null);
|
247
|
}
|
248
|
return this._renderer;
|
249
|
};
|
250
|
/**
|
251
|
* Get a promise that resolves when the ui state is stable following animations.
|
252
|
*/
|
253
|
ComponentFixture.prototype.whenRenderingDone = function () {
|
254
|
var renderer = this._getRenderer();
|
255
|
if (renderer && renderer.whenRenderingDone) {
|
256
|
return renderer.whenRenderingDone();
|
257
|
}
|
258
|
return this.whenStable();
|
259
|
};
|
260
|
/**
|
261
|
* Trigger component destruction.
|
262
|
*/
|
263
|
ComponentFixture.prototype.destroy = function () {
|
264
|
if (!this._isDestroyed) {
|
265
|
this.componentRef.destroy();
|
266
|
if (this._onUnstableSubscription != null) {
|
267
|
this._onUnstableSubscription.unsubscribe();
|
268
|
this._onUnstableSubscription = null;
|
269
|
}
|
270
|
if (this._onStableSubscription != null) {
|
271
|
this._onStableSubscription.unsubscribe();
|
272
|
this._onStableSubscription = null;
|
273
|
}
|
274
|
if (this._onMicrotaskEmptySubscription != null) {
|
275
|
this._onMicrotaskEmptySubscription.unsubscribe();
|
276
|
this._onMicrotaskEmptySubscription = null;
|
277
|
}
|
278
|
if (this._onErrorSubscription != null) {
|
279
|
this._onErrorSubscription.unsubscribe();
|
280
|
this._onErrorSubscription = null;
|
281
|
}
|
282
|
this._isDestroyed = true;
|
283
|
}
|
284
|
};
|
285
|
return ComponentFixture;
|
286
|
}());
|
287
|
function scheduleMicroTask(fn) {
|
288
|
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
|
289
|
}
|
290
|
/**
|
291
|
* @license
|
292
|
* Copyright Google Inc. All Rights Reserved.
|
293
|
*
|
294
|
* Use of this source code is governed by an MIT-style license that can be
|
295
|
* found in the LICENSE file at https://angular.io/license
|
296
|
*/
|
297
|
var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec'];
|
298
|
var ProxyZoneSpec = Zone['ProxyZoneSpec'];
|
299
|
var _fakeAsyncTestZoneSpec = null;
|
300
|
/**
|
301
|
* Clears out the shared fake async zone for a test.
|
302
|
* To be called in a global `beforeEach`.
|
303
|
*
|
304
|
* @experimental
|
305
|
*/
|
306
|
function resetFakeAsyncZone() {
|
307
|
_fakeAsyncTestZoneSpec = null;
|
308
|
ProxyZoneSpec.assertPresent().resetDelegate();
|
309
|
}
|
310
|
var _inFakeAsyncCall = false;
|
311
|
/**
|
312
|
* Wraps a function to be executed in the fakeAsync zone:
|
313
|
* - microtasks are manually executed by calling `flushMicrotasks()`,
|
314
|
* - timers are synchronous, `tick()` simulates the asynchronous passage of time.
|
315
|
*
|
316
|
* If there are any pending timers at the end of the function, an exception will be thrown.
|
317
|
*
|
318
|
* Can be used to wrap inject() calls.
|
319
|
*
|
320
|
* ## Example
|
321
|
*
|
322
|
* {@example testing/ts/fake_async.ts region='basic'}
|
323
|
*
|
324
|
* @param fn
|
325
|
* @returns {Function} The function wrapped to be executed in the fakeAsync zone
|
326
|
*
|
327
|
* @experimental
|
328
|
*/
|
329
|
function fakeAsync(fn) {
|
330
|
// Not using an arrow function to preserve context passed from call site
|
331
|
return function () {
|
332
|
var args = [];
|
333
|
for (var _i = 0; _i < arguments.length; _i++) {
|
334
|
args[_i] = arguments[_i];
|
335
|
}
|
336
|
var proxyZoneSpec = ProxyZoneSpec.assertPresent();
|
337
|
if (_inFakeAsyncCall) {
|
338
|
throw new Error('fakeAsync() calls can not be nested');
|
339
|
}
|
340
|
_inFakeAsyncCall = true;
|
341
|
try {
|
342
|
if (!_fakeAsyncTestZoneSpec) {
|
343
|
if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
|
344
|
throw new Error('fakeAsync() calls can not be nested');
|
345
|
}
|
346
|
_fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
|
347
|
}
|
348
|
var res = void 0;
|
349
|
var lastProxyZoneSpec = proxyZoneSpec.getDelegate();
|
350
|
proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
|
351
|
try {
|
352
|
res = fn.apply(this, args);
|
353
|
flushMicrotasks();
|
354
|
}
|
355
|
finally {
|
356
|
proxyZoneSpec.setDelegate(lastProxyZoneSpec);
|
357
|
}
|
358
|
if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
|
359
|
throw new Error(_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length + " " +
|
360
|
"periodic timer(s) still in the queue.");
|
361
|
}
|
362
|
if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
|
363
|
throw new Error(_fakeAsyncTestZoneSpec.pendingTimers.length + " timer(s) still in the queue.");
|
364
|
}
|
365
|
return res;
|
366
|
}
|
367
|
finally {
|
368
|
_inFakeAsyncCall = false;
|
369
|
resetFakeAsyncZone();
|
370
|
}
|
371
|
};
|
372
|
}
|
373
|
function _getFakeAsyncZoneSpec() {
|
374
|
if (_fakeAsyncTestZoneSpec == null) {
|
375
|
throw new Error('The code should be running in the fakeAsync zone to call this function');
|
376
|
}
|
377
|
return _fakeAsyncTestZoneSpec;
|
378
|
}
|
379
|
/**
|
380
|
* Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
|
381
|
*
|
382
|
* The microtasks queue is drained at the very start of this function and after any timer callback
|
383
|
* has been executed.
|
384
|
*
|
385
|
* ## Example
|
386
|
*
|
387
|
* {@example testing/ts/fake_async.ts region='basic'}
|
388
|
*
|
389
|
* @experimental
|
390
|
*/
|
391
|
function tick(millis) {
|
392
|
if (millis === void 0) { millis = 0; }
|
393
|
_getFakeAsyncZoneSpec().tick(millis);
|
394
|
}
|
395
|
/**
|
396
|
* Simulates the asynchronous passage of time for the timers in the fakeAsync zone by
|
397
|
* draining the macrotask queue until it is empty. The returned value is the milliseconds
|
398
|
* of time that would have been elapsed.
|
399
|
*
|
400
|
* @param maxTurns
|
401
|
* @returns {number} The simulated time elapsed, in millis.
|
402
|
*
|
403
|
* @experimental
|
404
|
*/
|
405
|
function flush(maxTurns) {
|
406
|
return _getFakeAsyncZoneSpec().flush(maxTurns);
|
407
|
}
|
408
|
/**
|
409
|
* Discard all remaining periodic tasks.
|
410
|
*
|
411
|
* @experimental
|
412
|
*/
|
413
|
function discardPeriodicTasks() {
|
414
|
var zoneSpec = _getFakeAsyncZoneSpec();
|
415
|
var pendingTimers = zoneSpec.pendingPeriodicTimers;
|
416
|
zoneSpec.pendingPeriodicTimers.length = 0;
|
417
|
}
|
418
|
/**
|
419
|
* Flush any pending microtasks.
|
420
|
*
|
421
|
* @experimental
|
422
|
*/
|
423
|
function flushMicrotasks() {
|
424
|
_getFakeAsyncZoneSpec().flushMicrotasks();
|
425
|
}
|
426
|
/**
|
427
|
* @license
|
428
|
* Copyright Google Inc. All Rights Reserved.
|
429
|
*
|
430
|
* Use of this source code is governed by an MIT-style license that can be
|
431
|
* found in the LICENSE file at https://angular.io/license
|
432
|
*/
|
433
|
/**
|
434
|
* Injectable completer that allows signaling completion of an asynchronous test. Used internally.
|
435
|
*/
|
436
|
/**
|
437
|
* @license
|
438
|
* Copyright Google Inc. All Rights Reserved.
|
439
|
*
|
440
|
* Use of this source code is governed by an MIT-style license that can be
|
441
|
* found in the LICENSE file at https://angular.io/license
|
442
|
*/ var AsyncTestCompleter = (function () {
|
443
|
function AsyncTestCompleter() {
|
444
|
var _this = this;
|
445
|
this._promise = new Promise(function (res, rej) {
|
446
|
_this._resolve = res;
|
447
|
_this._reject = rej;
|
448
|
});
|
449
|
}
|
450
|
AsyncTestCompleter.prototype.done = function (value) { this._resolve(value); };
|
451
|
AsyncTestCompleter.prototype.fail = function (error, stackTrace) { this._reject(error); };
|
452
|
Object.defineProperty(AsyncTestCompleter.prototype, "promise", {
|
453
|
get: function () { return this._promise; },
|
454
|
enumerable: true,
|
455
|
configurable: true
|
456
|
});
|
457
|
return AsyncTestCompleter;
|
458
|
}());
|
459
|
/**
|
460
|
* @license
|
461
|
* Copyright Google Inc. All Rights Reserved.
|
462
|
*
|
463
|
* Use of this source code is governed by an MIT-style license that can be
|
464
|
* found in the LICENSE file at https://angular.io/license
|
465
|
*/
|
466
|
function unimplemented() {
|
467
|
throw Error('unimplemented');
|
468
|
}
|
469
|
/**
|
470
|
* Special interface to the compiler only used by testing
|
471
|
*
|
472
|
* @experimental
|
473
|
*/
|
474
|
var TestingCompiler = (function (_super) {
|
475
|
tslib_1.__extends(TestingCompiler, _super);
|
476
|
function TestingCompiler() {
|
477
|
return _super !== null && _super.apply(this, arguments) || this;
|
478
|
}
|
479
|
Object.defineProperty(TestingCompiler.prototype, "injector", {
|
480
|
get: function () { throw unimplemented(); },
|
481
|
enumerable: true,
|
482
|
configurable: true
|
483
|
});
|
484
|
TestingCompiler.prototype.overrideModule = function (module, overrides) {
|
485
|
throw unimplemented();
|
486
|
};
|
487
|
TestingCompiler.prototype.overrideDirective = function (directive, overrides) {
|
488
|
throw unimplemented();
|
489
|
};
|
490
|
TestingCompiler.prototype.overrideComponent = function (component, overrides) {
|
491
|
throw unimplemented();
|
492
|
};
|
493
|
TestingCompiler.prototype.overridePipe = function (directive, overrides) {
|
494
|
throw unimplemented();
|
495
|
};
|
496
|
/**
|
497
|
* Allows to pass the compile summary from AOT compilation to the JIT compiler,
|
498
|
* so that it can use the code generated by AOT.
|
499
|
*/
|
500
|
TestingCompiler.prototype.loadAotSummaries = function (summaries) { throw unimplemented(); };
|
501
|
/**
|
502
|
* Gets the component factory for the given component.
|
503
|
* This assumes that the component has been compiled before calling this call using
|
504
|
* `compileModuleAndAllComponents*`.
|
505
|
*/
|
506
|
TestingCompiler.prototype.getComponentFactory = function (component) { throw unimplemented(); };
|
507
|
return TestingCompiler;
|
508
|
}(Compiler));
|
509
|
/**
|
510
|
* A factory for creating a Compiler
|
511
|
*
|
512
|
* @experimental
|
513
|
*/
|
514
|
var TestingCompilerFactory = (function () {
|
515
|
function TestingCompilerFactory() {
|
516
|
}
|
517
|
return TestingCompilerFactory;
|
518
|
}());
|
519
|
/**
|
520
|
* @license
|
521
|
* Copyright Google Inc. All Rights Reserved.
|
522
|
*
|
523
|
* Use of this source code is governed by an MIT-style license that can be
|
524
|
* found in the LICENSE file at https://angular.io/license
|
525
|
*/
|
526
|
var UNDEFINED = new Object();
|
527
|
/**
|
528
|
* An abstract class for inserting the root test component element in a platform independent way.
|
529
|
*
|
530
|
* @experimental
|
531
|
*/
|
532
|
var TestComponentRenderer = (function () {
|
533
|
function TestComponentRenderer() {
|
534
|
}
|
535
|
TestComponentRenderer.prototype.insertRootElement = function (rootElementId) { };
|
536
|
return TestComponentRenderer;
|
537
|
}());
|
538
|
var _nextRootElementId = 0;
|
539
|
/**
|
540
|
* @experimental
|
541
|
*/
|
542
|
var ComponentFixtureAutoDetect = new InjectionToken('ComponentFixtureAutoDetect');
|
543
|
/**
|
544
|
* @experimental
|
545
|
*/
|
546
|
var ComponentFixtureNoNgZone = new InjectionToken('ComponentFixtureNoNgZone');
|
547
|
/**
|
548
|
* @whatItDoes Configures and initializes environment for unit testing and provides methods for
|
549
|
* creating components and services in unit tests.
|
550
|
* @description
|
551
|
*
|
552
|
* TestBed is the primary api for writing unit tests for Angular applications and libraries.
|
553
|
*
|
554
|
* @stable
|
555
|
*/
|
556
|
var TestBed = (function () {
|
557
|
function TestBed() {
|
558
|
this._instantiated = false;
|
559
|
this._compiler = null;
|
560
|
this._moduleRef = null;
|
561
|
this._moduleFactory = null;
|
562
|
this._compilerOptions = [];
|
563
|
this._moduleOverrides = [];
|
564
|
this._componentOverrides = [];
|
565
|
this._directiveOverrides = [];
|
566
|
this._pipeOverrides = [];
|
567
|
this._providers = [];
|
568
|
this._declarations = [];
|
569
|
this._imports = [];
|
570
|
this._schemas = [];
|
571
|
this._activeFixtures = [];
|
572
|
this._aotSummaries = function () { return []; };
|
573
|
this.platform = null;
|
574
|
this.ngModule = null;
|
575
|
}
|
576
|
/**
|
577
|
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
|
578
|
* angular module. These are common to every test in the suite.
|
579
|
*
|
580
|
* This may only be called once, to set up the common providers for the current test
|
581
|
* suite on the current platform. If you absolutely need to change the providers,
|
582
|
* first use `resetTestEnvironment`.
|
583
|
*
|
584
|
* Test modules and platforms for individual platforms are available from
|
585
|
* '@angular/<platform_name>/testing'.
|
586
|
*
|
587
|
* @experimental
|
588
|
*/
|
589
|
TestBed.initTestEnvironment = function (ngModule, platform, aotSummaries) {
|
590
|
var testBed = getTestBed();
|
591
|
testBed.initTestEnvironment(ngModule, platform, aotSummaries);
|
592
|
return testBed;
|
593
|
};
|
594
|
/**
|
595
|
* Reset the providers for the test injector.
|
596
|
*
|
597
|
* @experimental
|
598
|
*/
|
599
|
TestBed.resetTestEnvironment = function () { getTestBed().resetTestEnvironment(); };
|
600
|
TestBed.resetTestingModule = function () {
|
601
|
getTestBed().resetTestingModule();
|
602
|
return TestBed;
|
603
|
};
|
604
|
/**
|
605
|
* Allows overriding default compiler providers and settings
|
606
|
* which are defined in test_injector.js
|
607
|
*/
|
608
|
TestBed.configureCompiler = function (config) {
|
609
|
getTestBed().configureCompiler(config);
|
610
|
return TestBed;
|
611
|
};
|
612
|
/**
|
613
|
* Allows overriding default providers, directives, pipes, modules of the test injector,
|
614
|
* which are defined in test_injector.js
|
615
|
*/
|
616
|
TestBed.configureTestingModule = function (moduleDef) {
|
617
|
getTestBed().configureTestingModule(moduleDef);
|
618
|
return TestBed;
|
619
|
};
|
620
|
/**
|
621
|
* Compile components with a `templateUrl` for the test's NgModule.
|
622
|
* It is necessary to call this function
|
623
|
* as fetching urls is asynchronous.
|
624
|
*/
|
625
|
TestBed.compileComponents = function () { return getTestBed().compileComponents(); };
|
626
|
TestBed.overrideModule = function (ngModule, override) {
|
627
|
getTestBed().overrideModule(ngModule, override);
|
628
|
return TestBed;
|
629
|
};
|
630
|
TestBed.overrideComponent = function (component, override) {
|
631
|
getTestBed().overrideComponent(component, override);
|
632
|
return TestBed;
|
633
|
};
|
634
|
TestBed.overrideDirective = function (directive, override) {
|
635
|
getTestBed().overrideDirective(directive, override);
|
636
|
return TestBed;
|
637
|
};
|
638
|
TestBed.overridePipe = function (pipe, override) {
|
639
|
getTestBed().overridePipe(pipe, override);
|
640
|
return TestBed;
|
641
|
};
|
642
|
TestBed.overrideTemplate = function (component, template) {
|
643
|
getTestBed().overrideComponent(component, { set: { template: template, templateUrl: null } });
|
644
|
return TestBed;
|
645
|
};
|
646
|
TestBed.overrideProvider = function (token, provider) {
|
647
|
getTestBed().overrideProvider(token, provider);
|
648
|
return TestBed;
|
649
|
};
|
650
|
TestBed.deprecatedOverrideProvider = function (token, provider) {
|
651
|
getTestBed().deprecatedOverrideProvider(token, provider);
|
652
|
return TestBed;
|
653
|
};
|
654
|
TestBed.get = function (token, notFoundValue) {
|
655
|
if (notFoundValue === void 0) { notFoundValue = Injector.THROW_IF_NOT_FOUND; }
|
656
|
return getTestBed().get(token, notFoundValue);
|
657
|
};
|
658
|
TestBed.createComponent = function (component) {
|
659
|
return getTestBed().createComponent(component);
|
660
|
};
|
661
|
/**
|
662
|
* Initialize the environment for testing with a compiler factory, a PlatformRef, and an
|
663
|
* angular module. These are common to every test in the suite.
|
664
|
*
|
665
|
* This may only be called once, to set up the common providers for the current test
|
666
|
* suite on the current platform. If you absolutely need to change the providers,
|
667
|
* first use `resetTestEnvironment`.
|
668
|
*
|
669
|
* Test modules and platforms for individual platforms are available from
|
670
|
* '@angular/<platform_name>/testing'.
|
671
|
*
|
672
|
* @experimental
|
673
|
*/
|
674
|
TestBed.prototype.initTestEnvironment = function (ngModule, platform, aotSummaries) {
|
675
|
if (this.platform || this.ngModule) {
|
676
|
throw new Error('Cannot set base providers because it has already been called');
|
677
|
}
|
678
|
this.platform = platform;
|
679
|
this.ngModule = ngModule;
|
680
|
if (aotSummaries) {
|
681
|
this._aotSummaries = aotSummaries;
|
682
|
}
|
683
|
};
|
684
|
/**
|
685
|
* Reset the providers for the test injector.
|
686
|
*
|
687
|
* @experimental
|
688
|
*/
|
689
|
TestBed.prototype.resetTestEnvironment = function () {
|
690
|
this.resetTestingModule();
|
691
|
this.platform = null;
|
692
|
this.ngModule = null;
|
693
|
this._aotSummaries = function () { return []; };
|
694
|
};
|
695
|
TestBed.prototype.resetTestingModule = function () {
|
696
|
ɵclearProviderOverrides();
|
697
|
this._compiler = null;
|
698
|
this._moduleOverrides = [];
|
699
|
this._componentOverrides = [];
|
700
|
this._directiveOverrides = [];
|
701
|
this._pipeOverrides = [];
|
702
|
this._moduleRef = null;
|
703
|
this._moduleFactory = null;
|
704
|
this._compilerOptions = [];
|
705
|
this._providers = [];
|
706
|
this._declarations = [];
|
707
|
this._imports = [];
|
708
|
this._schemas = [];
|
709
|
this._instantiated = false;
|
710
|
this._activeFixtures.forEach(function (fixture) {
|
711
|
try {
|
712
|
fixture.destroy();
|
713
|
}
|
714
|
catch (e) {
|
715
|
console.error('Error during cleanup of component', fixture.componentInstance);
|
716
|
}
|
717
|
});
|
718
|
this._activeFixtures = [];
|
719
|
};
|
720
|
TestBed.prototype.configureCompiler = function (config) {
|
721
|
this._assertNotInstantiated('TestBed.configureCompiler', 'configure the compiler');
|
722
|
this._compilerOptions.push(config);
|
723
|
};
|
724
|
TestBed.prototype.configureTestingModule = function (moduleDef) {
|
725
|
this._assertNotInstantiated('TestBed.configureTestingModule', 'configure the test module');
|
726
|
if (moduleDef.providers) {
|
727
|
(_a = this._providers).push.apply(_a, moduleDef.providers);
|
728
|
}
|
729
|
if (moduleDef.declarations) {
|
730
|
(_b = this._declarations).push.apply(_b, moduleDef.declarations);
|
731
|
}
|
732
|
if (moduleDef.imports) {
|
733
|
(_c = this._imports).push.apply(_c, moduleDef.imports);
|
734
|
}
|
735
|
if (moduleDef.schemas) {
|
736
|
(_d = this._schemas).push.apply(_d, moduleDef.schemas);
|
737
|
}
|
738
|
var _a, _b, _c, _d;
|
739
|
};
|
740
|
TestBed.prototype.compileComponents = function () {
|
741
|
var _this = this;
|
742
|
if (this._moduleFactory || this._instantiated) {
|
743
|
return Promise.resolve(null);
|
744
|
}
|
745
|
var moduleType = this._createCompilerAndModule();
|
746
|
return this._compiler.compileModuleAndAllComponentsAsync(moduleType)
|
747
|
.then(function (moduleAndComponentFactories) {
|
748
|
_this._moduleFactory = moduleAndComponentFactories.ngModuleFactory;
|
749
|
});
|
750
|
};
|
751
|
TestBed.prototype._initIfNeeded = function () {
|
752
|
if (this._instantiated) {
|
753
|
return;
|
754
|
}
|
755
|
if (!this._moduleFactory) {
|
756
|
try {
|
757
|
var moduleType = this._createCompilerAndModule();
|
758
|
this._moduleFactory =
|
759
|
this._compiler.compileModuleAndAllComponentsSync(moduleType).ngModuleFactory;
|
760
|
}
|
761
|
catch (e) {
|
762
|
if (getComponentType(e)) {
|
763
|
throw new Error("This test module uses the component " + ɵstringify(getComponentType(e)) + " which is using a \"templateUrl\" or \"styleUrls\", but they were never compiled. " +
|
764
|
"Please call \"TestBed.compileComponents\" before your test.");
|
765
|
}
|
766
|
else {
|
767
|
throw e;
|
768
|
}
|
769
|
}
|
770
|
}
|
771
|
var ngZone = new NgZone({ enableLongStackTrace: true });
|
772
|
var ngZoneInjector = ReflectiveInjector.resolveAndCreate([{ provide: NgZone, useValue: ngZone }], this.platform.injector);
|
773
|
this._moduleRef = this._moduleFactory.create(ngZoneInjector);
|
774
|
// ApplicationInitStatus.runInitializers() is marked @internal to core. So casting to any
|
775
|
// before accessing it.
|
776
|
this._moduleRef.injector.get(ApplicationInitStatus).runInitializers();
|
777
|
this._instantiated = true;
|
778
|
};
|
779
|
TestBed.prototype._createCompilerAndModule = function () {
|
780
|
var _this = this;
|
781
|
var providers = this._providers.concat([{ provide: TestBed, useValue: this }]);
|
782
|
var declarations = this._declarations;
|
783
|
var imports = [this.ngModule, this._imports];
|
784
|
var schemas = this._schemas;
|
785
|
var DynamicTestModule = (function () {
|
786
|
function DynamicTestModule() {
|
787
|
}
|
788
|
return DynamicTestModule;
|
789
|
}());
|
790
|
DynamicTestModule.decorators = [
|
791
|
{ type: NgModule, args: [{ providers: providers, declarations: declarations, imports: imports, schemas: schemas },] },
|
792
|
];
|
793
|
/** @nocollapse */
|
794
|
DynamicTestModule.ctorParameters = function () { return []; };
|
795
|
var compilerFactory = this.platform.injector.get(TestingCompilerFactory);
|
796
|
this._compiler =
|
797
|
compilerFactory.createTestingCompiler(this._compilerOptions.concat([{ useDebug: true }]));
|
798
|
this._compiler.loadAotSummaries(this._aotSummaries);
|
799
|
this._moduleOverrides.forEach(function (entry) { return _this._compiler.overrideModule(entry[0], entry[1]); });
|
800
|
this._componentOverrides.forEach(function (entry) { return _this._compiler.overrideComponent(entry[0], entry[1]); });
|
801
|
this._directiveOverrides.forEach(function (entry) { return _this._compiler.overrideDirective(entry[0], entry[1]); });
|
802
|
this._pipeOverrides.forEach(function (entry) { return _this._compiler.overridePipe(entry[0], entry[1]); });
|
803
|
return DynamicTestModule;
|
804
|
};
|
805
|
TestBed.prototype._assertNotInstantiated = function (methodName, methodDescription) {
|
806
|
if (this._instantiated) {
|
807
|
throw new Error("Cannot " + methodDescription + " when the test module has already been instantiated. " +
|
808
|
("Make sure you are not using `inject` before `" + methodName + "`."));
|
809
|
}
|
810
|
};
|
811
|
TestBed.prototype.get = function (token, notFoundValue) {
|
812
|
if (notFoundValue === void 0) { notFoundValue = Injector.THROW_IF_NOT_FOUND; }
|
813
|
this._initIfNeeded();
|
814
|
if (token === TestBed) {
|
815
|
return this;
|
816
|
}
|
817
|
// Tests can inject things from the ng module and from the compiler,
|
818
|
// but the ng module can't inject things from the compiler and vice versa.
|
819
|
var result = this._moduleRef.injector.get(token, UNDEFINED);
|
820
|
return result === UNDEFINED ? this._compiler.injector.get(token, notFoundValue) : result;
|
821
|
};
|
822
|
TestBed.prototype.execute = function (tokens, fn, context) {
|
823
|
var _this = this;
|
824
|
this._initIfNeeded();
|
825
|
var params = tokens.map(function (t) { return _this.get(t); });
|
826
|
return fn.apply(context, params);
|
827
|
};
|
828
|
TestBed.prototype.overrideModule = function (ngModule, override) {
|
829
|
this._assertNotInstantiated('overrideModule', 'override module metadata');
|
830
|
this._moduleOverrides.push([ngModule, override]);
|
831
|
};
|
832
|
TestBed.prototype.overrideComponent = function (component, override) {
|
833
|
this._assertNotInstantiated('overrideComponent', 'override component metadata');
|
834
|
this._componentOverrides.push([component, override]);
|
835
|
};
|
836
|
TestBed.prototype.overrideDirective = function (directive, override) {
|
837
|
this._assertNotInstantiated('overrideDirective', 'override directive metadata');
|
838
|
this._directiveOverrides.push([directive, override]);
|
839
|
};
|
840
|
TestBed.prototype.overridePipe = function (pipe, override) {
|
841
|
this._assertNotInstantiated('overridePipe', 'override pipe metadata');
|
842
|
this._pipeOverrides.push([pipe, override]);
|
843
|
};
|
844
|
TestBed.prototype.overrideProvider = function (token, provider) {
|
845
|
this.overrideProviderImpl(token, provider);
|
846
|
};
|
847
|
TestBed.prototype.deprecatedOverrideProvider = function (token, provider) {
|
848
|
this.overrideProviderImpl(token, provider, /* deprecated */ true);
|
849
|
};
|
850
|
TestBed.prototype.overrideProviderImpl = function (token, provider, deprecated) {
|
851
|
if (deprecated === void 0) { deprecated = false; }
|
852
|
var flags = 0;
|
853
|
var value;
|
854
|
if (provider.useFactory) {
|
855
|
flags |= 1024 /* TypeFactoryProvider */;
|
856
|
value = provider.useFactory;
|
857
|
}
|
858
|
else {
|
859
|
flags |= 256 /* TypeValueProvider */;
|
860
|
value = provider.useValue;
|
861
|
}
|
862
|
var deps = (provider.deps || []).map(function (dep) {
|
863
|
var depFlags = 0;
|
864
|
var depToken;
|
865
|
if (Array.isArray(dep)) {
|
866
|
dep.forEach(function (entry) {
|
867
|
if (entry instanceof Optional) {
|
868
|
depFlags |= 2 /* Optional */;
|
869
|
}
|
870
|
else if (entry instanceof SkipSelf) {
|
871
|
depFlags |= 1 /* SkipSelf */;
|
872
|
}
|
873
|
else {
|
874
|
depToken = entry;
|
875
|
}
|
876
|
});
|
877
|
}
|
878
|
else {
|
879
|
depToken = dep;
|
880
|
}
|
881
|
return [depFlags, depToken];
|
882
|
});
|
883
|
ɵoverrideProvider({ token: token, flags: flags, deps: deps, value: value, deprecatedBehavior: deprecated });
|
884
|
};
|
885
|
TestBed.prototype.createComponent = function (component) {
|
886
|
var _this = this;
|
887
|
this._initIfNeeded();
|
888
|
var componentFactory = this._compiler.getComponentFactory(component);
|
889
|
if (!componentFactory) {
|
890
|
throw new Error("Cannot create the component " + ɵstringify(component) + " as it was not imported into the testing module!");
|
891
|
}
|
892
|
var noNgZone = this.get(ComponentFixtureNoNgZone, false);
|
893
|
var autoDetect = this.get(ComponentFixtureAutoDetect, false);
|
894
|
var ngZone = noNgZone ? null : this.get(NgZone, null);
|
895
|
var testComponentRenderer = this.get(TestComponentRenderer);
|
896
|
var rootElId = "root" + _nextRootElementId++;
|
897
|
testComponentRenderer.insertRootElement(rootElId);
|
898
|
var initComponent = function () {
|
899
|
var componentRef = componentFactory.create(Injector.NULL, [], "#" + rootElId, _this._moduleRef);
|
900
|
return new ComponentFixture(componentRef, ngZone, autoDetect);
|
901
|
};
|
902
|
var fixture = !ngZone ? initComponent() : ngZone.run(initComponent);
|
903
|
this._activeFixtures.push(fixture);
|
904
|
return fixture;
|
905
|
};
|
906
|
return TestBed;
|
907
|
}());
|
908
|
var _testBed = null;
|
909
|
/**
|
910
|
* @experimental
|
911
|
*/
|
912
|
function getTestBed() {
|
913
|
return _testBed = _testBed || new TestBed();
|
914
|
}
|
915
|
/**
|
916
|
* Allows injecting dependencies in `beforeEach()` and `it()`.
|
917
|
*
|
918
|
* Example:
|
919
|
*
|
920
|
* ```
|
921
|
* beforeEach(inject([Dependency, AClass], (dep, object) => {
|
922
|
* // some code that uses `dep` and `object`
|
923
|
* // ...
|
924
|
* }));
|
925
|
*
|
926
|
* it('...', inject([AClass], (object) => {
|
927
|
* object.doSomething();
|
928
|
* expect(...);
|
929
|
* })
|
930
|
* ```
|
931
|
*
|
932
|
* Notes:
|
933
|
* - inject is currently a function because of some Traceur limitation the syntax should
|
934
|
* eventually
|
935
|
* becomes `it('...', @Inject (object: AClass, async: AsyncTestCompleter) => { ... });`
|
936
|
*
|
937
|
* @stable
|
938
|
*/
|
939
|
function inject(tokens, fn) {
|
940
|
var testBed = getTestBed();
|
941
|
if (tokens.indexOf(AsyncTestCompleter) >= 0) {
|
942
|
// Not using an arrow function to preserve context passed from call site
|
943
|
return function () {
|
944
|
var _this = this;
|
945
|
// Return an async test method that returns a Promise if AsyncTestCompleter is one of
|
946
|
// the injected tokens.
|
947
|
return testBed.compileComponents().then(function () {
|
948
|
var completer = testBed.get(AsyncTestCompleter);
|
949
|
testBed.execute(tokens, fn, _this);
|
950
|
return completer.promise;
|
951
|
});
|
952
|
};
|
953
|
}
|
954
|
else {
|
955
|
// Not using an arrow function to preserve context passed from call site
|
956
|
return function () { return testBed.execute(tokens, fn, this); };
|
957
|
}
|
958
|
}
|
959
|
/**
|
960
|
* @experimental
|
961
|
*/
|
962
|
var InjectSetupWrapper = (function () {
|
963
|
function InjectSetupWrapper(_moduleDef) {
|
964
|
this._moduleDef = _moduleDef;
|
965
|
}
|
966
|
InjectSetupWrapper.prototype._addModule = function () {
|
967
|
var moduleDef = this._moduleDef();
|
968
|
if (moduleDef) {
|
969
|
getTestBed().configureTestingModule(moduleDef);
|
970
|
}
|
971
|
};
|
972
|
InjectSetupWrapper.prototype.inject = function (tokens, fn) {
|
973
|
var self = this;
|
974
|
// Not using an arrow function to preserve context passed from call site
|
975
|
return function () {
|
976
|
self._addModule();
|
977
|
return inject(tokens, fn).call(this);
|
978
|
};
|
979
|
};
|
980
|
return InjectSetupWrapper;
|
981
|
}());
|
982
|
function withModule(moduleDef, fn) {
|
983
|
if (fn) {
|
984
|
// Not using an arrow function to preserve context passed from call site
|
985
|
return function () {
|
986
|
var testBed = getTestBed();
|
987
|
if (moduleDef) {
|
988
|
testBed.configureTestingModule(moduleDef);
|
989
|
}
|
990
|
return fn.apply(this);
|
991
|
};
|
992
|
}
|
993
|
return new InjectSetupWrapper(function () { return moduleDef; });
|
994
|
}
|
995
|
function getComponentType(error) {
|
996
|
return error[ɵERROR_COMPONENT_TYPE];
|
997
|
}
|
998
|
/**
|
999
|
* @license
|
1000
|
* Copyright Google Inc. All Rights Reserved.
|
1001
|
*
|
1002
|
* Use of this source code is governed by an MIT-style license that can be
|
1003
|
* found in the LICENSE file at https://angular.io/license
|
1004
|
*/
|
1005
|
/**
|
1006
|
* Public Test Library for unit testing Angular applications. Assumes that you are running
|
1007
|
* with Jasmine, Mocha, or a similar framework which exports a beforeEach function and
|
1008
|
* allows tests to be asynchronous by either returning a promise or using a 'done' parameter.
|
1009
|
*/
|
1010
|
var _global$1 = (typeof window === 'undefined' ? global : window);
|
1011
|
// Reset the test providers and the fake async zone before each test.
|
1012
|
if (_global$1.beforeEach) {
|
1013
|
_global$1.beforeEach(function () {
|
1014
|
TestBed.resetTestingModule();
|
1015
|
resetFakeAsyncZone();
|
1016
|
});
|
1017
|
}
|
1018
|
// TODO(juliemr): remove this, only used because we need to export something to have compilation
|
1019
|
// work.
|
1020
|
var __core_private_testing_placeholder__ = '';
|
1021
|
/**
|
1022
|
* @license
|
1023
|
* Copyright Google Inc. All Rights Reserved.
|
1024
|
*
|
1025
|
* Use of this source code is governed by an MIT-style license that can be
|
1026
|
* found in the LICENSE file at https://angular.io/license
|
1027
|
*/
|
1028
|
/**
|
1029
|
* @license
|
1030
|
* Copyright Google Inc. All Rights Reserved.
|
1031
|
*
|
1032
|
* Use of this source code is governed by an MIT-style license that can be
|
1033
|
* found in the LICENSE file at https://angular.io/license
|
1034
|
*/
|
1035
|
/**
|
1036
|
* @module
|
1037
|
* @description
|
1038
|
* Entry point for all public APIs of the core/testing package.
|
1039
|
*/
|
1040
|
/**
|
1041
|
* @license
|
1042
|
* Copyright Google Inc. All Rights Reserved.
|
1043
|
*
|
1044
|
* Use of this source code is governed by an MIT-style license that can be
|
1045
|
* found in the LICENSE file at https://angular.io/license
|
1046
|
*/
|
1047
|
/**
|
1048
|
* @module
|
1049
|
* @description
|
1050
|
* Entry point for all public APIs of the core/testing package.
|
1051
|
*/
|
1052
|
export { async, ComponentFixture, resetFakeAsyncZone, fakeAsync, tick, flush, discardPeriodicTasks, flushMicrotasks, TestComponentRenderer, ComponentFixtureAutoDetect, ComponentFixtureNoNgZone, TestBed, getTestBed, inject, InjectSetupWrapper, withModule, __core_private_testing_placeholder__, TestingCompiler as ɵTestingCompiler, TestingCompilerFactory as ɵTestingCompilerFactory };
|
1053
|
//# sourceMappingURL=testing.es5.js.map
|