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