[6a3a178] | 1 | 'use strict';
|
---|
| 2 | var __spreadArrays = (this && this.__spreadArrays) || function () {
|
---|
| 3 | for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
---|
| 4 | for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
---|
| 5 | for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
---|
| 6 | r[k] = a[j];
|
---|
| 7 | return r;
|
---|
| 8 | };
|
---|
| 9 | /**
|
---|
| 10 | * @license Angular v12.0.0-next.0
|
---|
| 11 | * (c) 2010-2020 Google LLC. https://angular.io/
|
---|
| 12 | * License: MIT
|
---|
| 13 | */
|
---|
| 14 | (function (factory) {
|
---|
| 15 | typeof define === 'function' && define.amd ? define(factory) :
|
---|
| 16 | factory();
|
---|
| 17 | }((function () {
|
---|
| 18 | 'use strict';
|
---|
| 19 | /**
|
---|
| 20 | * @license
|
---|
| 21 | * Copyright Google LLC All Rights Reserved.
|
---|
| 22 | *
|
---|
| 23 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 24 | * found in the LICENSE file at https://angular.io/license
|
---|
| 25 | */
|
---|
| 26 | (function (global) {
|
---|
| 27 | var OriginalDate = global.Date;
|
---|
| 28 | // Since when we compile this file to `es2015`, and if we define
|
---|
| 29 | // this `FakeDate` as `class FakeDate`, and then set `FakeDate.prototype`
|
---|
| 30 | // there will be an error which is `Cannot assign to read only property 'prototype'`
|
---|
| 31 | // so we need to use function implementation here.
|
---|
| 32 | function FakeDate() {
|
---|
| 33 | if (arguments.length === 0) {
|
---|
| 34 | var d = new OriginalDate();
|
---|
| 35 | d.setTime(FakeDate.now());
|
---|
| 36 | return d;
|
---|
| 37 | }
|
---|
| 38 | else {
|
---|
| 39 | var args = Array.prototype.slice.call(arguments);
|
---|
| 40 | return new (OriginalDate.bind.apply(OriginalDate, __spreadArrays([void 0], args)))();
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 | FakeDate.now = function () {
|
---|
| 44 | var fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
---|
| 45 | if (fakeAsyncTestZoneSpec) {
|
---|
| 46 | return fakeAsyncTestZoneSpec.getFakeSystemTime();
|
---|
| 47 | }
|
---|
| 48 | return OriginalDate.now.apply(this, arguments);
|
---|
| 49 | };
|
---|
| 50 | FakeDate.UTC = OriginalDate.UTC;
|
---|
| 51 | FakeDate.parse = OriginalDate.parse;
|
---|
| 52 | // keep a reference for zone patched timer function
|
---|
| 53 | var timers = {
|
---|
| 54 | setTimeout: global.setTimeout,
|
---|
| 55 | setInterval: global.setInterval,
|
---|
| 56 | clearTimeout: global.clearTimeout,
|
---|
| 57 | clearInterval: global.clearInterval
|
---|
| 58 | };
|
---|
| 59 | var Scheduler = /** @class */ (function () {
|
---|
| 60 | function Scheduler() {
|
---|
| 61 | // Scheduler queue with the tuple of end time and callback function - sorted by end time.
|
---|
| 62 | this._schedulerQueue = [];
|
---|
| 63 | // Current simulated time in millis.
|
---|
| 64 | this._currentTickTime = 0;
|
---|
| 65 | // Current fake system base time in millis.
|
---|
| 66 | this._currentFakeBaseSystemTime = OriginalDate.now();
|
---|
| 67 | // track requeuePeriodicTimer
|
---|
| 68 | this._currentTickRequeuePeriodicEntries = [];
|
---|
| 69 | }
|
---|
| 70 | Scheduler.prototype.getCurrentTickTime = function () {
|
---|
| 71 | return this._currentTickTime;
|
---|
| 72 | };
|
---|
| 73 | Scheduler.prototype.getFakeSystemTime = function () {
|
---|
| 74 | return this._currentFakeBaseSystemTime + this._currentTickTime;
|
---|
| 75 | };
|
---|
| 76 | Scheduler.prototype.setFakeBaseSystemTime = function (fakeBaseSystemTime) {
|
---|
| 77 | this._currentFakeBaseSystemTime = fakeBaseSystemTime;
|
---|
| 78 | };
|
---|
| 79 | Scheduler.prototype.getRealSystemTime = function () {
|
---|
| 80 | return OriginalDate.now();
|
---|
| 81 | };
|
---|
| 82 | Scheduler.prototype.scheduleFunction = function (cb, delay, options) {
|
---|
| 83 | options = Object.assign({
|
---|
| 84 | args: [],
|
---|
| 85 | isPeriodic: false,
|
---|
| 86 | isRequestAnimationFrame: false,
|
---|
| 87 | id: -1,
|
---|
| 88 | isRequeuePeriodic: false
|
---|
| 89 | }, options);
|
---|
| 90 | var currentId = options.id < 0 ? Scheduler.nextId++ : options.id;
|
---|
| 91 | var endTime = this._currentTickTime + delay;
|
---|
| 92 | // Insert so that scheduler queue remains sorted by end time.
|
---|
| 93 | var newEntry = {
|
---|
| 94 | endTime: endTime,
|
---|
| 95 | id: currentId,
|
---|
| 96 | func: cb,
|
---|
| 97 | args: options.args,
|
---|
| 98 | delay: delay,
|
---|
| 99 | isPeriodic: options.isPeriodic,
|
---|
| 100 | isRequestAnimationFrame: options.isRequestAnimationFrame
|
---|
| 101 | };
|
---|
| 102 | if (options.isRequeuePeriodic) {
|
---|
| 103 | this._currentTickRequeuePeriodicEntries.push(newEntry);
|
---|
| 104 | }
|
---|
| 105 | var i = 0;
|
---|
| 106 | for (; i < this._schedulerQueue.length; i++) {
|
---|
| 107 | var currentEntry = this._schedulerQueue[i];
|
---|
| 108 | if (newEntry.endTime < currentEntry.endTime) {
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | this._schedulerQueue.splice(i, 0, newEntry);
|
---|
| 113 | return currentId;
|
---|
| 114 | };
|
---|
| 115 | Scheduler.prototype.removeScheduledFunctionWithId = function (id) {
|
---|
| 116 | for (var i = 0; i < this._schedulerQueue.length; i++) {
|
---|
| 117 | if (this._schedulerQueue[i].id == id) {
|
---|
| 118 | this._schedulerQueue.splice(i, 1);
|
---|
| 119 | break;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | };
|
---|
| 123 | Scheduler.prototype.removeAll = function () {
|
---|
| 124 | this._schedulerQueue = [];
|
---|
| 125 | };
|
---|
| 126 | Scheduler.prototype.getTimerCount = function () {
|
---|
| 127 | return this._schedulerQueue.length;
|
---|
| 128 | };
|
---|
| 129 | Scheduler.prototype.tickToNext = function (step, doTick, tickOptions) {
|
---|
| 130 | if (step === void 0) { step = 1; }
|
---|
| 131 | if (this._schedulerQueue.length < step) {
|
---|
| 132 | return;
|
---|
| 133 | }
|
---|
| 134 | // Find the last task currently queued in the scheduler queue and tick
|
---|
| 135 | // till that time.
|
---|
| 136 | var startTime = this._currentTickTime;
|
---|
| 137 | var targetTask = this._schedulerQueue[step - 1];
|
---|
| 138 | this.tick(targetTask.endTime - startTime, doTick, tickOptions);
|
---|
| 139 | };
|
---|
| 140 | Scheduler.prototype.tick = function (millis, doTick, tickOptions) {
|
---|
| 141 | if (millis === void 0) { millis = 0; }
|
---|
| 142 | var finalTime = this._currentTickTime + millis;
|
---|
| 143 | var lastCurrentTime = 0;
|
---|
| 144 | tickOptions = Object.assign({ processNewMacroTasksSynchronously: true }, tickOptions);
|
---|
| 145 | // we need to copy the schedulerQueue so nested timeout
|
---|
| 146 | // will not be wrongly called in the current tick
|
---|
| 147 | // https://github.com/angular/angular/issues/33799
|
---|
| 148 | var schedulerQueue = tickOptions.processNewMacroTasksSynchronously ?
|
---|
| 149 | this._schedulerQueue :
|
---|
| 150 | this._schedulerQueue.slice();
|
---|
| 151 | if (schedulerQueue.length === 0 && doTick) {
|
---|
| 152 | doTick(millis);
|
---|
| 153 | return;
|
---|
| 154 | }
|
---|
| 155 | while (schedulerQueue.length > 0) {
|
---|
| 156 | // clear requeueEntries before each loop
|
---|
| 157 | this._currentTickRequeuePeriodicEntries = [];
|
---|
| 158 | var current = schedulerQueue[0];
|
---|
| 159 | if (finalTime < current.endTime) {
|
---|
| 160 | // Done processing the queue since it's sorted by endTime.
|
---|
| 161 | break;
|
---|
| 162 | }
|
---|
| 163 | else {
|
---|
| 164 | // Time to run scheduled function. Remove it from the head of queue.
|
---|
| 165 | var current_1 = schedulerQueue.shift();
|
---|
| 166 | if (!tickOptions.processNewMacroTasksSynchronously) {
|
---|
| 167 | var idx = this._schedulerQueue.indexOf(current_1);
|
---|
| 168 | if (idx >= 0) {
|
---|
| 169 | this._schedulerQueue.splice(idx, 1);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | lastCurrentTime = this._currentTickTime;
|
---|
| 173 | this._currentTickTime = current_1.endTime;
|
---|
| 174 | if (doTick) {
|
---|
| 175 | doTick(this._currentTickTime - lastCurrentTime);
|
---|
| 176 | }
|
---|
| 177 | var retval = current_1.func.apply(global, current_1.isRequestAnimationFrame ? [this._currentTickTime] : current_1.args);
|
---|
| 178 | if (!retval) {
|
---|
| 179 | // Uncaught exception in the current scheduled function. Stop processing the queue.
|
---|
| 180 | break;
|
---|
| 181 | }
|
---|
| 182 | // check is there any requeue periodic entry is added in
|
---|
| 183 | // current loop, if there is, we need to add to current loop
|
---|
| 184 | if (!tickOptions.processNewMacroTasksSynchronously) {
|
---|
| 185 | this._currentTickRequeuePeriodicEntries.forEach(function (newEntry) {
|
---|
| 186 | var i = 0;
|
---|
| 187 | for (; i < schedulerQueue.length; i++) {
|
---|
| 188 | var currentEntry = schedulerQueue[i];
|
---|
| 189 | if (newEntry.endTime < currentEntry.endTime) {
|
---|
| 190 | break;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | schedulerQueue.splice(i, 0, newEntry);
|
---|
| 194 | });
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | lastCurrentTime = this._currentTickTime;
|
---|
| 199 | this._currentTickTime = finalTime;
|
---|
| 200 | if (doTick) {
|
---|
| 201 | doTick(this._currentTickTime - lastCurrentTime);
|
---|
| 202 | }
|
---|
| 203 | };
|
---|
| 204 | Scheduler.prototype.flushOnlyPendingTimers = function (doTick) {
|
---|
| 205 | if (this._schedulerQueue.length === 0) {
|
---|
| 206 | return 0;
|
---|
| 207 | }
|
---|
| 208 | // Find the last task currently queued in the scheduler queue and tick
|
---|
| 209 | // till that time.
|
---|
| 210 | var startTime = this._currentTickTime;
|
---|
| 211 | var lastTask = this._schedulerQueue[this._schedulerQueue.length - 1];
|
---|
| 212 | this.tick(lastTask.endTime - startTime, doTick, { processNewMacroTasksSynchronously: false });
|
---|
| 213 | return this._currentTickTime - startTime;
|
---|
| 214 | };
|
---|
| 215 | Scheduler.prototype.flush = function (limit, flushPeriodic, doTick) {
|
---|
| 216 | if (limit === void 0) { limit = 20; }
|
---|
| 217 | if (flushPeriodic === void 0) { flushPeriodic = false; }
|
---|
| 218 | if (flushPeriodic) {
|
---|
| 219 | return this.flushPeriodic(doTick);
|
---|
| 220 | }
|
---|
| 221 | else {
|
---|
| 222 | return this.flushNonPeriodic(limit, doTick);
|
---|
| 223 | }
|
---|
| 224 | };
|
---|
| 225 | Scheduler.prototype.flushPeriodic = function (doTick) {
|
---|
| 226 | if (this._schedulerQueue.length === 0) {
|
---|
| 227 | return 0;
|
---|
| 228 | }
|
---|
| 229 | // Find the last task currently queued in the scheduler queue and tick
|
---|
| 230 | // till that time.
|
---|
| 231 | var startTime = this._currentTickTime;
|
---|
| 232 | var lastTask = this._schedulerQueue[this._schedulerQueue.length - 1];
|
---|
| 233 | this.tick(lastTask.endTime - startTime, doTick);
|
---|
| 234 | return this._currentTickTime - startTime;
|
---|
| 235 | };
|
---|
| 236 | Scheduler.prototype.flushNonPeriodic = function (limit, doTick) {
|
---|
| 237 | var startTime = this._currentTickTime;
|
---|
| 238 | var lastCurrentTime = 0;
|
---|
| 239 | var count = 0;
|
---|
| 240 | while (this._schedulerQueue.length > 0) {
|
---|
| 241 | count++;
|
---|
| 242 | if (count > limit) {
|
---|
| 243 | throw new Error('flush failed after reaching the limit of ' + limit +
|
---|
| 244 | ' tasks. Does your code use a polling timeout?');
|
---|
| 245 | }
|
---|
| 246 | // flush only non-periodic timers.
|
---|
| 247 | // If the only remaining tasks are periodic(or requestAnimationFrame), finish flushing.
|
---|
| 248 | if (this._schedulerQueue.filter(function (task) { return !task.isPeriodic && !task.isRequestAnimationFrame; })
|
---|
| 249 | .length === 0) {
|
---|
| 250 | break;
|
---|
| 251 | }
|
---|
| 252 | var current = this._schedulerQueue.shift();
|
---|
| 253 | lastCurrentTime = this._currentTickTime;
|
---|
| 254 | this._currentTickTime = current.endTime;
|
---|
| 255 | if (doTick) {
|
---|
| 256 | // Update any secondary schedulers like Jasmine mock Date.
|
---|
| 257 | doTick(this._currentTickTime - lastCurrentTime);
|
---|
| 258 | }
|
---|
| 259 | var retval = current.func.apply(global, current.args);
|
---|
| 260 | if (!retval) {
|
---|
| 261 | // Uncaught exception in the current scheduled function. Stop processing the queue.
|
---|
| 262 | break;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | return this._currentTickTime - startTime;
|
---|
| 266 | };
|
---|
| 267 | return Scheduler;
|
---|
| 268 | }());
|
---|
| 269 | // Next scheduler id.
|
---|
| 270 | Scheduler.nextId = 1;
|
---|
| 271 | var FakeAsyncTestZoneSpec = /** @class */ (function () {
|
---|
| 272 | function FakeAsyncTestZoneSpec(namePrefix, trackPendingRequestAnimationFrame, macroTaskOptions) {
|
---|
| 273 | if (trackPendingRequestAnimationFrame === void 0) { trackPendingRequestAnimationFrame = false; }
|
---|
| 274 | this.trackPendingRequestAnimationFrame = trackPendingRequestAnimationFrame;
|
---|
| 275 | this.macroTaskOptions = macroTaskOptions;
|
---|
| 276 | this._scheduler = new Scheduler();
|
---|
| 277 | this._microtasks = [];
|
---|
| 278 | this._lastError = null;
|
---|
| 279 | this._uncaughtPromiseErrors = Promise[Zone.__symbol__('uncaughtPromiseErrors')];
|
---|
| 280 | this.pendingPeriodicTimers = [];
|
---|
| 281 | this.pendingTimers = [];
|
---|
| 282 | this.patchDateLocked = false;
|
---|
| 283 | this.properties = { 'FakeAsyncTestZoneSpec': this };
|
---|
| 284 | this.name = 'fakeAsyncTestZone for ' + namePrefix;
|
---|
| 285 | // in case user can't access the construction of FakeAsyncTestSpec
|
---|
| 286 | // user can also define macroTaskOptions by define a global variable.
|
---|
| 287 | if (!this.macroTaskOptions) {
|
---|
| 288 | this.macroTaskOptions = global[Zone.__symbol__('FakeAsyncTestMacroTask')];
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 | FakeAsyncTestZoneSpec.assertInZone = function () {
|
---|
| 292 | if (Zone.current.get('FakeAsyncTestZoneSpec') == null) {
|
---|
| 293 | throw new Error('The code should be running in the fakeAsync zone to call this function');
|
---|
| 294 | }
|
---|
| 295 | };
|
---|
| 296 | FakeAsyncTestZoneSpec.prototype._fnAndFlush = function (fn, completers) {
|
---|
| 297 | var _this = this;
|
---|
| 298 | return function () {
|
---|
| 299 | var args = [];
|
---|
| 300 | for (var _i = 0; _i < arguments.length; _i++) {
|
---|
| 301 | args[_i] = arguments[_i];
|
---|
| 302 | }
|
---|
| 303 | fn.apply(global, args);
|
---|
| 304 | if (_this._lastError === null) { // Success
|
---|
| 305 | if (completers.onSuccess != null) {
|
---|
| 306 | completers.onSuccess.apply(global);
|
---|
| 307 | }
|
---|
| 308 | // Flush microtasks only on success.
|
---|
| 309 | _this.flushMicrotasks();
|
---|
| 310 | }
|
---|
| 311 | else { // Failure
|
---|
| 312 | if (completers.onError != null) {
|
---|
| 313 | completers.onError.apply(global);
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | // Return true if there were no errors, false otherwise.
|
---|
| 317 | return _this._lastError === null;
|
---|
| 318 | };
|
---|
| 319 | };
|
---|
| 320 | FakeAsyncTestZoneSpec._removeTimer = function (timers, id) {
|
---|
| 321 | var index = timers.indexOf(id);
|
---|
| 322 | if (index > -1) {
|
---|
| 323 | timers.splice(index, 1);
|
---|
| 324 | }
|
---|
| 325 | };
|
---|
| 326 | FakeAsyncTestZoneSpec.prototype._dequeueTimer = function (id) {
|
---|
| 327 | var _this = this;
|
---|
| 328 | return function () {
|
---|
| 329 | FakeAsyncTestZoneSpec._removeTimer(_this.pendingTimers, id);
|
---|
| 330 | };
|
---|
| 331 | };
|
---|
| 332 | FakeAsyncTestZoneSpec.prototype._requeuePeriodicTimer = function (fn, interval, args, id) {
|
---|
| 333 | var _this = this;
|
---|
| 334 | return function () {
|
---|
| 335 | // Requeue the timer callback if it's not been canceled.
|
---|
| 336 | if (_this.pendingPeriodicTimers.indexOf(id) !== -1) {
|
---|
| 337 | _this._scheduler.scheduleFunction(fn, interval, { args: args, isPeriodic: true, id: id, isRequeuePeriodic: true });
|
---|
| 338 | }
|
---|
| 339 | };
|
---|
| 340 | };
|
---|
| 341 | FakeAsyncTestZoneSpec.prototype._dequeuePeriodicTimer = function (id) {
|
---|
| 342 | var _this = this;
|
---|
| 343 | return function () {
|
---|
| 344 | FakeAsyncTestZoneSpec._removeTimer(_this.pendingPeriodicTimers, id);
|
---|
| 345 | };
|
---|
| 346 | };
|
---|
| 347 | FakeAsyncTestZoneSpec.prototype._setTimeout = function (fn, delay, args, isTimer) {
|
---|
| 348 | if (isTimer === void 0) { isTimer = true; }
|
---|
| 349 | var removeTimerFn = this._dequeueTimer(Scheduler.nextId);
|
---|
| 350 | // Queue the callback and dequeue the timer on success and error.
|
---|
| 351 | var cb = this._fnAndFlush(fn, { onSuccess: removeTimerFn, onError: removeTimerFn });
|
---|
| 352 | var id = this._scheduler.scheduleFunction(cb, delay, { args: args, isRequestAnimationFrame: !isTimer });
|
---|
| 353 | if (isTimer) {
|
---|
| 354 | this.pendingTimers.push(id);
|
---|
| 355 | }
|
---|
| 356 | return id;
|
---|
| 357 | };
|
---|
| 358 | FakeAsyncTestZoneSpec.prototype._clearTimeout = function (id) {
|
---|
| 359 | FakeAsyncTestZoneSpec._removeTimer(this.pendingTimers, id);
|
---|
| 360 | this._scheduler.removeScheduledFunctionWithId(id);
|
---|
| 361 | };
|
---|
| 362 | FakeAsyncTestZoneSpec.prototype._setInterval = function (fn, interval, args) {
|
---|
| 363 | var id = Scheduler.nextId;
|
---|
| 364 | var completers = { onSuccess: null, onError: this._dequeuePeriodicTimer(id) };
|
---|
| 365 | var cb = this._fnAndFlush(fn, completers);
|
---|
| 366 | // Use the callback created above to requeue on success.
|
---|
| 367 | completers.onSuccess = this._requeuePeriodicTimer(cb, interval, args, id);
|
---|
| 368 | // Queue the callback and dequeue the periodic timer only on error.
|
---|
| 369 | this._scheduler.scheduleFunction(cb, interval, { args: args, isPeriodic: true });
|
---|
| 370 | this.pendingPeriodicTimers.push(id);
|
---|
| 371 | return id;
|
---|
| 372 | };
|
---|
| 373 | FakeAsyncTestZoneSpec.prototype._clearInterval = function (id) {
|
---|
| 374 | FakeAsyncTestZoneSpec._removeTimer(this.pendingPeriodicTimers, id);
|
---|
| 375 | this._scheduler.removeScheduledFunctionWithId(id);
|
---|
| 376 | };
|
---|
| 377 | FakeAsyncTestZoneSpec.prototype._resetLastErrorAndThrow = function () {
|
---|
| 378 | var error = this._lastError || this._uncaughtPromiseErrors[0];
|
---|
| 379 | this._uncaughtPromiseErrors.length = 0;
|
---|
| 380 | this._lastError = null;
|
---|
| 381 | throw error;
|
---|
| 382 | };
|
---|
| 383 | FakeAsyncTestZoneSpec.prototype.getCurrentTickTime = function () {
|
---|
| 384 | return this._scheduler.getCurrentTickTime();
|
---|
| 385 | };
|
---|
| 386 | FakeAsyncTestZoneSpec.prototype.getFakeSystemTime = function () {
|
---|
| 387 | return this._scheduler.getFakeSystemTime();
|
---|
| 388 | };
|
---|
| 389 | FakeAsyncTestZoneSpec.prototype.setFakeBaseSystemTime = function (realTime) {
|
---|
| 390 | this._scheduler.setFakeBaseSystemTime(realTime);
|
---|
| 391 | };
|
---|
| 392 | FakeAsyncTestZoneSpec.prototype.getRealSystemTime = function () {
|
---|
| 393 | return this._scheduler.getRealSystemTime();
|
---|
| 394 | };
|
---|
| 395 | FakeAsyncTestZoneSpec.patchDate = function () {
|
---|
| 396 | if (!!global[Zone.__symbol__('disableDatePatching')]) {
|
---|
| 397 | // we don't want to patch global Date
|
---|
| 398 | // because in some case, global Date
|
---|
| 399 | // is already being patched, we need to provide
|
---|
| 400 | // an option to let user still use their
|
---|
| 401 | // own version of Date.
|
---|
| 402 | return;
|
---|
| 403 | }
|
---|
| 404 | if (global['Date'] === FakeDate) {
|
---|
| 405 | // already patched
|
---|
| 406 | return;
|
---|
| 407 | }
|
---|
| 408 | global['Date'] = FakeDate;
|
---|
| 409 | FakeDate.prototype = OriginalDate.prototype;
|
---|
| 410 | // try check and reset timers
|
---|
| 411 | // because jasmine.clock().install() may
|
---|
| 412 | // have replaced the global timer
|
---|
| 413 | FakeAsyncTestZoneSpec.checkTimerPatch();
|
---|
| 414 | };
|
---|
| 415 | FakeAsyncTestZoneSpec.resetDate = function () {
|
---|
| 416 | if (global['Date'] === FakeDate) {
|
---|
| 417 | global['Date'] = OriginalDate;
|
---|
| 418 | }
|
---|
| 419 | };
|
---|
| 420 | FakeAsyncTestZoneSpec.checkTimerPatch = function () {
|
---|
| 421 | if (global.setTimeout !== timers.setTimeout) {
|
---|
| 422 | global.setTimeout = timers.setTimeout;
|
---|
| 423 | global.clearTimeout = timers.clearTimeout;
|
---|
| 424 | }
|
---|
| 425 | if (global.setInterval !== timers.setInterval) {
|
---|
| 426 | global.setInterval = timers.setInterval;
|
---|
| 427 | global.clearInterval = timers.clearInterval;
|
---|
| 428 | }
|
---|
| 429 | };
|
---|
| 430 | FakeAsyncTestZoneSpec.prototype.lockDatePatch = function () {
|
---|
| 431 | this.patchDateLocked = true;
|
---|
| 432 | FakeAsyncTestZoneSpec.patchDate();
|
---|
| 433 | };
|
---|
| 434 | FakeAsyncTestZoneSpec.prototype.unlockDatePatch = function () {
|
---|
| 435 | this.patchDateLocked = false;
|
---|
| 436 | FakeAsyncTestZoneSpec.resetDate();
|
---|
| 437 | };
|
---|
| 438 | FakeAsyncTestZoneSpec.prototype.tickToNext = function (steps, doTick, tickOptions) {
|
---|
| 439 | if (steps === void 0) { steps = 1; }
|
---|
| 440 | if (tickOptions === void 0) { tickOptions = { processNewMacroTasksSynchronously: true }; }
|
---|
| 441 | if (steps <= 0) {
|
---|
| 442 | return;
|
---|
| 443 | }
|
---|
| 444 | FakeAsyncTestZoneSpec.assertInZone();
|
---|
| 445 | this.flushMicrotasks();
|
---|
| 446 | this._scheduler.tickToNext(steps, doTick, tickOptions);
|
---|
| 447 | if (this._lastError !== null) {
|
---|
| 448 | this._resetLastErrorAndThrow();
|
---|
| 449 | }
|
---|
| 450 | };
|
---|
| 451 | FakeAsyncTestZoneSpec.prototype.tick = function (millis, doTick, tickOptions) {
|
---|
| 452 | if (millis === void 0) { millis = 0; }
|
---|
| 453 | if (tickOptions === void 0) { tickOptions = { processNewMacroTasksSynchronously: true }; }
|
---|
| 454 | FakeAsyncTestZoneSpec.assertInZone();
|
---|
| 455 | this.flushMicrotasks();
|
---|
| 456 | this._scheduler.tick(millis, doTick, tickOptions);
|
---|
| 457 | if (this._lastError !== null) {
|
---|
| 458 | this._resetLastErrorAndThrow();
|
---|
| 459 | }
|
---|
| 460 | };
|
---|
| 461 | FakeAsyncTestZoneSpec.prototype.flushMicrotasks = function () {
|
---|
| 462 | var _this = this;
|
---|
| 463 | FakeAsyncTestZoneSpec.assertInZone();
|
---|
| 464 | var flushErrors = function () {
|
---|
| 465 | if (_this._lastError !== null || _this._uncaughtPromiseErrors.length) {
|
---|
| 466 | // If there is an error stop processing the microtask queue and rethrow the error.
|
---|
| 467 | _this._resetLastErrorAndThrow();
|
---|
| 468 | }
|
---|
| 469 | };
|
---|
| 470 | while (this._microtasks.length > 0) {
|
---|
| 471 | var microtask = this._microtasks.shift();
|
---|
| 472 | microtask.func.apply(microtask.target, microtask.args);
|
---|
| 473 | }
|
---|
| 474 | flushErrors();
|
---|
| 475 | };
|
---|
| 476 | FakeAsyncTestZoneSpec.prototype.flush = function (limit, flushPeriodic, doTick) {
|
---|
| 477 | FakeAsyncTestZoneSpec.assertInZone();
|
---|
| 478 | this.flushMicrotasks();
|
---|
| 479 | var elapsed = this._scheduler.flush(limit, flushPeriodic, doTick);
|
---|
| 480 | if (this._lastError !== null) {
|
---|
| 481 | this._resetLastErrorAndThrow();
|
---|
| 482 | }
|
---|
| 483 | return elapsed;
|
---|
| 484 | };
|
---|
| 485 | FakeAsyncTestZoneSpec.prototype.flushOnlyPendingTimers = function (doTick) {
|
---|
| 486 | FakeAsyncTestZoneSpec.assertInZone();
|
---|
| 487 | this.flushMicrotasks();
|
---|
| 488 | var elapsed = this._scheduler.flushOnlyPendingTimers(doTick);
|
---|
| 489 | if (this._lastError !== null) {
|
---|
| 490 | this._resetLastErrorAndThrow();
|
---|
| 491 | }
|
---|
| 492 | return elapsed;
|
---|
| 493 | };
|
---|
| 494 | FakeAsyncTestZoneSpec.prototype.removeAllTimers = function () {
|
---|
| 495 | FakeAsyncTestZoneSpec.assertInZone();
|
---|
| 496 | this._scheduler.removeAll();
|
---|
| 497 | this.pendingPeriodicTimers = [];
|
---|
| 498 | this.pendingTimers = [];
|
---|
| 499 | };
|
---|
| 500 | FakeAsyncTestZoneSpec.prototype.getTimerCount = function () {
|
---|
| 501 | return this._scheduler.getTimerCount() + this._microtasks.length;
|
---|
| 502 | };
|
---|
| 503 | FakeAsyncTestZoneSpec.prototype.onScheduleTask = function (delegate, current, target, task) {
|
---|
| 504 | switch (task.type) {
|
---|
| 505 | case 'microTask':
|
---|
| 506 | var args = task.data && task.data.args;
|
---|
| 507 | // should pass additional arguments to callback if have any
|
---|
| 508 | // currently we know process.nextTick will have such additional
|
---|
| 509 | // arguments
|
---|
| 510 | var additionalArgs = void 0;
|
---|
| 511 | if (args) {
|
---|
| 512 | var callbackIndex = task.data.cbIdx;
|
---|
| 513 | if (typeof args.length === 'number' && args.length > callbackIndex + 1) {
|
---|
| 514 | additionalArgs = Array.prototype.slice.call(args, callbackIndex + 1);
|
---|
| 515 | }
|
---|
| 516 | }
|
---|
| 517 | this._microtasks.push({
|
---|
| 518 | func: task.invoke,
|
---|
| 519 | args: additionalArgs,
|
---|
| 520 | target: task.data && task.data.target
|
---|
| 521 | });
|
---|
| 522 | break;
|
---|
| 523 | case 'macroTask':
|
---|
| 524 | switch (task.source) {
|
---|
| 525 | case 'setTimeout':
|
---|
| 526 | task.data['handleId'] = this._setTimeout(task.invoke, task.data['delay'], Array.prototype.slice.call(task.data['args'], 2));
|
---|
| 527 | break;
|
---|
| 528 | case 'setImmediate':
|
---|
| 529 | task.data['handleId'] = this._setTimeout(task.invoke, 0, Array.prototype.slice.call(task.data['args'], 1));
|
---|
| 530 | break;
|
---|
| 531 | case 'setInterval':
|
---|
| 532 | task.data['handleId'] = this._setInterval(task.invoke, task.data['delay'], Array.prototype.slice.call(task.data['args'], 2));
|
---|
| 533 | break;
|
---|
| 534 | case 'XMLHttpRequest.send':
|
---|
| 535 | throw new Error('Cannot make XHRs from within a fake async test. Request URL: ' +
|
---|
| 536 | task.data['url']);
|
---|
| 537 | case 'requestAnimationFrame':
|
---|
| 538 | case 'webkitRequestAnimationFrame':
|
---|
| 539 | case 'mozRequestAnimationFrame':
|
---|
| 540 | // Simulate a requestAnimationFrame by using a setTimeout with 16 ms.
|
---|
| 541 | // (60 frames per second)
|
---|
| 542 | task.data['handleId'] = this._setTimeout(task.invoke, 16, task.data['args'], this.trackPendingRequestAnimationFrame);
|
---|
| 543 | break;
|
---|
| 544 | default:
|
---|
| 545 | // user can define which macroTask they want to support by passing
|
---|
| 546 | // macroTaskOptions
|
---|
| 547 | var macroTaskOption = this.findMacroTaskOption(task);
|
---|
| 548 | if (macroTaskOption) {
|
---|
| 549 | var args_1 = task.data && task.data['args'];
|
---|
| 550 | var delay = args_1 && args_1.length > 1 ? args_1[1] : 0;
|
---|
| 551 | var callbackArgs = macroTaskOption.callbackArgs ? macroTaskOption.callbackArgs : args_1;
|
---|
| 552 | if (!!macroTaskOption.isPeriodic) {
|
---|
| 553 | // periodic macroTask, use setInterval to simulate
|
---|
| 554 | task.data['handleId'] = this._setInterval(task.invoke, delay, callbackArgs);
|
---|
| 555 | task.data.isPeriodic = true;
|
---|
| 556 | }
|
---|
| 557 | else {
|
---|
| 558 | // not periodic, use setTimeout to simulate
|
---|
| 559 | task.data['handleId'] = this._setTimeout(task.invoke, delay, callbackArgs);
|
---|
| 560 | }
|
---|
| 561 | break;
|
---|
| 562 | }
|
---|
| 563 | throw new Error('Unknown macroTask scheduled in fake async test: ' + task.source);
|
---|
| 564 | }
|
---|
| 565 | break;
|
---|
| 566 | case 'eventTask':
|
---|
| 567 | task = delegate.scheduleTask(target, task);
|
---|
| 568 | break;
|
---|
| 569 | }
|
---|
| 570 | return task;
|
---|
| 571 | };
|
---|
| 572 | FakeAsyncTestZoneSpec.prototype.onCancelTask = function (delegate, current, target, task) {
|
---|
| 573 | switch (task.source) {
|
---|
| 574 | case 'setTimeout':
|
---|
| 575 | case 'requestAnimationFrame':
|
---|
| 576 | case 'webkitRequestAnimationFrame':
|
---|
| 577 | case 'mozRequestAnimationFrame':
|
---|
| 578 | return this._clearTimeout(task.data['handleId']);
|
---|
| 579 | case 'setInterval':
|
---|
| 580 | return this._clearInterval(task.data['handleId']);
|
---|
| 581 | default:
|
---|
| 582 | // user can define which macroTask they want to support by passing
|
---|
| 583 | // macroTaskOptions
|
---|
| 584 | var macroTaskOption = this.findMacroTaskOption(task);
|
---|
| 585 | if (macroTaskOption) {
|
---|
| 586 | var handleId = task.data['handleId'];
|
---|
| 587 | return macroTaskOption.isPeriodic ? this._clearInterval(handleId) :
|
---|
| 588 | this._clearTimeout(handleId);
|
---|
| 589 | }
|
---|
| 590 | return delegate.cancelTask(target, task);
|
---|
| 591 | }
|
---|
| 592 | };
|
---|
| 593 | FakeAsyncTestZoneSpec.prototype.onInvoke = function (delegate, current, target, callback, applyThis, applyArgs, source) {
|
---|
| 594 | try {
|
---|
| 595 | FakeAsyncTestZoneSpec.patchDate();
|
---|
| 596 | return delegate.invoke(target, callback, applyThis, applyArgs, source);
|
---|
| 597 | }
|
---|
| 598 | finally {
|
---|
| 599 | if (!this.patchDateLocked) {
|
---|
| 600 | FakeAsyncTestZoneSpec.resetDate();
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 | };
|
---|
| 604 | FakeAsyncTestZoneSpec.prototype.findMacroTaskOption = function (task) {
|
---|
| 605 | if (!this.macroTaskOptions) {
|
---|
| 606 | return null;
|
---|
| 607 | }
|
---|
| 608 | for (var i = 0; i < this.macroTaskOptions.length; i++) {
|
---|
| 609 | var macroTaskOption = this.macroTaskOptions[i];
|
---|
| 610 | if (macroTaskOption.source === task.source) {
|
---|
| 611 | return macroTaskOption;
|
---|
| 612 | }
|
---|
| 613 | }
|
---|
| 614 | return null;
|
---|
| 615 | };
|
---|
| 616 | FakeAsyncTestZoneSpec.prototype.onHandleError = function (parentZoneDelegate, currentZone, targetZone, error) {
|
---|
| 617 | this._lastError = error;
|
---|
| 618 | return false; // Don't propagate error to parent zone.
|
---|
| 619 | };
|
---|
| 620 | return FakeAsyncTestZoneSpec;
|
---|
| 621 | }());
|
---|
| 622 | // Export the class so that new instances can be created with proper
|
---|
| 623 | // constructor params.
|
---|
| 624 | Zone['FakeAsyncTestZoneSpec'] = FakeAsyncTestZoneSpec;
|
---|
| 625 | })(typeof window === 'object' && window || typeof self === 'object' && self || global);
|
---|
| 626 | Zone.__load_patch('fakeasync', function (global, Zone, api) {
|
---|
| 627 | var FakeAsyncTestZoneSpec = Zone && Zone['FakeAsyncTestZoneSpec'];
|
---|
| 628 | function getProxyZoneSpec() {
|
---|
| 629 | return Zone && Zone['ProxyZoneSpec'];
|
---|
| 630 | }
|
---|
| 631 | var _fakeAsyncTestZoneSpec = null;
|
---|
| 632 | /**
|
---|
| 633 | * Clears out the shared fake async zone for a test.
|
---|
| 634 | * To be called in a global `beforeEach`.
|
---|
| 635 | *
|
---|
| 636 | * @experimental
|
---|
| 637 | */
|
---|
| 638 | function resetFakeAsyncZone() {
|
---|
| 639 | if (_fakeAsyncTestZoneSpec) {
|
---|
| 640 | _fakeAsyncTestZoneSpec.unlockDatePatch();
|
---|
| 641 | }
|
---|
| 642 | _fakeAsyncTestZoneSpec = null;
|
---|
| 643 | // in node.js testing we may not have ProxyZoneSpec in which case there is nothing to reset.
|
---|
| 644 | getProxyZoneSpec() && getProxyZoneSpec().assertPresent().resetDelegate();
|
---|
| 645 | }
|
---|
| 646 | /**
|
---|
| 647 | * Wraps a function to be executed in the fakeAsync zone:
|
---|
| 648 | * - microtasks are manually executed by calling `flushMicrotasks()`,
|
---|
| 649 | * - timers are synchronous, `tick()` simulates the asynchronous passage of time.
|
---|
| 650 | *
|
---|
| 651 | * If there are any pending timers at the end of the function, an exception will be thrown.
|
---|
| 652 | *
|
---|
| 653 | * Can be used to wrap inject() calls.
|
---|
| 654 | *
|
---|
| 655 | * ## Example
|
---|
| 656 | *
|
---|
| 657 | * {@example core/testing/ts/fake_async.ts region='basic'}
|
---|
| 658 | *
|
---|
| 659 | * @param fn
|
---|
| 660 | * @returns The function wrapped to be executed in the fakeAsync zone
|
---|
| 661 | *
|
---|
| 662 | * @experimental
|
---|
| 663 | */
|
---|
| 664 | function fakeAsync(fn) {
|
---|
| 665 | // Not using an arrow function to preserve context passed from call site
|
---|
| 666 | var fakeAsyncFn = function () {
|
---|
| 667 | var args = [];
|
---|
| 668 | for (var _i = 0; _i < arguments.length; _i++) {
|
---|
| 669 | args[_i] = arguments[_i];
|
---|
| 670 | }
|
---|
| 671 | var ProxyZoneSpec = getProxyZoneSpec();
|
---|
| 672 | if (!ProxyZoneSpec) {
|
---|
| 673 | throw new Error('ProxyZoneSpec is needed for the async() test helper but could not be found. ' +
|
---|
| 674 | 'Please make sure that your environment includes zone.js/dist/proxy.js');
|
---|
| 675 | }
|
---|
| 676 | var proxyZoneSpec = ProxyZoneSpec.assertPresent();
|
---|
| 677 | if (Zone.current.get('FakeAsyncTestZoneSpec')) {
|
---|
| 678 | throw new Error('fakeAsync() calls can not be nested');
|
---|
| 679 | }
|
---|
| 680 | try {
|
---|
| 681 | // in case jasmine.clock init a fakeAsyncTestZoneSpec
|
---|
| 682 | if (!_fakeAsyncTestZoneSpec) {
|
---|
| 683 | if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
|
---|
| 684 | throw new Error('fakeAsync() calls can not be nested');
|
---|
| 685 | }
|
---|
| 686 | _fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
|
---|
| 687 | }
|
---|
| 688 | var res = void 0;
|
---|
| 689 | var lastProxyZoneSpec = proxyZoneSpec.getDelegate();
|
---|
| 690 | proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
|
---|
| 691 | _fakeAsyncTestZoneSpec.lockDatePatch();
|
---|
| 692 | try {
|
---|
| 693 | res = fn.apply(this, args);
|
---|
| 694 | flushMicrotasks();
|
---|
| 695 | }
|
---|
| 696 | finally {
|
---|
| 697 | proxyZoneSpec.setDelegate(lastProxyZoneSpec);
|
---|
| 698 | }
|
---|
| 699 | if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
|
---|
| 700 | throw new Error(_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length + " " +
|
---|
| 701 | "periodic timer(s) still in the queue.");
|
---|
| 702 | }
|
---|
| 703 | if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
|
---|
| 704 | throw new Error(_fakeAsyncTestZoneSpec.pendingTimers.length + " timer(s) still in the queue.");
|
---|
| 705 | }
|
---|
| 706 | return res;
|
---|
| 707 | }
|
---|
| 708 | finally {
|
---|
| 709 | resetFakeAsyncZone();
|
---|
| 710 | }
|
---|
| 711 | };
|
---|
| 712 | fakeAsyncFn.isFakeAsync = true;
|
---|
| 713 | return fakeAsyncFn;
|
---|
| 714 | }
|
---|
| 715 | function _getFakeAsyncZoneSpec() {
|
---|
| 716 | if (_fakeAsyncTestZoneSpec == null) {
|
---|
| 717 | _fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
---|
| 718 | if (_fakeAsyncTestZoneSpec == null) {
|
---|
| 719 | throw new Error('The code should be running in the fakeAsync zone to call this function');
|
---|
| 720 | }
|
---|
| 721 | }
|
---|
| 722 | return _fakeAsyncTestZoneSpec;
|
---|
| 723 | }
|
---|
| 724 | /**
|
---|
| 725 | * Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
|
---|
| 726 | *
|
---|
| 727 | * The microtasks queue is drained at the very start of this function and after any timer callback
|
---|
| 728 | * has been executed.
|
---|
| 729 | *
|
---|
| 730 | * ## Example
|
---|
| 731 | *
|
---|
| 732 | * {@example core/testing/ts/fake_async.ts region='basic'}
|
---|
| 733 | *
|
---|
| 734 | * @experimental
|
---|
| 735 | */
|
---|
| 736 | function tick(millis, ignoreNestedTimeout) {
|
---|
| 737 | if (millis === void 0) { millis = 0; }
|
---|
| 738 | if (ignoreNestedTimeout === void 0) { ignoreNestedTimeout = false; }
|
---|
| 739 | _getFakeAsyncZoneSpec().tick(millis, null, ignoreNestedTimeout);
|
---|
| 740 | }
|
---|
| 741 | /**
|
---|
| 742 | * Simulates the asynchronous passage of time for the timers in the fakeAsync zone by
|
---|
| 743 | * draining the macrotask queue until it is empty. The returned value is the milliseconds
|
---|
| 744 | * of time that would have been elapsed.
|
---|
| 745 | *
|
---|
| 746 | * @param maxTurns
|
---|
| 747 | * @returns The simulated time elapsed, in millis.
|
---|
| 748 | *
|
---|
| 749 | * @experimental
|
---|
| 750 | */
|
---|
| 751 | function flush(maxTurns) {
|
---|
| 752 | return _getFakeAsyncZoneSpec().flush(maxTurns);
|
---|
| 753 | }
|
---|
| 754 | /**
|
---|
| 755 | * Discard all remaining periodic tasks.
|
---|
| 756 | *
|
---|
| 757 | * @experimental
|
---|
| 758 | */
|
---|
| 759 | function discardPeriodicTasks() {
|
---|
| 760 | var zoneSpec = _getFakeAsyncZoneSpec();
|
---|
| 761 | var pendingTimers = zoneSpec.pendingPeriodicTimers;
|
---|
| 762 | zoneSpec.pendingPeriodicTimers.length = 0;
|
---|
| 763 | }
|
---|
| 764 | /**
|
---|
| 765 | * Flush any pending microtasks.
|
---|
| 766 | *
|
---|
| 767 | * @experimental
|
---|
| 768 | */
|
---|
| 769 | function flushMicrotasks() {
|
---|
| 770 | _getFakeAsyncZoneSpec().flushMicrotasks();
|
---|
| 771 | }
|
---|
| 772 | Zone[api.symbol('fakeAsyncTest')] =
|
---|
| 773 | { resetFakeAsyncZone: resetFakeAsyncZone, flushMicrotasks: flushMicrotasks, discardPeriodicTasks: discardPeriodicTasks, tick: tick, flush: flush, fakeAsync: fakeAsync };
|
---|
| 774 | }, true);
|
---|
| 775 | })));
|
---|