| 
									
										
										
										
											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-06-08 16:38:52 -07:00
										 |  |  | import {AsyncTestCompleter} from './async_test_completer'; | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  | import {StringMapWrapper} from './facade/collection'; | 
					
						
							| 
									
										
										
										
											2016-09-18 15:39:26 -07:00
										 |  |  | import {global} from './facade/lang'; | 
					
						
							| 
									
										
										
										
											2016-09-18 15:55:08 -07:00
										 |  |  | import {isPromise} from './private_import_core'; | 
					
						
							| 
									
										
										
										
											2016-09-18 15:39:26 -07:00
										 |  |  | import {getTestBed, inject} from './test_bed'; | 
					
						
							| 
									
										
										
										
											2015-10-08 15:33:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | export {AsyncTestCompleter} from './async_test_completer'; | 
					
						
							| 
									
										
										
										
											2016-07-11 10:55:47 -07:00
										 |  |  | export {MockAnimationPlayer} from './mock_animation_player'; | 
					
						
							| 
									
										
										
										
											2016-07-20 10:51:21 -07:00
										 |  |  | export {inject} from './test_bed'; | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 17:10:22 -07:00
										 |  |  | export * from './logger'; | 
					
						
							|  |  |  | export * from './ng_zone_mock'; | 
					
						
							| 
									
										
										
										
											2016-05-31 15:15:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 14:55:58 -08:00
										 |  |  | export const proxy: ClassDecorator = (t: any) => t; | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  | const _global = <any>(typeof window === 'undefined' ? global : window); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-27 14:55:58 -08:00
										 |  |  | export const afterEach: Function = _global.afterEach; | 
					
						
							|  |  |  | export const expect: (actual: any) => jasmine.Matchers = _global.expect; | 
					
						
							| 
									
										
										
										
											2015-05-21 16:30:07 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  | const jsmBeforeEach = _global.beforeEach; | 
					
						
							|  |  |  | const jsmDescribe = _global.describe; | 
					
						
							|  |  |  | const jsmDDescribe = _global.fdescribe; | 
					
						
							|  |  |  | const jsmXDescribe = _global.xdescribe; | 
					
						
							|  |  |  | const jsmIt = _global.it; | 
					
						
							|  |  |  | const jsmIIt = _global.fit; | 
					
						
							|  |  |  | const jsmXIt = _global.xit; | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  | const runnerStack: BeforeEachRunner[] = []; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | jasmine.DEFAULT_TIMEOUT_INTERVAL = 3000; | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  | const globalTimeOut = jasmine.DEFAULT_TIMEOUT_INTERVAL; | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  | const testBed = getTestBed(); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-28 16:23:28 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Mechanism to run `beforeEach()` functions of Angular tests. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |  * Note: Jasmine own `beforeEach` is used by this library to handle DI providers. | 
					
						
							| 
									
										
										
										
											2015-08-28 16:23:28 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | class BeforeEachRunner { | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |   private _fns: Array<Function> = []; | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-28 16:23:28 -07:00
										 |  |  |   constructor(private _parent: BeforeEachRunner) {} | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |   beforeEach(fn: Function): void { this._fns.push(fn); } | 
					
						
							| 
									
										
										
										
											2015-08-28 16:23:28 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 19:03:21 -08:00
										 |  |  |   run(): void { | 
					
						
							|  |  |  |     if (this._parent) this._parent.run(); | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |     this._fns.forEach((fn) => { fn(); }); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  | // Reset the test providers before each test
 | 
					
						
							| 
									
										
										
										
											2016-07-28 04:54:49 -07:00
										 |  |  | jsmBeforeEach(() => { testBed.resetTestingModule(); }); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  | function _describe(jsmFn: Function, ...args: any[]) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   const parentRunner = runnerStack.length === 0 ? null : runnerStack[runnerStack.length - 1]; | 
					
						
							|  |  |  |   const runner = new BeforeEachRunner(parentRunner); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   runnerStack.push(runner); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   const suite = jsmFn(...args); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   runnerStack.pop(); | 
					
						
							|  |  |  |   return suite; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  | export function describe(...args: any[]): void { | 
					
						
							| 
									
										
										
										
											2015-06-02 09:51:40 -07:00
										 |  |  |   return _describe(jsmDescribe, ...args); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  | export function ddescribe(...args: any[]): void { | 
					
						
							| 
									
										
										
										
											2015-06-02 09:51:40 -07:00
										 |  |  |   return _describe(jsmDDescribe, ...args); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  | export function xdescribe(...args: any[]): void { | 
					
						
							| 
									
										
										
										
											2015-06-02 09:51:40 -07:00
										 |  |  |   return _describe(jsmXDescribe, ...args); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | export function beforeEach(fn: Function): void { | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   if (runnerStack.length > 0) { | 
					
						
							|  |  |  |     // Inside a describe block, beforeEach() uses a BeforeEachRunner
 | 
					
						
							| 
									
										
										
										
											2015-08-28 16:23:28 -07:00
										 |  |  |     runnerStack[runnerStack.length - 1].beforeEach(fn); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     // Top level beforeEach() are delegated to jasmine
 | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |     jsmBeforeEach(fn); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |  * Allows overriding default providers defined in test_injector.js. | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |  * The given function must return a list of DI providers. | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Example: | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-11-11 14:28:23 +01:00
										 |  |  |  *   beforeEachProviders(() => [ | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |  *     {provide: Compiler, useClass: MockCompiler}, | 
					
						
							|  |  |  |  *     {provide: SomeToken, useValue: myValue}, | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |  *   ]); | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  | export function beforeEachProviders(fn: Function): void { | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   jsmBeforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const providers = fn(); | 
					
						
							| 
									
										
										
										
											2015-11-11 14:28:23 +01:00
										 |  |  |     if (!providers) return; | 
					
						
							| 
									
										
										
										
											2016-07-28 04:54:49 -07:00
										 |  |  |     testBed.configureTestingModule({providers: providers}); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  | function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: number): void { | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |   if (runnerStack.length == 0) { | 
					
						
							|  |  |  |     // This left here intentionally, as we should never get here, and it aids debugging.
 | 
					
						
							|  |  |  |     debugger; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     throw new Error('Empty Stack!'); | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |   const runner = runnerStack[runnerStack.length - 1]; | 
					
						
							|  |  |  |   const timeOut = Math.max(globalTimeOut, testTimeOut); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |   jsmFn(name, (done: any) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const completerProvider = { | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |       provide: AsyncTestCompleter, | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |       useFactory: () => { | 
					
						
							|  |  |  |         // Mark the test as async when an AsyncTestCompleter is injected in an it()
 | 
					
						
							|  |  |  |         return new AsyncTestCompleter(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2016-07-28 04:54:49 -07:00
										 |  |  |     testBed.configureTestingModule({providers: [completerProvider]}); | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |     runner.run(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (testFn.length == 0) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const retVal = testFn(); | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |       if (isPromise(retVal)) { | 
					
						
							|  |  |  |         // Asynchronous test function that returns a Promise - wait for completion.
 | 
					
						
							|  |  |  |         (<Promise<any>>retVal).then(done, done.fail); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         // Synchronous test function - complete immediately.
 | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-08-28 16:23:28 -07:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |       // Asynchronous test function that takes in 'done' parameter.
 | 
					
						
							|  |  |  |       testFn(done); | 
					
						
							| 
									
										
										
										
											2015-08-28 16:23:28 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-26 13:06:50 -07:00
										 |  |  |   }, timeOut); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  | export function it(name: any, fn: any, timeOut: any = null): void { | 
					
						
							| 
									
										
										
										
											2015-06-02 16:29:09 +02:00
										 |  |  |   return _it(jsmIt, name, fn, timeOut); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  | export function xit(name: any, fn: any, timeOut: any = null): void { | 
					
						
							| 
									
										
										
										
											2015-06-02 16:29:09 +02:00
										 |  |  |   return _it(jsmXIt, name, fn, timeOut); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  | export function iit(name: any, fn: any, timeOut: any = null): void { | 
					
						
							| 
									
										
										
										
											2015-06-02 16:29:09 +02:00
										 |  |  |   return _it(jsmIIt, name, fn, timeOut); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class SpyObject { | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |   constructor(type?: any) { | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |     if (type) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       for (const prop in type.prototype) { | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |         let m: any = null; | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |         try { | 
					
						
							|  |  |  |           m = type.prototype[prop]; | 
					
						
							|  |  |  |         } catch (e) { | 
					
						
							|  |  |  |           // As we are creating spys for abstract classes,
 | 
					
						
							|  |  |  |           // these classes might have getters that throw when they are accessed.
 | 
					
						
							|  |  |  |           // As we are only auto creating spys for methods, this
 | 
					
						
							|  |  |  |           // should not matter.
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (typeof m === 'function') { | 
					
						
							| 
									
										
										
										
											2015-06-18 15:40:12 -07:00
										 |  |  |           this.spy(prop); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |   spy(name: string) { | 
					
						
							|  |  |  |     if (!(this as any)[name]) { | 
					
						
							|  |  |  |       (this as any)[name] = jasmine.createSpy(name); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |     return (this as any)[name]; | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |   prop(name: string, value: any) { (this as any)[name] = value; } | 
					
						
							| 
									
										
										
										
											2015-08-26 11:41:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |   static stub(object: any = null, config: any = null, overrides: any = null) { | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |     if (!(object instanceof SpyObject)) { | 
					
						
							|  |  |  |       overrides = config; | 
					
						
							|  |  |  |       config = object; | 
					
						
							|  |  |  |       object = new SpyObject(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const m = StringMapWrapper.merge(config, overrides); | 
					
						
							| 
									
										
										
										
											2016-10-11 15:44:48 -07:00
										 |  |  |     Object.keys(m).forEach(key => { object.spy(key).and.returnValue(m[key]); }); | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |     return object; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |