|
19 | 19 | * found in the LICENSE file at https://angular.io/license
|
20 | 20 | */
|
21 | 21 | (function (global) {
|
22 |
| - var Scheduler = (function () { |
| 22 | + var OriginalDate = global.Date; |
| 23 | + var FakeDate = /** @class */ (function () { |
| 24 | + function FakeDate() { |
| 25 | + var d = new OriginalDate(); |
| 26 | + d.setTime(global.Date.now()); |
| 27 | + return d; |
| 28 | + } |
| 29 | + FakeDate.UTC = function () { |
| 30 | + return OriginalDate.UTC(); |
| 31 | + }; |
| 32 | + FakeDate.now = function () { |
| 33 | + var fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); |
| 34 | + if (fakeAsyncTestZoneSpec) { |
| 35 | + return fakeAsyncTestZoneSpec.getCurrentRealTime() + fakeAsyncTestZoneSpec.getCurrentTime(); |
| 36 | + } |
| 37 | + return OriginalDate.now.apply(this, arguments); |
| 38 | + }; |
| 39 | + FakeDate.parse = function () { |
| 40 | + return OriginalDate.parse(); |
| 41 | + }; |
| 42 | + return FakeDate; |
| 43 | + }()); |
| 44 | + var Scheduler = /** @class */ (function () { |
23 | 45 | function Scheduler() {
|
24 | 46 | // Next scheduler id.
|
25 |
| - this.nextId = 0; |
| 47 | + this.nextId = 1; |
26 | 48 | // Scheduler queue with the tuple of end time and callback function - sorted by end time.
|
27 | 49 | this._schedulerQueue = [];
|
28 | 50 | // Current simulated time in millis.
|
29 | 51 | this._currentTime = 0;
|
| 52 | + // Current real time in millis. |
| 53 | + this._currentRealTime = Date.now(); |
30 | 54 | }
|
| 55 | + Scheduler.prototype.getCurrentTime = function () { |
| 56 | + return this._currentTime; |
| 57 | + }; |
| 58 | + Scheduler.prototype.getCurrentRealTime = function () { |
| 59 | + return this._currentRealTime; |
| 60 | + }; |
| 61 | + Scheduler.prototype.setCurrentRealTime = function (realTime) { |
| 62 | + this._currentRealTime = realTime; |
| 63 | + }; |
31 | 64 | Scheduler.prototype.scheduleFunction = function (cb, delay, args, isPeriodic, isRequestAnimationFrame, id) {
|
32 | 65 | if (args === void 0) { args = []; }
|
33 | 66 | if (isPeriodic === void 0) { isPeriodic = false; }
|
|
148 | 181 | };
|
149 | 182 | return Scheduler;
|
150 | 183 | }());
|
151 |
| - var FakeAsyncTestZoneSpec = (function () { |
| 184 | + var FakeAsyncTestZoneSpec = /** @class */ (function () { |
152 | 185 | function FakeAsyncTestZoneSpec(namePrefix, trackPendingRequestAnimationFrame, macroTaskOptions) {
|
153 | 186 | if (trackPendingRequestAnimationFrame === void 0) { trackPendingRequestAnimationFrame = false; }
|
154 | 187 | this.trackPendingRequestAnimationFrame = trackPendingRequestAnimationFrame;
|
|
238 | 271 | FakeAsyncTestZoneSpec._removeTimer(this.pendingTimers, id);
|
239 | 272 | this._scheduler.removeScheduledFunctionWithId(id);
|
240 | 273 | };
|
241 |
| - FakeAsyncTestZoneSpec.prototype._setInterval = function (fn, interval) { |
242 |
| - var args = []; |
243 |
| - for (var _i = 2; _i < arguments.length; _i++) { |
244 |
| - args[_i - 2] = arguments[_i]; |
245 |
| - } |
| 274 | + FakeAsyncTestZoneSpec.prototype._setInterval = function (fn, interval, args) { |
246 | 275 | var id = this._scheduler.nextId;
|
247 | 276 | var completers = { onSuccess: null, onError: this._dequeuePeriodicTimer(id) };
|
248 | 277 | var cb = this._fnAndFlush(fn, completers);
|
|
263 | 292 | this._lastError = null;
|
264 | 293 | throw error;
|
265 | 294 | };
|
| 295 | + FakeAsyncTestZoneSpec.prototype.getCurrentTime = function () { |
| 296 | + return this._scheduler.getCurrentTime(); |
| 297 | + }; |
| 298 | + FakeAsyncTestZoneSpec.prototype.getCurrentRealTime = function () { |
| 299 | + return this._scheduler.getCurrentRealTime(); |
| 300 | + }; |
| 301 | + FakeAsyncTestZoneSpec.prototype.setCurrentRealTime = function (realTime) { |
| 302 | + this._scheduler.setCurrentRealTime(realTime); |
| 303 | + }; |
| 304 | + FakeAsyncTestZoneSpec.patchDate = function () { |
| 305 | + if (global['Date'] === FakeDate) { |
| 306 | + // already patched |
| 307 | + return; |
| 308 | + } |
| 309 | + global['Date'] = FakeDate; |
| 310 | + }; |
| 311 | + FakeAsyncTestZoneSpec.resetDate = function () { |
| 312 | + if (global['Date'] === FakeDate) { |
| 313 | + global['Date'] = OriginalDate; |
| 314 | + } |
| 315 | + }; |
266 | 316 | FakeAsyncTestZoneSpec.prototype.tick = function (millis, doTick) {
|
267 | 317 | if (millis === void 0) { millis = 0; }
|
268 | 318 | FakeAsyncTestZoneSpec.assertInZone();
|
|
319 | 369 | case 'macroTask':
|
320 | 370 | switch (task.source) {
|
321 | 371 | case 'setTimeout':
|
322 |
| - task.data['handleId'] = |
323 |
| - this._setTimeout(task.invoke, task.data['delay'], task.data['args']); |
| 372 | + task.data['handleId'] = this._setTimeout(task.invoke, task.data['delay'], Array.prototype.slice.call(task.data['args'], 2)); |
324 | 373 | break;
|
325 | 374 | case 'setInterval':
|
326 |
| - task.data['handleId'] = |
327 |
| - this._setInterval(task.invoke, task.data['delay'], task.data['args']); |
| 375 | + task.data['handleId'] = this._setInterval(task.invoke, task.data['delay'], Array.prototype.slice.call(task.data['args'], 2)); |
328 | 376 | break;
|
329 | 377 | case 'XMLHttpRequest.send':
|
330 | 378 | throw new Error('Cannot make XHRs from within a fake async test. Request URL: ' +
|
|
385 | 433 | return delegate.cancelTask(target, task);
|
386 | 434 | }
|
387 | 435 | };
|
| 436 | + FakeAsyncTestZoneSpec.prototype.onInvoke = function (delegate, current, target, callback, applyThis, applyArgs, source) { |
| 437 | + try { |
| 438 | + FakeAsyncTestZoneSpec.patchDate(); |
| 439 | + return delegate.invoke(target, callback, applyThis, applyArgs, source); |
| 440 | + } |
| 441 | + finally { |
| 442 | + FakeAsyncTestZoneSpec.resetDate(); |
| 443 | + } |
| 444 | + }; |
388 | 445 | FakeAsyncTestZoneSpec.prototype.findMacroTaskOption = function (task) {
|
389 | 446 | if (!this.macroTaskOptions) {
|
390 | 447 | return null;
|
|
0 commit comments