From 85d4c4b82e3c7d8409b8346c10f6aa8190ebbbe8 Mon Sep 17 00:00:00 2001 From: Julie Ralph Date: Mon, 22 May 2017 11:19:21 -0700 Subject: [PATCH] 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. --- npm-shrinkwrap.clean.json | 4 +-- npm-shrinkwrap.json | 8 ++--- package.json | 2 +- packages/core/test/fake_async_spec.ts | 44 +++++++++++++++++++++++- packages/core/testing/src/fake_async.ts | 14 ++++++++ tools/public_api_guard/core/testing.d.ts | 3 ++ 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/npm-shrinkwrap.clean.json b/npm-shrinkwrap.clean.json index aea634bf5d..0a2d441d36 100644 --- a/npm-shrinkwrap.clean.json +++ b/npm-shrinkwrap.clean.json @@ -1,6 +1,6 @@ { "name": "angular-srcs", - "version": "4.2.0-beta.0", + "version": "4.2.0-beta.1", "dependencies": { "@types/angularjs": { "version": "1.5.13-alpha", @@ -6877,7 +6877,7 @@ } }, "zone.js": { - "version": "0.8.9" + "version": "0.8.10" } } } diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 2039949d46..51b160d554 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "angular-srcs", - "version": "4.2.0-beta.0", + "version": "4.2.0-beta.1", "dependencies": { "@types/angularjs": { "version": "1.5.13-alpha", @@ -10061,9 +10061,9 @@ } }, "zone.js": { - "version": "0.8.9", - "from": "zone.js@latest", - "resolved": "https://registry.npmjs.org/zone.js/-/zone.js-0.8.9.tgz" + "version": "0.8.10", + "from": "zone.js@0.8.10", + "resolved": "http://registry.npmjs.org/zone.js/-/zone.js-0.8.10.tgz" } } } diff --git a/package.json b/package.json index 7b94a70f99..40743b564f 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "reflect-metadata": "^0.1.3", "rxjs": "^5.0.1", "typescript": "^2.3.2", - "zone.js": "^0.8.9" + "zone.js": "^0.8.10" }, "optionalDependencies": { "fsevents": "^1.0.14" diff --git a/packages/core/test/fake_async_spec.ts b/packages/core/test/fake_async_spec.ts index 7dcce2de59..1fdc6fb925 100644 --- a/packages/core/test/fake_async_spec.ts +++ b/packages/core/test/fake_async_spec.ts @@ -6,7 +6,7 @@ * 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 {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'); 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', () => { @@ -276,6 +312,12 @@ export function main() { }).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', () => { expect(() => { discardPeriodicTasks(); diff --git a/packages/core/testing/src/fake_async.ts b/packages/core/testing/src/fake_async.ts index d87ae709c3..bd5d7f780a 100644 --- a/packages/core/testing/src/fake_async.ts +++ b/packages/core/testing/src/fake_async.ts @@ -115,6 +115,20 @@ export function tick(millis: number = 0): void { _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. * diff --git a/tools/public_api_guard/core/testing.d.ts b/tools/public_api_guard/core/testing.d.ts index 39044a1646..5f9acd7c7c 100644 --- a/tools/public_api_guard/core/testing.d.ts +++ b/tools/public_api_guard/core/testing.d.ts @@ -32,6 +32,9 @@ export declare function discardPeriodicTasks(): void; /** @experimental */ export declare function fakeAsync(fn: Function): (...args: any[]) => any; +/** @experimental */ +export declare function flush(maxTurns?: number): number; + /** @experimental */ export declare function flushMicrotasks(): void;