[6a3a178] | 1 | 'use strict';
|
---|
| 2 | /**
|
---|
| 3 | * @license Angular v12.0.0-next.0
|
---|
| 4 | * (c) 2010-2020 Google LLC. https://angular.io/
|
---|
| 5 | * License: MIT
|
---|
| 6 | */
|
---|
| 7 | (function (factory) {
|
---|
| 8 | typeof define === 'function' && define.amd ? define(factory) :
|
---|
| 9 | factory();
|
---|
| 10 | }((function () {
|
---|
| 11 | 'use strict';
|
---|
| 12 | /**
|
---|
| 13 | * @license
|
---|
| 14 | * Copyright Google LLC All Rights Reserved.
|
---|
| 15 | *
|
---|
| 16 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 17 | * found in the LICENSE file at https://angular.io/license
|
---|
| 18 | */
|
---|
| 19 | (function (_global) {
|
---|
| 20 | var AsyncTestZoneSpec = /** @class */ (function () {
|
---|
| 21 | function AsyncTestZoneSpec(finishCallback, failCallback, namePrefix) {
|
---|
| 22 | this.finishCallback = finishCallback;
|
---|
| 23 | this.failCallback = failCallback;
|
---|
| 24 | this._pendingMicroTasks = false;
|
---|
| 25 | this._pendingMacroTasks = false;
|
---|
| 26 | this._alreadyErrored = false;
|
---|
| 27 | this._isSync = false;
|
---|
| 28 | this.runZone = Zone.current;
|
---|
| 29 | this.unresolvedChainedPromiseCount = 0;
|
---|
| 30 | this.supportWaitUnresolvedChainedPromise = false;
|
---|
| 31 | this.name = 'asyncTestZone for ' + namePrefix;
|
---|
| 32 | this.properties = { 'AsyncTestZoneSpec': this };
|
---|
| 33 | this.supportWaitUnresolvedChainedPromise =
|
---|
| 34 | _global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] === true;
|
---|
| 35 | }
|
---|
| 36 | AsyncTestZoneSpec.prototype.isUnresolvedChainedPromisePending = function () {
|
---|
| 37 | return this.unresolvedChainedPromiseCount > 0;
|
---|
| 38 | };
|
---|
| 39 | AsyncTestZoneSpec.prototype._finishCallbackIfDone = function () {
|
---|
| 40 | var _this = this;
|
---|
| 41 | if (!(this._pendingMicroTasks || this._pendingMacroTasks ||
|
---|
| 42 | (this.supportWaitUnresolvedChainedPromise && this.isUnresolvedChainedPromisePending()))) {
|
---|
| 43 | // We do this because we would like to catch unhandled rejected promises.
|
---|
| 44 | this.runZone.run(function () {
|
---|
| 45 | setTimeout(function () {
|
---|
| 46 | if (!_this._alreadyErrored && !(_this._pendingMicroTasks || _this._pendingMacroTasks)) {
|
---|
| 47 | _this.finishCallback();
|
---|
| 48 | }
|
---|
| 49 | }, 0);
|
---|
| 50 | });
|
---|
| 51 | }
|
---|
| 52 | };
|
---|
| 53 | AsyncTestZoneSpec.prototype.patchPromiseForTest = function () {
|
---|
| 54 | if (!this.supportWaitUnresolvedChainedPromise) {
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 | var patchPromiseForTest = Promise[Zone.__symbol__('patchPromiseForTest')];
|
---|
| 58 | if (patchPromiseForTest) {
|
---|
| 59 | patchPromiseForTest();
|
---|
| 60 | }
|
---|
| 61 | };
|
---|
| 62 | AsyncTestZoneSpec.prototype.unPatchPromiseForTest = function () {
|
---|
| 63 | if (!this.supportWaitUnresolvedChainedPromise) {
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 | var unPatchPromiseForTest = Promise[Zone.__symbol__('unPatchPromiseForTest')];
|
---|
| 67 | if (unPatchPromiseForTest) {
|
---|
| 68 | unPatchPromiseForTest();
|
---|
| 69 | }
|
---|
| 70 | };
|
---|
| 71 | AsyncTestZoneSpec.prototype.onScheduleTask = function (delegate, current, target, task) {
|
---|
| 72 | if (task.type !== 'eventTask') {
|
---|
| 73 | this._isSync = false;
|
---|
| 74 | }
|
---|
| 75 | if (task.type === 'microTask' && task.data && task.data instanceof Promise) {
|
---|
| 76 | // check whether the promise is a chained promise
|
---|
| 77 | if (task.data[AsyncTestZoneSpec.symbolParentUnresolved] === true) {
|
---|
| 78 | // chained promise is being scheduled
|
---|
| 79 | this.unresolvedChainedPromiseCount--;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | return delegate.scheduleTask(target, task);
|
---|
| 83 | };
|
---|
| 84 | AsyncTestZoneSpec.prototype.onInvokeTask = function (delegate, current, target, task, applyThis, applyArgs) {
|
---|
| 85 | if (task.type !== 'eventTask') {
|
---|
| 86 | this._isSync = false;
|
---|
| 87 | }
|
---|
| 88 | return delegate.invokeTask(target, task, applyThis, applyArgs);
|
---|
| 89 | };
|
---|
| 90 | AsyncTestZoneSpec.prototype.onCancelTask = function (delegate, current, target, task) {
|
---|
| 91 | if (task.type !== 'eventTask') {
|
---|
| 92 | this._isSync = false;
|
---|
| 93 | }
|
---|
| 94 | return delegate.cancelTask(target, task);
|
---|
| 95 | };
|
---|
| 96 | // Note - we need to use onInvoke at the moment to call finish when a test is
|
---|
| 97 | // fully synchronous. TODO(juliemr): remove this when the logic for
|
---|
| 98 | // onHasTask changes and it calls whenever the task queues are dirty.
|
---|
| 99 | // updated by(JiaLiPassion), only call finish callback when no task
|
---|
| 100 | // was scheduled/invoked/canceled.
|
---|
| 101 | AsyncTestZoneSpec.prototype.onInvoke = function (parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) {
|
---|
| 102 | try {
|
---|
| 103 | this._isSync = true;
|
---|
| 104 | return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
|
---|
| 105 | }
|
---|
| 106 | finally {
|
---|
| 107 | var afterTaskCounts = parentZoneDelegate._taskCounts;
|
---|
| 108 | if (this._isSync) {
|
---|
| 109 | this._finishCallbackIfDone();
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | };
|
---|
| 113 | AsyncTestZoneSpec.prototype.onHandleError = function (parentZoneDelegate, currentZone, targetZone, error) {
|
---|
| 114 | // Let the parent try to handle the error.
|
---|
| 115 | var result = parentZoneDelegate.handleError(targetZone, error);
|
---|
| 116 | if (result) {
|
---|
| 117 | this.failCallback(error);
|
---|
| 118 | this._alreadyErrored = true;
|
---|
| 119 | }
|
---|
| 120 | return false;
|
---|
| 121 | };
|
---|
| 122 | AsyncTestZoneSpec.prototype.onHasTask = function (delegate, current, target, hasTaskState) {
|
---|
| 123 | delegate.hasTask(target, hasTaskState);
|
---|
| 124 | if (hasTaskState.change == 'microTask') {
|
---|
| 125 | this._pendingMicroTasks = hasTaskState.microTask;
|
---|
| 126 | this._finishCallbackIfDone();
|
---|
| 127 | }
|
---|
| 128 | else if (hasTaskState.change == 'macroTask') {
|
---|
| 129 | this._pendingMacroTasks = hasTaskState.macroTask;
|
---|
| 130 | this._finishCallbackIfDone();
|
---|
| 131 | }
|
---|
| 132 | };
|
---|
| 133 | return AsyncTestZoneSpec;
|
---|
| 134 | }());
|
---|
| 135 | AsyncTestZoneSpec.symbolParentUnresolved = Zone.__symbol__('parentUnresolved');
|
---|
| 136 | // Export the class so that new instances can be created with proper
|
---|
| 137 | // constructor params.
|
---|
| 138 | Zone['AsyncTestZoneSpec'] = AsyncTestZoneSpec;
|
---|
| 139 | })(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global);
|
---|
| 140 | Zone.__load_patch('asynctest', function (global, Zone, api) {
|
---|
| 141 | /**
|
---|
| 142 | * Wraps a test function in an asynchronous test zone. The test will automatically
|
---|
| 143 | * complete when all asynchronous calls within this zone are done.
|
---|
| 144 | */
|
---|
| 145 | Zone[api.symbol('asyncTest')] = function asyncTest(fn) {
|
---|
| 146 | // If we're running using the Jasmine test framework, adapt to call the 'done'
|
---|
| 147 | // function when asynchronous activity is finished.
|
---|
| 148 | if (global.jasmine) {
|
---|
| 149 | // Not using an arrow function to preserve context passed from call site
|
---|
| 150 | return function (done) {
|
---|
| 151 | if (!done) {
|
---|
| 152 | // if we run beforeEach in @angular/core/testing/testing_internal then we get no done
|
---|
| 153 | // fake it here and assume sync.
|
---|
| 154 | done = function () { };
|
---|
| 155 | done.fail = function (e) {
|
---|
| 156 | throw e;
|
---|
| 157 | };
|
---|
| 158 | }
|
---|
| 159 | runInTestZone(fn, this, done, function (err) {
|
---|
| 160 | if (typeof err === 'string') {
|
---|
| 161 | return done.fail(new Error(err));
|
---|
| 162 | }
|
---|
| 163 | else {
|
---|
| 164 | done.fail(err);
|
---|
| 165 | }
|
---|
| 166 | });
|
---|
| 167 | };
|
---|
| 168 | }
|
---|
| 169 | // Otherwise, return a promise which will resolve when asynchronous activity
|
---|
| 170 | // is finished. This will be correctly consumed by the Mocha framework with
|
---|
| 171 | // it('...', async(myFn)); or can be used in a custom framework.
|
---|
| 172 | // Not using an arrow function to preserve context passed from call site
|
---|
| 173 | return function () {
|
---|
| 174 | var _this = this;
|
---|
| 175 | return new Promise(function (finishCallback, failCallback) {
|
---|
| 176 | runInTestZone(fn, _this, finishCallback, failCallback);
|
---|
| 177 | });
|
---|
| 178 | };
|
---|
| 179 | };
|
---|
| 180 | function runInTestZone(fn, context, finishCallback, failCallback) {
|
---|
| 181 | var currentZone = Zone.current;
|
---|
| 182 | var AsyncTestZoneSpec = Zone['AsyncTestZoneSpec'];
|
---|
| 183 | if (AsyncTestZoneSpec === undefined) {
|
---|
| 184 | throw new Error('AsyncTestZoneSpec is needed for the async() test helper but could not be found. ' +
|
---|
| 185 | 'Please make sure that your environment includes zone.js/dist/async-test.js');
|
---|
| 186 | }
|
---|
| 187 | var ProxyZoneSpec = Zone['ProxyZoneSpec'];
|
---|
| 188 | if (!ProxyZoneSpec) {
|
---|
| 189 | throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
|
---|
| 190 | 'Please make sure that your environment includes zone.js/dist/proxy.js');
|
---|
| 191 | }
|
---|
| 192 | var proxyZoneSpec = ProxyZoneSpec.get();
|
---|
| 193 | ProxyZoneSpec.assertPresent();
|
---|
| 194 | // We need to create the AsyncTestZoneSpec outside the ProxyZone.
|
---|
| 195 | // If we do it in ProxyZone then we will get to infinite recursion.
|
---|
| 196 | var proxyZone = Zone.current.getZoneWith('ProxyZoneSpec');
|
---|
| 197 | var previousDelegate = proxyZoneSpec.getDelegate();
|
---|
| 198 | proxyZone.parent.run(function () {
|
---|
| 199 | var testZoneSpec = new AsyncTestZoneSpec(function () {
|
---|
| 200 | // Need to restore the original zone.
|
---|
| 201 | if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
---|
| 202 | // Only reset the zone spec if it's
|
---|
| 203 | // sill this one. Otherwise, assume
|
---|
| 204 | // it's OK.
|
---|
| 205 | proxyZoneSpec.setDelegate(previousDelegate);
|
---|
| 206 | }
|
---|
| 207 | testZoneSpec.unPatchPromiseForTest();
|
---|
| 208 | currentZone.run(function () {
|
---|
| 209 | finishCallback();
|
---|
| 210 | });
|
---|
| 211 | }, function (error) {
|
---|
| 212 | // Need to restore the original zone.
|
---|
| 213 | if (proxyZoneSpec.getDelegate() == testZoneSpec) {
|
---|
| 214 | // Only reset the zone spec if it's sill this one. Otherwise, assume it's OK.
|
---|
| 215 | proxyZoneSpec.setDelegate(previousDelegate);
|
---|
| 216 | }
|
---|
| 217 | testZoneSpec.unPatchPromiseForTest();
|
---|
| 218 | currentZone.run(function () {
|
---|
| 219 | failCallback(error);
|
---|
| 220 | });
|
---|
| 221 | }, 'test');
|
---|
| 222 | proxyZoneSpec.setDelegate(testZoneSpec);
|
---|
| 223 | testZoneSpec.patchPromiseForTest();
|
---|
| 224 | });
|
---|
| 225 | return Zone.current.runGuarded(fn, context);
|
---|
| 226 | }
|
---|
| 227 | });
|
---|
| 228 | })));
|
---|