Skip to content
Permalink
Browse files

fix(testing): add discardPeriodicTasks to be used with fakeAsync (#8629)

Closes #8616
  • Loading branch information...
juliemr authored and mhevery committed May 26, 2016
1 parent b2e804c commit 0cb93a436d39770243a6b84b6df67b101c5d83fe
@@ -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');
});
});
});
}
@@ -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.
*/
@@ -350,6 +350,7 @@ var CORE_TESTING: string[] = [
'resetBaseTestProviders',
'setBaseTestProviders',
'tick',
'discardPeriodicTasks',
'withProviders',
'xdescribe',
'xit'

0 comments on commit 0cb93a4

Please sign in to comment.
You can’t perform that action at this time.