diff --git a/modules/@angular/core/test/fake_async_spec.ts b/modules/@angular/core/test/fake_async_spec.ts index 6d468e3ea6..f756fc55b1 100644 --- a/modules/@angular/core/test/fake_async_spec.ts +++ b/modules/@angular/core/test/fake_async_spec.ts @@ -13,6 +13,7 @@ import { flushMicrotasks, Log, tick, + discardPeriodicTasks, } from '@angular/core/testing'; import {TimerWrapper, PromiseWrapper} from '../../router/src/facade/async'; import {BaseException} from '../../router/src/facade/exceptions'; @@ -204,6 +205,24 @@ export function main() { expect(cycles).toEqual(1); })); + it('should clear periodic timers', fakeAsync(() => { + var cycles = 0; + var id = TimerWrapper.setInterval(() => { cycles++; }, 10); + + tick(10); + expect(cycles).toEqual(1); + + discardPeriodicTasks(); + + // Tick once to clear out the timer which already started. + tick(10); + expect(cycles).toEqual(2); + + tick(10); + // Nothing should change + expect(cycles).toEqual(2); + })); + it('should process microtasks before timers', fakeAsync(() => { var log = new Log(); @@ -259,6 +278,11 @@ export function main() { expect(() => { tick(); }) .toThrowError('The code should be running in the fakeAsync zone to call this function'); }); + + it('calling discardPeriodicTasks should throw', () => { + expect(() => { discardPeriodicTasks() ; }) + .toThrowError('The code should be running in the fakeAsync zone to call this function'); + }); }); }); } diff --git a/modules/@angular/core/testing/fake_async.ts b/modules/@angular/core/testing/fake_async.ts index bb0398ee52..5810258b88 100644 --- a/modules/@angular/core/testing/fake_async.ts +++ b/modules/@angular/core/testing/fake_async.ts @@ -80,6 +80,15 @@ export function tick(millis: number = 0): void { _getFakeAsyncZoneSpec().tick(millis); } +/** + * Discard all remaining periodic tasks. + */ +export function discardPeriodicTasks(): void { + let zoneSpec = _getFakeAsyncZoneSpec(); + let pendingTimers = zoneSpec.pendingPeriodicTimers; + zoneSpec.pendingPeriodicTimers.length = 0; +} + /** * Flush any pending microtasks. */ diff --git a/modules/@angular/integration_test/public_api_spec.ts b/modules/@angular/integration_test/public_api_spec.ts index de69b8d096..e04148813a 100644 --- a/modules/@angular/integration_test/public_api_spec.ts +++ b/modules/@angular/integration_test/public_api_spec.ts @@ -350,6 +350,7 @@ var CORE_TESTING: string[] = [ 'resetBaseTestProviders', 'setBaseTestProviders', 'tick', + 'discardPeriodicTasks', 'withProviders', 'xdescribe', 'xit'