| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  | // Based on https://github.com/angular/angular/blob/master/modules/angular2/test/testing/testing_public_spec.ts
 | 
					
						
							|  |  |  | /* tslint:disable:no-unused-variable */ | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Tests that show what goes wrong when the tests are incorrectly written or have a problem | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2016-06-03 18:15:14 -07:00
										 |  |  |   BadTemplateUrlComp, ButtonComp, | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |   ChildChildComp, ChildComp, ChildWithChildComp, | 
					
						
							|  |  |  |   ExternalTemplateComp, | 
					
						
							|  |  |  |   FancyService, MockFancyService, | 
					
						
							|  |  |  |   InputComp, | 
					
						
							|  |  |  |   MyIfComp, MyIfChildComp, MyIfParentComp, | 
					
						
							|  |  |  |   MockChildComp, MockChildChildComp, | 
					
						
							|  |  |  |   ParentComp, | 
					
						
							|  |  |  |   TestProvidersComp, TestViewProvidersComp | 
					
						
							|  |  |  | } from './bag'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | import { DebugElement } from '@angular/core'; | 
					
						
							|  |  |  | import { By }           from '@angular/platform-browser'; | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2016-07-06 18:19:10 +02:00
										 |  |  |   addProviders, | 
					
						
							| 
									
										
										
										
											2016-05-20 01:09:45 -07:00
										 |  |  |   async, inject | 
					
						
							|  |  |  | } from '@angular/core/testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 18:19:10 +02:00
										 |  |  | import { ComponentFixture, TestComponentBuilder } from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | import { ViewMetadata }   from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  | import { Observable }     from 'rxjs/Rx'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ////////  SPECS  /////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | xdescribe('async & inject testing errors', () => { | 
					
						
							|  |  |  |   let originalJasmineIt: any; | 
					
						
							|  |  |  |   let originalJasmineBeforeEach: any; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let patchJasmineIt = () => { | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |     return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |       originalJasmineIt = jasmine.getEnv().it; | 
					
						
							|  |  |  |       jasmine.getEnv().it = (description: string, fn: Function): jasmine.Spec => { | 
					
						
							|  |  |  |         let done = () => { resolve(); }; | 
					
						
							|  |  |  |         (<any>done).fail = (err: any) => { reject(err); }; | 
					
						
							|  |  |  |         fn(done); | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let restoreJasmineIt = () => { jasmine.getEnv().it = originalJasmineIt; }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let patchJasmineBeforeEach = () => { | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |     return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |       originalJasmineBeforeEach = jasmine.getEnv().beforeEach; | 
					
						
							|  |  |  |       jasmine.getEnv().beforeEach = (fn: any): void => { | 
					
						
							|  |  |  |         let done = () => { resolve(); }; | 
					
						
							|  |  |  |         (<any>done).fail = (err: any) => { reject(err); }; | 
					
						
							|  |  |  |         fn(done); | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let restoreJasmineBeforeEach = | 
					
						
							|  |  |  |       () => { jasmine.getEnv().beforeEach = originalJasmineBeforeEach; }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const shouldNotSucceed = | 
					
						
							|  |  |  |     (done: DoneFn) => () => done.fail( 'Expected an error, but did not get one.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const shouldFail = | 
					
						
							|  |  |  |     (done: DoneFn, emsg: string) => (err: any) => {  expect(err).toEqual(emsg);  done(); }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should fail when an asynchronous error is thrown', (done: DoneFn) => { | 
					
						
							|  |  |  |     let itPromise = patchJasmineIt(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('throws an async error', | 
					
						
							|  |  |  |         async(inject([], () => { setTimeout(() => { throw new Error('bar'); }, 0); }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     itPromise.then( | 
					
						
							|  |  |  |       shouldNotSucceed(done), | 
					
						
							|  |  |  |       err => { | 
					
						
							|  |  |  |         expect(err).toEqual('bar'); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     restoreJasmineIt(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should fail when a returned promise is rejected', (done: DoneFn) => { | 
					
						
							|  |  |  |     let itPromise = patchJasmineIt(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |     it('should fail with an error from a promise', async(() => { | 
					
						
							| 
									
										
										
										
											2016-05-20 01:09:45 -07:00
										 |  |  |       return Promise.reject('baz'); | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     itPromise.then( | 
					
						
							|  |  |  |       shouldNotSucceed(done), | 
					
						
							|  |  |  |       err => { | 
					
						
							|  |  |  |         expect(err).toEqual('Uncaught (in promise): baz'); | 
					
						
							|  |  |  |         done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     restoreJasmineIt(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should fail when an error occurs inside inject', (done: DoneFn) => { | 
					
						
							|  |  |  |     let itPromise = patchJasmineIt(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('throws an error', inject([], () => { throw new Error('foo'); })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     itPromise.then( | 
					
						
							|  |  |  |       shouldNotSucceed(done), | 
					
						
							|  |  |  |       shouldFail(done, 'foo') | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     restoreJasmineIt(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // TODO(juliemr): reenable this test when we are using a test zone and can capture this error.
 | 
					
						
							|  |  |  |   it('should fail when an asynchronous error is thrown', (done: DoneFn) => { | 
					
						
							|  |  |  |     let itPromise = patchJasmineIt(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('throws an async error', | 
					
						
							|  |  |  |       async(inject([], () => { setTimeout(() => { throw new Error('bar'); }, 0); }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     itPromise.then( | 
					
						
							|  |  |  |       shouldNotSucceed(done), | 
					
						
							|  |  |  |       shouldFail(done, 'bar') | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     restoreJasmineIt(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should fail when XHR loading of a template fails', (done: DoneFn) => { | 
					
						
							|  |  |  |     let itPromise = patchJasmineIt(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should fail with an error from a promise', | 
					
						
							|  |  |  |       async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | 
					
						
							| 
									
										
										
										
											2016-06-03 18:15:14 -07:00
										 |  |  |         tcb.createAsync(BadTemplateUrlComp); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     itPromise.then( | 
					
						
							|  |  |  |       shouldNotSucceed(done), | 
					
						
							|  |  |  |       shouldFail(done, 'Uncaught (in promise): Failed to load non-existant.html') | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     restoreJasmineIt(); | 
					
						
							|  |  |  |   }, 10000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 18:19:10 +02:00
										 |  |  |   describe('using addProviders', () => { | 
					
						
							|  |  |  |     addProviders([{ provide: FancyService, useValue: new FancyService() }]); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach( | 
					
						
							|  |  |  |         inject([FancyService], (service: FancyService) => { expect(service.value).toEqual('real value'); })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 18:19:10 +02:00
										 |  |  |     describe('nested addProviders', () => { | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should fail when the injector has already been used', () => { | 
					
						
							|  |  |  |         patchJasmineBeforeEach(); | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							| 
									
										
										
										
											2016-07-06 18:19:10 +02:00
										 |  |  |           addProviders([{ provide: FancyService, useValue: new FancyService() }]); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2016-07-06 18:19:10 +02:00
										 |  |  |         .toThrowError('addProviders was called after the injector had been used ' + | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |                       'in a beforeEach or it block. This invalidates the test injector'); | 
					
						
							|  |  |  |         restoreJasmineBeforeEach(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |