| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							| 
									
										
										
										
											2015-11-18 09:18:37 -08:00
										 |  |  |   afterEach, | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xdescribe, | 
					
						
							| 
									
										
										
										
											2015-11-18 09:18:37 -08:00
										 |  |  |   Log, | 
					
						
							| 
									
										
										
										
											2015-08-11 14:00:54 -07:00
										 |  |  |   xit | 
					
						
							| 
									
										
										
										
											2015-10-13 00:29:13 -07:00
										 |  |  | } from 'angular2/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {IS_DART, isPresent, stringify} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-11-13 11:21:16 -08:00
										 |  |  | import {bootstrap} from 'angular2/platform/browser'; | 
					
						
							|  |  |  | import {ApplicationRef} from 'angular2/src/core/application_ref'; | 
					
						
							| 
									
										
										
										
											2015-11-10 15:42:22 -08:00
										 |  |  | import {Component, Directive, View, OnDestroy, platform} from 'angular2/core'; | 
					
						
							| 
									
										
										
										
											2015-11-13 11:21:16 -08:00
										 |  |  | import {BROWSER_PROVIDERS, BROWSER_APP_PROVIDERS} from 'angular2/platform/browser'; | 
					
						
							| 
									
										
										
										
											2015-11-19 15:09:34 -08:00
										 |  |  | import {DOM} from 'angular2/src/platform/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2015-11-17 15:24:36 -08:00
										 |  |  | import {DOCUMENT} from 'angular2/src/platform/dom/dom_tokens'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {PromiseWrapper} from 'angular2/src/facade/async'; | 
					
						
							| 
									
										
										
										
											2015-11-18 09:18:37 -08:00
										 |  |  | import {provide, Inject, Injector, PLATFORM_INITIALIZER, APP_INITIALIZER} from 'angular2/core'; | 
					
						
							|  |  |  | import {disposePlatform} from 'angular2/src/core/application_ref'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {ExceptionHandler} from 'angular2/src/facade/exceptions'; | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | import {Testability, TestabilityRegistry} from 'angular2/src/core/testability/testability'; | 
					
						
							| 
									
										
										
										
											2015-10-08 08:22:11 -07:00
										 |  |  | import {ComponentRef_} from "angular2/src/core/linker/dynamic_component_loader"; | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'hello-app'}) | 
					
						
							|  |  |  | @View({template: '{{greeting}} world!'}) | 
					
						
							|  |  |  | class HelloRootCmp { | 
					
						
							|  |  |  |   greeting: string; | 
					
						
							|  |  |  |   constructor() { this.greeting = 'hello'; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'hello-app'}) | 
					
						
							| 
									
										
										
										
											2015-06-24 13:46:39 -07:00
										 |  |  | @View({template: 'before: <ng-content></ng-content> after: done'}) | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | class HelloRootCmpContent { | 
					
						
							|  |  |  |   constructor() {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'hello-app-2'}) | 
					
						
							|  |  |  | @View({template: '{{greeting}} world, again!'}) | 
					
						
							|  |  |  | class HelloRootCmp2 { | 
					
						
							|  |  |  |   greeting: string; | 
					
						
							|  |  |  |   constructor() { this.greeting = 'hello'; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'hello-app'}) | 
					
						
							|  |  |  | @View({template: ''}) | 
					
						
							|  |  |  | class HelloRootCmp3 { | 
					
						
							|  |  |  |   appBinding; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(@Inject("appBinding") appBinding) { this.appBinding = appBinding; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'hello-app'}) | 
					
						
							|  |  |  | @View({template: ''}) | 
					
						
							|  |  |  | class HelloRootCmp4 { | 
					
						
							| 
									
										
										
										
											2015-10-28 10:34:13 -07:00
										 |  |  |   appRef; | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-28 10:34:13 -07:00
										 |  |  |   constructor(@Inject(ApplicationRef) appRef) { this.appRef = appRef; } | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'hello-app'}) | 
					
						
							|  |  |  | class HelloRootMissingTemplate { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Directive({selector: 'hello-app'}) | 
					
						
							|  |  |  | class HelloRootDirectiveIsNotCmp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 15:42:22 -08:00
										 |  |  | @Component({selector: 'hello-app'}) | 
					
						
							|  |  |  | @View({template: ''}) | 
					
						
							|  |  |  | class HelloOnDestroyTickCmp implements OnDestroy { | 
					
						
							|  |  |  |   appRef: ApplicationRef; | 
					
						
							|  |  |  |   constructor(@Inject(ApplicationRef) appRef) { this.appRef = appRef; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onDestroy(): void { this.appRef.tick(); } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  | class _ArrayLogger { | 
					
						
							|  |  |  |   res: any[] = []; | 
					
						
							|  |  |  |   log(s: any): void { this.res.push(s); } | 
					
						
							| 
									
										
										
										
											2015-08-24 11:35:27 -07:00
										 |  |  |   logError(s: any): void { this.res.push(s); } | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  |   logGroup(s: any): void { this.res.push(s); } | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |   logGroupEnd(){}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |   var fakeDoc, el, el2, testProviders, lightDom; | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('bootstrap factory method', () => { | 
					
						
							| 
									
										
										
										
											2015-06-24 13:46:39 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       fakeDoc = DOM.createHtmlDocument(); | 
					
						
							|  |  |  |       el = DOM.createElement('hello-app', fakeDoc); | 
					
						
							|  |  |  |       el2 = DOM.createElement('hello-app-2', fakeDoc); | 
					
						
							|  |  |  |       lightDom = DOM.createElement('light-dom-el', fakeDoc); | 
					
						
							|  |  |  |       DOM.appendChild(fakeDoc.body, el); | 
					
						
							|  |  |  |       DOM.appendChild(fakeDoc.body, el2); | 
					
						
							|  |  |  |       DOM.appendChild(el, lightDom); | 
					
						
							|  |  |  |       DOM.setText(lightDom, 'loading'); | 
					
						
							| 
									
										
										
										
											2015-10-12 11:30:34 -07:00
										 |  |  |       testProviders = [provide(DOCUMENT, {useValue: fakeDoc})]; | 
					
						
							| 
									
										
										
										
											2015-06-24 13:46:39 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 09:18:37 -08:00
										 |  |  |     afterEach(disposePlatform); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |     it('should throw if bootstrapped Directive is not a Component', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-08-05 10:37:11 -07:00
										 |  |  |          var logger = new _ArrayLogger(); | 
					
						
							|  |  |  |          var exceptionHandler = new ExceptionHandler(logger, false); | 
					
						
							|  |  |  |          var refPromise = | 
					
						
							|  |  |  |              bootstrap(HelloRootDirectiveIsNotCmp, | 
					
						
							| 
									
										
										
										
											2015-10-12 11:30:34 -07:00
										 |  |  |                        [testProviders, provide(ExceptionHandler, {useValue: exceptionHandler})]); | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |          PromiseWrapper.then(refPromise, null, (exception) => { | 
					
						
							|  |  |  |            expect(exception).toContainError( | 
					
						
							| 
									
										
										
										
											2015-10-01 10:07:49 -07:00
										 |  |  |                `Could not compile '${stringify(HelloRootDirectiveIsNotCmp)}' because it is not a component.`); | 
					
						
							|  |  |  |            expect(logger.res.join("")).toContain("Could not compile"); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |            async.done(); | 
					
						
							|  |  |  |            return null; | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw if no element is found', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  |          var logger = new _ArrayLogger(); | 
					
						
							| 
									
										
										
										
											2015-08-11 14:00:54 -07:00
										 |  |  |          var exceptionHandler = new ExceptionHandler(logger, IS_DART ? false : true); | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  |          var refPromise = | 
					
						
							| 
									
										
										
										
											2015-10-12 11:30:34 -07:00
										 |  |  |              bootstrap(HelloRootCmp, [provide(ExceptionHandler, {useValue: exceptionHandler})]); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |          PromiseWrapper.then(refPromise, null, (reason) => { | 
					
						
							|  |  |  |            expect(reason.message).toContain('The selector "hello-app" did not match any elements'); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |            return null; | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  |     if (DOM.supportsDOMEvents()) { | 
					
						
							|  |  |  |       it('should invoke the default exception handler when bootstrap fails', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |            var logger = new _ArrayLogger(); | 
					
						
							| 
									
										
										
										
											2015-08-11 14:00:54 -07:00
										 |  |  |            var exceptionHandler = new ExceptionHandler(logger, IS_DART ? false : true); | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            var refPromise = | 
					
						
							| 
									
										
										
										
											2015-10-12 11:30:34 -07:00
										 |  |  |                bootstrap(HelloRootCmp, [provide(ExceptionHandler, {useValue: exceptionHandler})]); | 
					
						
							| 
									
										
										
										
											2015-07-27 15:47:42 -07:00
										 |  |  |            PromiseWrapper.then(refPromise, null, (reason) => { | 
					
						
							|  |  |  |              expect(logger.res.join("")) | 
					
						
							|  |  |  |                  .toContain('The selector "hello-app" did not match any elements'); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |              return null; | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |     it('should create an injector promise', () => { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |       var refPromise = bootstrap(HelloRootCmp, testProviders); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |       expect(refPromise).not.toBe(null); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should display hello world', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |          var refPromise = bootstrap(HelloRootCmp, testProviders); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |          refPromise.then((ref) => { | 
					
						
							|  |  |  |            expect(el).toHaveText('hello world!'); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should support multiple calls to bootstrap', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |          var refPromise1 = bootstrap(HelloRootCmp, testProviders); | 
					
						
							|  |  |  |          var refPromise2 = bootstrap(HelloRootCmp2, testProviders); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |          PromiseWrapper.all([refPromise1, refPromise2]) | 
					
						
							|  |  |  |              .then((refs) => { | 
					
						
							|  |  |  |                expect(el).toHaveText('hello world!'); | 
					
						
							|  |  |  |                expect(el2).toHaveText('hello world, again!'); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-11-13 11:21:16 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 15:42:22 -08:00
										 |  |  |     it('should not crash if change detection is invoked when the root component is disposed', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |          bootstrap(HelloOnDestroyTickCmp, testProviders) | 
					
						
							|  |  |  |              .then((ref) => { | 
					
						
							|  |  |  |                expect(() => ref.dispose()).not.toThrow(); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 10:40:33 -08:00
										 |  |  |     it('should unregister change detectors when components are disposed', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-11-13 11:21:16 -08:00
										 |  |  |          var app = platform(BROWSER_PROVIDERS).application([BROWSER_APP_PROVIDERS, testProviders]); | 
					
						
							| 
									
										
										
										
											2015-11-10 10:40:33 -08:00
										 |  |  |          app.bootstrap(HelloRootCmp) | 
					
						
							|  |  |  |              .then((ref) => { | 
					
						
							|  |  |  |                ref.dispose(); | 
					
						
							|  |  |  |                expect(() => app.tick()).not.toThrow(); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it("should make the provided bindings available to the application component", | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |          var refPromise = bootstrap( | 
					
						
							| 
									
										
										
										
											2015-10-12 11:30:34 -07:00
										 |  |  |              HelloRootCmp3, [testProviders, provide("appBinding", {useValue: "BoundValue"})]); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          refPromise.then((ref) => { | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |            expect(ref.hostComponent.appBinding).toEqual("BoundValue"); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should avoid cyclic dependencies when root component requires Lifecycle through DI", | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |          var refPromise = bootstrap(HelloRootCmp4, testProviders); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          refPromise.then((ref) => { | 
					
						
							| 
									
										
										
										
											2015-10-28 10:34:13 -07:00
										 |  |  |            expect(ref.hostComponent.appRef).toBe((<ComponentRef_>ref).injector.get(ApplicationRef)); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 09:18:37 -08:00
										 |  |  |     it("should run platform initializers", inject([Log], (log: Log) => { | 
					
						
							|  |  |  |          let p = platform([ | 
					
						
							|  |  |  |            BROWSER_PROVIDERS, | 
					
						
							|  |  |  |            provide(PLATFORM_INITIALIZER, {useValue: log.fn("platform_init1"), multi: true}), | 
					
						
							|  |  |  |            provide(PLATFORM_INITIALIZER, {useValue: log.fn("platform_init2"), multi: true}) | 
					
						
							|  |  |  |          ]); | 
					
						
							|  |  |  |          expect(log.result()).toEqual("platform_init1; platform_init2"); | 
					
						
							|  |  |  |          log.clear(); | 
					
						
							|  |  |  |          p.application([ | 
					
						
							|  |  |  |            BROWSER_APP_PROVIDERS, | 
					
						
							|  |  |  |            provide(APP_INITIALIZER, {useValue: log.fn("app_init1"), multi: true}), | 
					
						
							|  |  |  |            provide(APP_INITIALIZER, {useValue: log.fn("app_init2"), multi: true}) | 
					
						
							|  |  |  |          ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          expect(log.result()).toEqual("app_init1; app_init2"); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |     it('should register each application with the testability registry', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |          var refPromise1 = bootstrap(HelloRootCmp, testProviders); | 
					
						
							|  |  |  |          var refPromise2 = bootstrap(HelloRootCmp2, testProviders); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          PromiseWrapper.all([refPromise1, refPromise2]) | 
					
						
							| 
									
										
										
										
											2015-06-12 15:49:34 -07:00
										 |  |  |              .then((refs: ApplicationRef[]) => { | 
					
						
							|  |  |  |                var registry = refs[0].injector.get(TestabilityRegistry); | 
					
						
							|  |  |  |                var testabilities = | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |                    [refs[0].injector.get(Testability), refs[1].injector.get(Testability)]; | 
					
						
							| 
									
										
										
										
											2015-06-12 15:49:34 -07:00
										 |  |  |                PromiseWrapper.all(testabilities) | 
					
						
							|  |  |  |                    .then((testabilities: Testability[]) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 09:45:15 -07:00
										 |  |  |                      expect(registry.findTestabilityInTree(el)).toEqual(testabilities[0]); | 
					
						
							|  |  |  |                      expect(registry.findTestabilityInTree(el2)).toEqual(testabilities[1]); | 
					
						
							|  |  |  |                      async.done(); | 
					
						
							|  |  |  |                    }); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |