| 
									
										
										
										
											2016-06-23 09:47:54 -07: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 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07: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 04:45:15 -07: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 12:10:53 -07:00
										 |  |  |   ProxyZoneSpec.assertPresent().resetDelegate(); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let _inFakeAsyncCall = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02: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 16:04:35 -07:00
										 |  |  |  * Can be used to wrap inject() calls. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |  * ## Example | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/fake_async.ts region='basic'} | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |  * @param fn | 
					
						
							| 
									
										
										
										
											2017-07-19 14:01:28 -07:00
										 |  |  |  * @returns The function wrapped to be executed in the fakeAsync zone | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @experimental | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02: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 17:48:35 -07:00
										 |  |  | export function fakeAsync(fn: Function): (...args: any[]) => any { | 
					
						
							| 
									
										
										
										
											2016-12-31 11:50:03 +01:00
										 |  |  |   // Not using an arrow function to preserve context passed from call site
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  |   return function(...args: any[]) { | 
					
						
							|  |  |  |     const proxyZoneSpec = ProxyZoneSpec.assertPresent(); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |     if (_inFakeAsyncCall) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |       throw new Error('fakeAsync() calls can not be nested'); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     _inFakeAsyncCall = true; | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  |       if (!_fakeAsyncTestZoneSpec) { | 
					
						
							|  |  |  |         if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |           throw new Error('fakeAsync() calls can not be nested'); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  |         _fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec(); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  |       let res: any; | 
					
						
							|  |  |  |       const lastProxyZoneSpec = proxyZoneSpec.getDelegate(); | 
					
						
							|  |  |  |       proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec); | 
					
						
							|  |  |  |       try { | 
					
						
							| 
									
										
										
										
											2016-12-31 11:50:03 +01:00
										 |  |  |         res = fn.apply(this, args); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |         flushMicrotasks(); | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  |       } finally { | 
					
						
							|  |  |  |         proxyZoneSpec.setDelegate(lastProxyZoneSpec); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |       if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |         throw new Error( | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |             `${_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` + | 
					
						
							|  |  |  |             `periodic timer(s) still in the queue.`); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |       if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |         throw new Error( | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |             `${_fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return res; | 
					
						
							|  |  |  |     } finally { | 
					
						
							|  |  |  |       _inFakeAsyncCall = false; | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  |       resetFakeAsyncZone(); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function _getFakeAsyncZoneSpec(): any { | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |   if (_fakeAsyncTestZoneSpec == null) { | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |     throw new Error('The code should be running in the fakeAsync zone to call this function'); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |   return _fakeAsyncTestZoneSpec; | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Simulates the asynchronous passage of time for the timers in the fakeAsync zone. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |  * The microtasks queue is drained at the very start of this function and after any timer callback | 
					
						
							|  |  |  |  * has been executed. | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |  * ## Example | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * {@example testing/ts/fake_async.ts region='basic'} | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * @experimental | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | export function tick(millis: number = 0): void { | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |   _getFakeAsyncZoneSpec().tick(millis); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 11:19:21 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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 | 
					
						
							| 
									
										
										
										
											2017-07-19 14:01:28 -07:00
										 |  |  |  * @returns The simulated time elapsed, in millis. | 
					
						
							| 
									
										
										
										
											2017-05-22 11:19:21 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @experimental | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export function flush(maxTurns?: number): number { | 
					
						
							|  |  |  |   return _getFakeAsyncZoneSpec().flush(maxTurns); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Discard all remaining periodic tasks. | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @experimental | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | export function discardPeriodicTasks(): void { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   const zoneSpec = _getFakeAsyncZoneSpec(); | 
					
						
							|  |  |  |   const pendingTimers = zoneSpec.pendingPeriodicTimers; | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  |   zoneSpec.pendingPeriodicTimers.length = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Flush any pending microtasks. | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @experimental | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | export function flushMicrotasks(): void { | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |   _getFakeAsyncZoneSpec().flushMicrotasks(); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | } |