| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {BaseException} from '../index'; | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  | let _FakeAsyncTestZoneSpecType = (Zone as any /** TODO #9100 */)['FakeAsyncTestZoneSpec']; | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  * @returns {Function} 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-04-18 16:04:35 -07:00
										 |  |  |   if (Zone.current.get('FakeAsyncTestZoneSpec') != null) { | 
					
						
							|  |  |  |     throw new BaseException('fakeAsync() calls can not be nested'); | 
					
						
							| 
									
										
										
										
											2015-05-19 09:13:17 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |   let fakeAsyncTestZoneSpec = new _FakeAsyncTestZoneSpecType(); | 
					
						
							|  |  |  |   let fakeAsyncZone = Zone.current.fork(fakeAsyncTestZoneSpec); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |   return function(...args: any[] /** TODO #9100 */) { | 
					
						
							| 
									
										
										
										
											2015-06-02 19:55:03 +02:00
										 |  |  |     let res = fakeAsyncZone.run(() => { | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |       let res = fn(...args); | 
					
						
							| 
									
										
										
										
											2015-06-02 19:55:03 +02:00
										 |  |  |       flushMicrotasks(); | 
					
						
							|  |  |  |       return res; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |     if (fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       throw new BaseException( | 
					
						
							|  |  |  |           `${fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` + | 
					
						
							|  |  |  |           `periodic timer(s) still in the queue.`); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |     if (fakeAsyncTestZoneSpec.pendingTimers.length > 0) { | 
					
						
							|  |  |  |       throw new BaseException( | 
					
						
							|  |  |  |           `${fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`); | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07: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 16:28:57 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-18 16:04:35 -07:00
										 |  |  |   return zoneSpec; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							|  |  |  |   let zoneSpec = _getFakeAsyncZoneSpec(); | 
					
						
							|  |  |  |   let pendingTimers = zoneSpec.pendingPeriodicTimers; | 
					
						
							|  |  |  |   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
										 |  |  | } |