2016-04-28 20:50:03 -04:00
|
|
|
import {BaseException} from '../index';
|
2016-04-26 16:06:50 -04:00
|
|
|
import {getTestInjector} from './test_injector';
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
let _FakeAsyncTestZoneSpecType = Zone['FakeAsyncTestZoneSpec'];
|
2015-05-20 20:19:46 -04:00
|
|
|
|
2015-05-12 10:28:57 -04:00
|
|
|
/**
|
|
|
|
* Wraps a function to be executed in the fakeAsync zone:
|
|
|
|
* - microtasks are manually executed by calling `flushMicrotasks()`,
|
|
|
|
* - timers are synchronous, `tick()` simulates the asynchronous passage of time.
|
|
|
|
*
|
|
|
|
* If there are any pending timers at the end of the function, an exception will be thrown.
|
|
|
|
*
|
2016-04-18 19:04:35 -04:00
|
|
|
* Can be used to wrap inject() calls.
|
|
|
|
*
|
2015-12-03 18:49:09 -05:00
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* {@example testing/ts/fake_async.ts region='basic'}
|
|
|
|
*
|
2015-05-12 10:28:57 -04:00
|
|
|
* @param fn
|
|
|
|
* @returns {Function} The function wrapped to be executed in the fakeAsync zone
|
|
|
|
*/
|
2016-04-26 16:06:50 -04:00
|
|
|
export function fakeAsync(fn: Function): Function {
|
2016-04-18 19:04:35 -04:00
|
|
|
if (Zone.current.get('FakeAsyncTestZoneSpec') != null) {
|
|
|
|
throw new BaseException('fakeAsync() calls can not be nested');
|
2015-05-19 03:13:17 -04:00
|
|
|
}
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
let fakeAsyncTestZoneSpec = new _FakeAsyncTestZoneSpecType();
|
|
|
|
let fakeAsyncZone = Zone.current.fork(fakeAsyncTestZoneSpec);
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
return function(...args) {
|
2015-06-02 13:55:03 -04:00
|
|
|
let res = fakeAsyncZone.run(() => {
|
2016-04-26 16:06:50 -04:00
|
|
|
let res = fn(...args);
|
2015-06-02 13:55:03 -04:00
|
|
|
flushMicrotasks();
|
|
|
|
return res;
|
|
|
|
});
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
if (fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
|
|
|
|
throw new BaseException(`${fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` +
|
|
|
|
`periodic timer(s) still in the queue.`);
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:04:35 -04:00
|
|
|
if (fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
|
|
|
|
throw new BaseException(
|
|
|
|
`${fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`);
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|
|
|
|
return res;
|
2016-04-18 19:04:35 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getFakeAsyncZoneSpec(): any {
|
|
|
|
let zoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
|
|
|
if (zoneSpec == null) {
|
|
|
|
throw new Error('The code should be running in the fakeAsync zone to call this function');
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|
2016-04-18 19:04:35 -04:00
|
|
|
return zoneSpec;
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* Clear the queue of pending timers and microtasks.
|
2016-04-18 19:04:35 -04:00
|
|
|
* Tests no longer need to call this explicitly.
|
2015-12-03 18:49:09 -05:00
|
|
|
*
|
2016-04-18 19:04:35 -04:00
|
|
|
* @deprecated
|
2015-12-03 18:49:09 -05:00
|
|
|
*/
|
2015-09-03 17:45:25 -04:00
|
|
|
export function clearPendingTimers(): void {
|
2016-04-18 19:04:35 -04:00
|
|
|
// Do nothing.
|
2015-07-27 18:47:42 -04:00
|
|
|
}
|
|
|
|
|
2015-05-12 10:28:57 -04:00
|
|
|
/**
|
|
|
|
* Simulates the asynchronous passage of time for the timers in the fakeAsync zone.
|
|
|
|
*
|
2015-05-20 20:19:46 -04:00
|
|
|
* The microtasks queue is drained at the very start of this function and after any timer callback
|
|
|
|
* has been executed.
|
2015-05-12 10:28:57 -04:00
|
|
|
*
|
2015-12-03 18:49:09 -05:00
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* {@example testing/ts/fake_async.ts region='basic'}
|
|
|
|
*
|
2015-05-12 10:28:57 -04:00
|
|
|
* @param {number} millis Number of millisecond, defaults to 0
|
|
|
|
*/
|
|
|
|
export function tick(millis: number = 0): void {
|
2016-04-18 19:04:35 -04:00
|
|
|
_getFakeAsyncZoneSpec().tick(millis);
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush any pending microtasks.
|
|
|
|
*/
|
|
|
|
export function flushMicrotasks(): void {
|
2016-04-18 19:04:35 -04:00
|
|
|
_getFakeAsyncZoneSpec().flushMicrotasks();
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|