| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /**
- * @license
- * Copyright Google Inc. All Rights Reserved.
- *
- * Use of this source code is governed by an MIT-style license that can be
- * found in the LICENSE file at https://angular.io/license
- */
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
- typeof define === 'function' && define.amd ? define(factory) :
- (factory());
- }(this, (function () { 'use strict';
- /**
- * @license
- * Copyright Google Inc. All Rights Reserved.
- *
- * Use of this source code is governed by an MIT-style license that can be
- * found in the LICENSE file at https://angular.io/license
- */
- var _global = typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global;
- var AsyncTestZoneSpec = /** @class */ (function () {
- function AsyncTestZoneSpec(finishCallback, failCallback, namePrefix) {
- this.finishCallback = finishCallback;
- this.failCallback = failCallback;
- this._pendingMicroTasks = false;
- this._pendingMacroTasks = false;
- this._alreadyErrored = false;
- this._isSync = false;
- this.runZone = Zone.current;
- this.unresolvedChainedPromiseCount = 0;
- this.supportWaitUnresolvedChainedPromise = false;
- this.name = 'asyncTestZone for ' + namePrefix;
- this.properties = { 'AsyncTestZoneSpec': this };
- this.supportWaitUnresolvedChainedPromise =
- _global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] === true;
- }
- AsyncTestZoneSpec.prototype.isUnresolvedChainedPromisePending = function () {
- return this.unresolvedChainedPromiseCount > 0;
- };
- AsyncTestZoneSpec.prototype._finishCallbackIfDone = function () {
- var _this = this;
- if (!(this._pendingMicroTasks || this._pendingMacroTasks ||
- (this.supportWaitUnresolvedChainedPromise && this.isUnresolvedChainedPromisePending()))) {
- // We do this because we would like to catch unhandled rejected promises.
- this.runZone.run(function () {
- setTimeout(function () {
- if (!_this._alreadyErrored && !(_this._pendingMicroTasks || _this._pendingMacroTasks)) {
- _this.finishCallback();
- }
- }, 0);
- });
- }
- };
- AsyncTestZoneSpec.prototype.patchPromiseForTest = function () {
- if (!this.supportWaitUnresolvedChainedPromise) {
- return;
- }
- var patchPromiseForTest = Promise[Zone.__symbol__('patchPromiseForTest')];
- if (patchPromiseForTest) {
- patchPromiseForTest();
- }
- };
- AsyncTestZoneSpec.prototype.unPatchPromiseForTest = function () {
- if (!this.supportWaitUnresolvedChainedPromise) {
- return;
- }
- var unPatchPromiseForTest = Promise[Zone.__symbol__('unPatchPromiseForTest')];
- if (unPatchPromiseForTest) {
- unPatchPromiseForTest();
- }
- };
- AsyncTestZoneSpec.prototype.onScheduleTask = function (delegate, current, target, task) {
- if (task.type !== 'eventTask') {
- this._isSync = false;
- }
- if (task.type === 'microTask' && task.data && task.data instanceof Promise) {
- // check whether the promise is a chained promise
- if (task.data[AsyncTestZoneSpec.symbolParentUnresolved] === true) {
- // chained promise is being scheduled
- this.unresolvedChainedPromiseCount--;
- }
- }
- return delegate.scheduleTask(target, task);
- };
- AsyncTestZoneSpec.prototype.onInvokeTask = function (delegate, current, target, task, applyThis, applyArgs) {
- if (task.type !== 'eventTask') {
- this._isSync = false;
- }
- return delegate.invokeTask(target, task, applyThis, applyArgs);
- };
- AsyncTestZoneSpec.prototype.onCancelTask = function (delegate, current, target, task) {
- if (task.type !== 'eventTask') {
- this._isSync = false;
- }
- return delegate.cancelTask(target, task);
- };
- // Note - we need to use onInvoke at the moment to call finish when a test is
- // fully synchronous. TODO(juliemr): remove this when the logic for
- // onHasTask changes and it calls whenever the task queues are dirty.
- // updated by(JiaLiPassion), only call finish callback when no task
- // was scheduled/invoked/canceled.
- AsyncTestZoneSpec.prototype.onInvoke = function (parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
- try {
- this._isSync = true;
- return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
- }
- finally {
- var afterTaskCounts = parentZoneDelegate._taskCounts;
- if (this._isSync) {
- this._finishCallbackIfDone();
- }
- }
- };
- AsyncTestZoneSpec.prototype.onHandleError = function (parentZoneDelegate, currentZone, targetZone, error) {
- // Let the parent try to handle the error.
- var result = parentZoneDelegate.handleError(targetZone, error);
- if (result) {
- this.failCallback(error);
- this._alreadyErrored = true;
- }
- return false;
- };
- AsyncTestZoneSpec.prototype.onHasTask = function (delegate, current, target, hasTaskState) {
- delegate.hasTask(target, hasTaskState);
- if (hasTaskState.change == 'microTask') {
- this._pendingMicroTasks = hasTaskState.microTask;
- this._finishCallbackIfDone();
- }
- else if (hasTaskState.change == 'macroTask') {
- this._pendingMacroTasks = hasTaskState.macroTask;
- this._finishCallbackIfDone();
- }
- };
- AsyncTestZoneSpec.symbolParentUnresolved = Zone.__symbol__('parentUnresolved');
- return AsyncTestZoneSpec;
- }());
- // Export the class so that new instances can be created with proper
- // constructor params.
- Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
- /**
- * @license
- * Copyright Google Inc. All Rights Reserved.
- *
- * Use of this source code is governed by an MIT-style license that can be
- * found in the LICENSE file at https://angular.io/license
- */
- Zone.__load_patch('asynctest', function (global, Zone, api) {
- /**
- * Wraps a test function in an asynchronous test zone. The test will automatically
- * complete when all asynchronous calls within this zone are done.
- */
- Zone[api.symbol('asyncTest')] = function asyncTest(fn) {
- // If we're running using the Jasmine test framework, adapt to call the 'done'
- // function when asynchronous activity is finished.
- if (global.jasmine) {
- // Not using an arrow function to preserve context passed from call site
- return function (done) {
- if (!done) {
- // if we run beforeEach in @angular/core/testing/testing_internal then we get no done
- // fake it here and assume sync.
- done = function () { };
- done.fail = function (e) {
- throw e;
- };
- }
- runInTestZone(fn, this, done, function (err) {
- if (typeof err === 'string') {
- return done.fail(new Error(err));
- }
- else {
- done.fail(err);
- }
- });
- };
- }
- // Otherwise, return a promise which will resolve when asynchronous activity
- // is finished. This will be correctly consumed by the Mocha framework with
- // it('...', async(myFn)); or can be used in a custom framework.
- // Not using an arrow function to preserve context passed from call site
- return function () {
- var _this = this;
- return new Promise(function (finishCallback, failCallback) {
- runInTestZone(fn, _this, finishCallback, failCallback);
- });
- };
- };
- function runInTestZone(fn, context, finishCallback, failCallback) {
- var currentZone = Zone.current;
- var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
- if (AsyncTestZoneSpec === undefined) {
- throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
- 'Please make sure that your environment includes zone.js/dist/async-test.js');
- }
- var ProxyZoneSpec = Zone['ProxyZoneSpec'];
- if (ProxyZoneSpec === undefined) {
- throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
- 'Please make sure that your environment includes zone.js/dist/proxy.js');
- }
- var proxyZoneSpec = ProxyZoneSpec.get();
- ProxyZoneSpec.assertPresent();
- // We need to create the AsyncTestZoneSpec outside the ProxyZone.
- // If we do it in ProxyZone then we will get to infinite recursion.
- var proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
- var previousDelegate = proxyZoneSpec.getDelegate();
- proxyZone.parent.run(function () {
- var testZoneSpec = new AsyncTestZoneSpec(function () {
- // Need to restore the original zone.
- if (proxyZoneSpec.getDelegate() == testZoneSpec) {
- // Only reset the zone spec if it's
- // sill this one. Otherwise, assume
- // it's OK.
- proxyZoneSpec.setDelegate(previousDelegate);
- }
- testZoneSpec.unPatchPromiseForTest();
- currentZone.run(function () {
- finishCallback();
- });
- }, function (error) {
- // Need to restore the original zone.
- if (proxyZoneSpec.getDelegate() == testZoneSpec) {
- // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
- proxyZoneSpec.setDelegate(previousDelegate);
- }
- testZoneSpec.unPatchPromiseForTest();
- currentZone.run(function () {
- failCallback(error);
- });
- }, 'test');
- proxyZoneSpec.setDelegate(testZoneSpec);
- testZoneSpec.patchPromiseForTest();
- });
- return Zone.current.runGuarded(fn, context);
- }
- });
- })));
|