feat(core): update zone.js to 0.8.10 and expose the flush method (#16860)

`flush()` can now be used from within fakeAsync tests to simulate moving
time forward until all macrotask events have been cleared from the
event queue.
This commit is contained in:
Julie Ralph 2017-05-22 11:19:21 -07:00 committed by Chuck Jazdzewski
parent 6e41add867
commit 85d4c4b82e
6 changed files with 67 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "angular-srcs", "name": "angular-srcs",
"version": "4.2.0-beta.0", "version": "4.2.0-beta.1",
"dependencies": { "dependencies": {
"@types/angularjs": { "@types/angularjs": {
"version": "1.5.13-alpha", "version": "1.5.13-alpha",
@ -6877,7 +6877,7 @@
} }
}, },
"zone.js": { "zone.js": {
"version": "0.8.9" "version": "0.8.10"
} }
} }
} }

8
npm-shrinkwrap.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "angular-srcs", "name": "angular-srcs",
"version": "4.2.0-beta.0", "version": "4.2.0-beta.1",
"dependencies": { "dependencies": {
"@types/angularjs": { "@types/angularjs": {
"version": "1.5.13-alpha", "version": "1.5.13-alpha",
@ -10061,9 +10061,9 @@
} }
}, },
"zone.js": { "zone.js": {
"version": "0.8.9", "version": "0.8.10",
"from": "zone.js@latest", "from": "zone.js@0.8.10",
"resolved": "https://registry.npmjs.org/zone.js/-/zone.js-0.8.9.tgz" "resolved": "http://registry.npmjs.org/zone.js/-/zone.js-0.8.10.tgz"
} }
} }
} }

View File

@ -25,7 +25,7 @@
"reflect-metadata": "^0.1.3", "reflect-metadata": "^0.1.3",
"rxjs": "^5.0.1", "rxjs": "^5.0.1",
"typescript": "^2.3.2", "typescript": "^2.3.2",
"zone.js": "^0.8.9" "zone.js": "^0.8.10"
}, },
"optionalDependencies": { "optionalDependencies": {
"fsevents": "^1.0.14" "fsevents": "^1.0.14"

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {discardPeriodicTasks, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing'; import {discardPeriodicTasks, fakeAsync, flush, flushMicrotasks, tick} from '@angular/core/testing';
import {Log, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal'; import {Log, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal';
import {expect} from '@angular/platform-browser/testing/src/matchers'; import {expect} from '@angular/platform-browser/testing/src/matchers';
@ -261,6 +261,42 @@ export function main() {
'microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask'); 'microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
clearInterval(id); clearInterval(id);
})); }));
it('should flush tasks', fakeAsync(() => {
let ran = false;
setTimeout(() => { ran = true; }, 10);
flush();
expect(ran).toEqual(true);
}));
it('should flush multiple tasks', fakeAsync(() => {
let ran = false;
let ran2 = false;
setTimeout(() => { ran = true; }, 10);
setTimeout(() => { ran2 = true; }, 30);
let elapsed = flush();
expect(ran).toEqual(true);
expect(ran2).toEqual(true);
expect(elapsed).toEqual(30);
}));
it('should move periodic tasks', fakeAsync(() => {
let ran = false;
let count = 0;
setInterval(() => { count++; }, 10);
setTimeout(() => { ran = true; }, 35);
let elapsed = flush();
expect(count).toEqual(3);
expect(ran).toEqual(true);
expect(elapsed).toEqual(35);
discardPeriodicTasks();
}));
}); });
describe('outside of the fakeAsync zone', () => { describe('outside of the fakeAsync zone', () => {
@ -276,6 +312,12 @@ export function main() {
}).toThrowError('The code should be running in the fakeAsync zone to call this function'); }).toThrowError('The code should be running in the fakeAsync zone to call this function');
}); });
it('calling flush should throw', () => {
expect(() => {
flush();
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
});
it('calling discardPeriodicTasks should throw', () => { it('calling discardPeriodicTasks should throw', () => {
expect(() => { expect(() => {
discardPeriodicTasks(); discardPeriodicTasks();

View File

@ -115,6 +115,20 @@ export function tick(millis: number = 0): void {
_getFakeAsyncZoneSpec().tick(millis); _getFakeAsyncZoneSpec().tick(millis);
} }
/**
* Simulates the asynchronous passage of time for the timers in the fakeAsync zone by
* draining the macrotask queue until it is empty. The returned value is the milliseconds
* of time that would have been elapsed.
*
* @param maxTurns
* @returns {number} The simulated time elapsed, in millis.
*
* @experimental
*/
export function flush(maxTurns?: number): number {
return _getFakeAsyncZoneSpec().flush(maxTurns);
}
/** /**
* Discard all remaining periodic tasks. * Discard all remaining periodic tasks.
* *

View File

@ -32,6 +32,9 @@ export declare function discardPeriodicTasks(): void;
/** @experimental */ /** @experimental */
export declare function fakeAsync(fn: Function): (...args: any[]) => any; export declare function fakeAsync(fn: Function): (...args: any[]) => any;
/** @experimental */
export declare function flush(maxTurns?: number): number;
/** @experimental */ /** @experimental */
export declare function flushMicrotasks(): void; export declare function flushMicrotasks(): void;