Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix: run all timers in passage of time in a single fakeAsync's tick call #454

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion dist/zone-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,15 @@ var Zone$1 = (function (global) {
if (NativePromise) {
patchThen(NativePromise);
if (typeof global['fetch'] !== 'undefined') {
var fetchPromise = global['fetch']('about:blank');
var fetchPromise = void 0;
try {
// In MS Edge this throws
fetchPromise = global['fetch']();
}
catch (e) {
// In Chrome this throws instead.
fetchPromise = global['fetch']('about:blank');
}
// ignore output to prevent error;
fetchPromise.then(function () { return null; }, function () { return null; });
if (fetchPromise.constructor != NativePromise) {
Expand Down
10 changes: 9 additions & 1 deletion dist/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,15 @@ var Zone$1 = (function (global) {
if (NativePromise) {
patchThen(NativePromise);
if (typeof global['fetch'] !== 'undefined') {
var fetchPromise = global['fetch']('about:blank');
var fetchPromise = void 0;
try {
// In MS Edge this throws
fetchPromise = global['fetch']();
}
catch (e) {
// In Chrome this throws instead.
fetchPromise = global['fetch']('about:blank');
}
// ignore output to prevent error;
fetchPromise.then(function () { return null; }, function () { return null; });
if (fetchPromise.constructor != NativePromise) {
Expand Down
2 changes: 1 addition & 1 deletion dist/zone.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,24 @@
}

tick(millis: number = 0): void {
this._currentTime += millis;
while (this._schedulerQueue.length > 0) {
let finalTime = this._currentTime + millis;
while (this._schedulerQueue.length > 0) {
let current = this._schedulerQueue[0];
if (this._currentTime < current.endTime) {
if (finalTime < current.endTime) {
// Done processing the queue since it's sorted by endTime.
break;
} else {
// Time to run scheduled function. Remove it from the head of queue.
let current = this._schedulerQueue.shift();
this._currentTime = current.endTime;
let retval = current.func.apply(global, current.args);
if (!retval) {
// Uncaught exception in the current scheduled function. Stop processing the queue.
break;
}
}
}
this._currentTime = finalTime;
}
}

Expand Down
9 changes: 8 additions & 1 deletion lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,14 @@ const Zone: ZoneType = (function(global: any) {
if (NativePromise) {
patchThen(NativePromise);
if (typeof global['fetch'] !== 'undefined') {
const fetchPromise = global['fetch']('about:blank');
let fetchPromise: Promise<any>;
try {
// In MS Edge this throws
fetchPromise = global['fetch']();
} catch (e) {
// In Chrome this throws instead.
fetchPromise = global['fetch']('about:blank');
}
// ignore output to prevent error;
fetchPromise.then(() => null, () => null);
if (fetchPromise.constructor != NativePromise) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zone.js",
"version": "0.6.22",
"version": "0.6.23",
"description": "Zones for JavaScript",
"main": "dist/zone-node.js",
"browser": "dist/zone.js",
Expand Down
4 changes: 2 additions & 2 deletions sauce.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ module.exports = function (config) {
platform: 'Windows 10',
version: '11'
},
/*'SL_MSEDGE13': {
'SL_MSEDGE13': {
base: 'SauceLabs',
browserName: 'MicrosoftEdge',
platform: 'Windows 10',
version: '13.10586'
},*/
},
'SL_ANDROID4.1': {
base: 'SauceLabs',
browserName: 'android',
Expand Down
26 changes: 26 additions & 0 deletions test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ describe('FakeAsyncTestZoneSpec', () => {
});
});

it('should run queued timer created by timer callback', () => {
fakeAsyncTestZone.run(() => {
let counter = 0;
const startCounterLoop = () => {
counter++;
setTimeout(startCounterLoop, 10);
}

startCounterLoop();

expect(counter).toEqual(1);

testZoneSpec.tick(10);
expect(counter).toEqual(2);

testZoneSpec.tick(10);
expect(counter).toEqual(3);

testZoneSpec.tick(30);
expect(counter).toEqual(6);
});
});

it('should run queued timer only once', () => {
fakeAsyncTestZone.run(() => {
let cycles = 0;
Expand Down Expand Up @@ -160,6 +183,9 @@ describe('FakeAsyncTestZoneSpec', () => {

testZoneSpec.tick(10);
expect(cycles).toEqual(3);

testZoneSpec.tick(30);
expect(cycles).toEqual(6);
});
});

Expand Down