| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  | import {discardPeriodicTasksFallback, fakeAsyncFallback, flushFallback, flushMicrotasksFallback, resetFakeAsyncZoneFallback, tickFallback} from './fake_async_fallback'; | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-03 20:34:30 -08:00
										 |  |  | const _Zone: any = typeof Zone !== 'undefined' ? Zone : null; | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  | const fakeAsyncTestModule = _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')]; | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Clears out the shared fake async zone for a test. | 
					
						
							|  |  |  |  * To be called in a global `beforeEach`. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-10-19 12:12:20 +01:00
										 |  |  |  * @publicApi | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  | export function resetFakeAsyncZone(): void { | 
					
						
							|  |  |  |   if (fakeAsyncTestModule) { | 
					
						
							|  |  |  |     return fakeAsyncTestModule.resetFakeAsyncZone(); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return resetFakeAsyncZoneFallback(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -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. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-05-18 16:13:00 +01:00
										 |  |  |  * @usageNotes | 
					
						
							|  |  |  |  * ### Example | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-11-22 14:06:17 +02:00
										 |  |  |  * {@example core/testing/ts/fake_async.ts region='basic'} | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-10-19 12:12:20 +01:00
										 |  |  |  * @publicApi | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  |   if (fakeAsyncTestModule) { | 
					
						
							|  |  |  |     return fakeAsyncTestModule.fakeAsync(fn); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return fakeAsyncFallback(fn); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-05-18 16:13:00 +01:00
										 |  |  |  * @usageNotes | 
					
						
							|  |  |  |  * ### Example | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-11-22 14:06:17 +02:00
										 |  |  |  * {@example core/testing/ts/fake_async.ts region='basic'} | 
					
						
							| 
									
										
										
										
											2015-12-03 15:49:09 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-10-19 12:12:20 +01:00
										 |  |  |  * @publicApi | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | export function tick(millis: number = 0): void { | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  |   if (fakeAsyncTestModule) { | 
					
						
							|  |  |  |     return fakeAsyncTestModule.tick(millis); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return tickFallback(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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-10-19 12:12:20 +01:00
										 |  |  |  * @publicApi | 
					
						
							| 
									
										
										
										
											2017-05-22 11:19:21 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | export function flush(maxTurns?: number): number { | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  |   if (fakeAsyncTestModule) { | 
					
						
							|  |  |  |     return fakeAsyncTestModule.flush(maxTurns); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return flushFallback(maxTurns); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-05-22 11:19:21 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Discard all remaining periodic tasks. | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-10-19 12:12:20 +01:00
										 |  |  |  * @publicApi | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | export function discardPeriodicTasks(): void { | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  |   if (fakeAsyncTestModule) { | 
					
						
							|  |  |  |     return fakeAsyncTestModule.discardPeriodicTasks(); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     discardPeriodicTasksFallback(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Flush any pending microtasks. | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-10-19 12:12:20 +01:00
										 |  |  |  * @publicApi | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | export function flushMicrotasks(): void { | 
					
						
							| 
									
										
										
										
											2018-04-01 00:44:38 +09:00
										 |  |  |   if (fakeAsyncTestModule) { | 
					
						
							|  |  |  |     return fakeAsyncTestModule.flushMicrotasks(); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return flushMicrotasksFallback(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-05-12 16:28:57 +02:00
										 |  |  | } |