2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2015-05-20 20:19:46 -04:00
|
|
|
|
2016-08-19 15:10:53 -04:00
|
|
|
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
|
|
|
|
type ProxyZoneSpec = {
|
|
|
|
setDelegate(delegateSpec: ZoneSpec): void; getDelegate(): ZoneSpec; resetDelegate(): void;
|
|
|
|
};
|
|
|
|
const ProxyZoneSpec: {get(): ProxyZoneSpec; assertPresent: () => ProxyZoneSpec} =
|
|
|
|
(Zone as any)['ProxyZoneSpec'];
|
|
|
|
|
2016-08-02 07:45:15 -04:00
|
|
|
let _fakeAsyncTestZoneSpec: any = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears out the shared fake async zone for a test.
|
|
|
|
* To be called in a global `beforeEach`.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export function resetFakeAsyncZone() {
|
|
|
|
_fakeAsyncTestZoneSpec = null;
|
2016-08-19 15:10:53 -04:00
|
|
|
ProxyZoneSpec.assertPresent().resetDelegate();
|
2016-08-02 07:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let _inFakeAsyncCall = false;
|
|
|
|
|
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-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-05-12 10:28:57 -04:00
|
|
|
*/
|
refactor(testing): remove wrapping of Jasmine functions (#9564)
Instead, the async function now determines whether it should return a promise
or instead call a done function parameter. Importing Jasmine functions
from `@angular/core/testing` is no longer necessary and is now deprecated.
Additionally, beforeEachProviders is also deprecated, as it is specific
to the testing framework. Instead, use the new addProviders method directly.
Before:
```js
import {beforeEachProviders, it, describe, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEachProviders(() => [MyService]);
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
After:
```js
import {addProviders, inject} from 'angular2/testing/core';
describe('my code', () => {
beforeEach(() => {
addProviders([MyService]);
});
it('does stuff', inject([MyService], (service) => {
// actual test
});
});
```
2016-06-24 20:48:35 -04:00
|
|
|
export function fakeAsync(fn: Function): (...args: any[]) => any {
|
2016-08-19 15:10:53 -04:00
|
|
|
return function(...args: any[]) {
|
|
|
|
const proxyZoneSpec = ProxyZoneSpec.assertPresent();
|
2016-08-02 07:45:15 -04:00
|
|
|
if (_inFakeAsyncCall) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error('fakeAsync() calls can not be nested');
|
2016-08-02 07:45:15 -04:00
|
|
|
}
|
|
|
|
_inFakeAsyncCall = true;
|
|
|
|
try {
|
2016-08-19 15:10:53 -04:00
|
|
|
if (!_fakeAsyncTestZoneSpec) {
|
|
|
|
if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error('fakeAsync() calls can not be nested');
|
2016-08-02 07:45:15 -04:00
|
|
|
}
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-08-19 15:10:53 -04:00
|
|
|
_fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
|
2016-08-02 07:45:15 -04:00
|
|
|
}
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-08-19 15:10:53 -04:00
|
|
|
let res: any;
|
|
|
|
const lastProxyZoneSpec = proxyZoneSpec.getDelegate();
|
|
|
|
proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
|
|
|
|
try {
|
|
|
|
res = fn(...args);
|
2016-08-02 07:45:15 -04:00
|
|
|
flushMicrotasks();
|
2016-08-19 15:10:53 -04:00
|
|
|
} finally {
|
|
|
|
proxyZoneSpec.setDelegate(lastProxyZoneSpec);
|
|
|
|
}
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-08-02 07:45:15 -04:00
|
|
|
if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(
|
2016-08-02 07:45:15 -04:00
|
|
|
`${_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` +
|
|
|
|
`periodic timer(s) still in the queue.`);
|
|
|
|
}
|
2015-05-12 10:28:57 -04:00
|
|
|
|
2016-08-02 07:45:15 -04:00
|
|
|
if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
|
2016-08-25 03:50:16 -04:00
|
|
|
throw new Error(
|
2016-08-02 07:45:15 -04:00
|
|
|
`${_fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
} finally {
|
|
|
|
_inFakeAsyncCall = false;
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|
2016-04-18 19:04:35 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getFakeAsyncZoneSpec(): any {
|
2016-08-02 07:45:15 -04:00
|
|
|
if (_fakeAsyncTestZoneSpec == null) {
|
2016-04-18 19:04:35 -04:00
|
|
|
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-08-02 07:45:15 -04:00
|
|
|
return _fakeAsyncTestZoneSpec;
|
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'}
|
|
|
|
*
|
2016-06-27 15:27:23 -04:00
|
|
|
* @experimental
|
2015-05-12 10:28:57 -04:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-26 13:19:30 -04:00
|
|
|
/**
|
|
|
|
* Discard all remaining periodic tasks.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2016-05-26 13:19:30 -04:00
|
|
|
*/
|
|
|
|
export function discardPeriodicTasks(): void {
|
|
|
|
let zoneSpec = _getFakeAsyncZoneSpec();
|
|
|
|
let pendingTimers = zoneSpec.pendingPeriodicTimers;
|
|
|
|
zoneSpec.pendingPeriodicTimers.length = 0;
|
|
|
|
}
|
|
|
|
|
2015-05-12 10:28:57 -04:00
|
|
|
/**
|
|
|
|
* Flush any pending microtasks.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-05-12 10:28:57 -04:00
|
|
|
*/
|
|
|
|
export function flushMicrotasks(): void {
|
2016-04-18 19:04:35 -04:00
|
|
|
_getFakeAsyncZoneSpec().flushMicrotasks();
|
2015-05-12 10:28:57 -04:00
|
|
|
}
|