| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |   beforeEachBindings, | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  |   flushMicrotasks, | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xdescribe, | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |   TestComponentBuilder, | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   xit, | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {bootstrap} from 'angular2/src/core/application'; | 
					
						
							|  |  |  | import {Component, Directive, View} from 'angular2/src/core/annotations/decorators'; | 
					
						
							|  |  |  | import {DOM} from 'angular2/src/dom/dom_adapter'; | 
					
						
							|  |  |  | import {bind} from 'angular2/di'; | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  | import {DOCUMENT_TOKEN} from 'angular2/src/render/render'; | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  | import {RouteConfig, Route, Redirect} from 'angular2/src/router/route_config_decorator'; | 
					
						
							| 
									
										
										
										
											2015-06-08 18:04:13 -07:00
										 |  |  | import {PromiseWrapper} from 'angular2/src/facade/async'; | 
					
						
							|  |  |  | import {BaseException} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   routerInjectables, | 
					
						
							|  |  |  |   RouteParams, | 
					
						
							|  |  |  |   Router, | 
					
						
							|  |  |  |   appBaseHrefToken, | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  |   routerDirectives, | 
					
						
							|  |  |  |   HashLocationStrategy | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  | } from 'angular2/router'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 12:14:19 -07:00
										 |  |  | import {LocationStrategy} from 'angular2/src/router/location_strategy'; | 
					
						
							|  |  |  | import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy'; | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  | import {appComponentTypeToken} from 'angular2/src/core/application_tokens'; | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('router injectables', () => { | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |     beforeEachBindings(() => { | 
					
						
							|  |  |  |       return [routerInjectables, bind(LocationStrategy).toClass(MockLocationStrategy)]; | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |     // do not refactor out the `bootstrap` functionality. We still want to
 | 
					
						
							|  |  |  |     // keep this test around so we can ensure that bootstrapping a router works
 | 
					
						
							|  |  |  |     describe('boostrap functionality', () => { | 
					
						
							|  |  |  |       it('should bootstrap a simple app', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |            var fakeDoc = DOM.createHtmlDocument(); | 
					
						
							|  |  |  |            var el = DOM.createElement('app-cmp', fakeDoc); | 
					
						
							|  |  |  |            DOM.appendChild(fakeDoc.body, el); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |            bootstrap(AppCmp, | 
					
						
							|  |  |  |                      [ | 
					
						
							|  |  |  |                        routerInjectables, | 
					
						
							|  |  |  |                        bind(LocationStrategy).toClass(MockLocationStrategy), | 
					
						
							|  |  |  |                        bind(DOCUMENT_TOKEN).toValue(fakeDoc) | 
					
						
							|  |  |  |                      ]) | 
					
						
							|  |  |  |                .then((applicationRef) => { | 
					
						
							|  |  |  |                  var router = applicationRef.hostComponent.router; | 
					
						
							|  |  |  |                  router.subscribe((_) => { | 
					
						
							|  |  |  |                    expect(el).toHaveText('outer { hello }'); | 
					
						
							|  |  |  |                    expect(applicationRef.hostComponent.location.path()).toEqual(''); | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							| 
									
										
										
										
											2015-06-08 18:04:13 -07:00
										 |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-06-08 18:04:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |     describe('broken app', () => { | 
					
						
							|  |  |  |       beforeEachBindings(() => { return [bind(appComponentTypeToken).toValue(BrokenAppCmp)]; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should rethrow exceptions from component constructors', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  |            tcb.createAsync(AppCmp).then((rootTC) => { | 
					
						
							|  |  |  |              var router = rootTC.componentInstance.router; | 
					
						
							|  |  |  |              PromiseWrapper.catchError(router.navigate('/cause-error'), (error) => { | 
					
						
							|  |  |  |                expect(rootTC.nativeElement).toHaveText('outer { oh no }'); | 
					
						
							| 
									
										
										
										
											2015-07-23 18:00:19 -07:00
										 |  |  |                expect(error).toContainError('oops!'); | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |                async.done(); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |            }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  |     describe('back button app', () => { | 
					
						
							|  |  |  |       beforeEachBindings(() => { return [bind(appComponentTypeToken).toValue(HierarchyAppCmp)]; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should change the url without pushing a new history state for back navigations', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tcb.createAsync(HierarchyAppCmp) | 
					
						
							|  |  |  |                .then((rootTC) => { | 
					
						
							|  |  |  |                  var router = rootTC.componentInstance.router; | 
					
						
							|  |  |  |                  var position = 0; | 
					
						
							|  |  |  |                  var flipped = false; | 
					
						
							|  |  |  |                  var history = | 
					
						
							|  |  |  |                      [ | 
					
						
							|  |  |  |                        ['/parent/child', 'root { parent { hello } }', '/super-parent/child'], | 
					
						
							|  |  |  |                        ['/super-parent/child', 'root { super-parent { hello2 } }', '/parent/child'], | 
					
						
							|  |  |  |                        ['/parent/child', 'root { parent { hello } }', false] | 
					
						
							|  |  |  |                      ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                      router.subscribe((_) => { | 
					
						
							|  |  |  |                        var location = rootTC.componentInstance.location; | 
					
						
							|  |  |  |                        var element = rootTC.nativeElement; | 
					
						
							|  |  |  |                        var path = location.path(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                        var entry = history[position]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                        expect(path).toEqual(entry[0]); | 
					
						
							|  |  |  |                        expect(element).toHaveText(entry[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                        var nextUrl = entry[2]; | 
					
						
							|  |  |  |                        if (nextUrl == false) { | 
					
						
							|  |  |  |                          flipped = true; | 
					
						
							|  |  |  |                        } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                        if (flipped && position == 0) { | 
					
						
							|  |  |  |                          async.done(); | 
					
						
							|  |  |  |                          return; | 
					
						
							|  |  |  |                        } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                        position = position + (flipped ? -1 : 1); | 
					
						
							|  |  |  |                        if (flipped) { | 
					
						
							|  |  |  |                          location.back(); | 
					
						
							|  |  |  |                        } else { | 
					
						
							|  |  |  |                          router.navigate(nextUrl); | 
					
						
							|  |  |  |                        } | 
					
						
							|  |  |  |                      }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                  router.navigate(history[0][0]); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |     describe('hierarchical app', () => { | 
					
						
							|  |  |  |       beforeEachBindings(() => { return [bind(appComponentTypeToken).toValue(HierarchyAppCmp)]; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should bootstrap an app with a hierarchy', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tcb.createAsync(HierarchyAppCmp) | 
					
						
							|  |  |  |                .then((rootTC) => { | 
					
						
							|  |  |  |                  var router = rootTC.componentInstance.router; | 
					
						
							|  |  |  |                  router.subscribe((_) => { | 
					
						
							|  |  |  |                    expect(rootTC.nativeElement).toHaveText('root { parent { hello } }'); | 
					
						
							|  |  |  |                    expect(rootTC.componentInstance.location.path()).toEqual('/parent/child'); | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |                  router.navigate('/parent/child'); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-07-21 15:05:08 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe('custom app base ref', () => { | 
					
						
							|  |  |  |         beforeEachBindings(() => { return [bind(appBaseHrefToken).toValue('/my/app')]; }); | 
					
						
							|  |  |  |         it('should bootstrap', inject([AsyncTestCompleter, TestComponentBuilder], | 
					
						
							|  |  |  |                                       (async, tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                                         tcb.createAsync(HierarchyAppCmp) | 
					
						
							|  |  |  |                                             .then((rootTC) => { | 
					
						
							|  |  |  |                                               var router = rootTC.componentInstance.router; | 
					
						
							|  |  |  |                                               router.subscribe((_) => { | 
					
						
							|  |  |  |                                                 expect(rootTC.nativeElement) | 
					
						
							|  |  |  |                                                     .toHaveText('root { parent { hello } }'); | 
					
						
							|  |  |  |                                                 expect(rootTC.componentInstance.location.path()) | 
					
						
							|  |  |  |                                                     .toEqual('/my/app/parent/child'); | 
					
						
							|  |  |  |                                                 async.done(); | 
					
						
							|  |  |  |                                               }); | 
					
						
							|  |  |  |                                               router.navigate('/parent/child'); | 
					
						
							|  |  |  |                                             }); | 
					
						
							|  |  |  |                                       })); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |     // TODO: add a test in which the child component has bindings
 | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('querystring params app', () => { | 
					
						
							|  |  |  |       beforeEachBindings( | 
					
						
							|  |  |  |           () => { return [bind(appComponentTypeToken).toValue(QueryStringAppCmp)]; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should recognize and return querystring params with the injected RouteParams', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter, TestComponentBuilder], (async, tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  |            tcb.createAsync(QueryStringAppCmp) | 
					
						
							|  |  |  |                .then((rootTC) => { | 
					
						
							|  |  |  |                  var router = rootTC.componentInstance.router; | 
					
						
							|  |  |  |                  router.subscribe((_) => { | 
					
						
							|  |  |  |                    rootTC.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                    expect(rootTC.nativeElement).toHaveText('qParam = search-for-something'); | 
					
						
							|  |  |  |                    /* | 
					
						
							|  |  |  |                    expect(applicationRef.hostComponent.location.path()) | 
					
						
							|  |  |  |                        .toEqual('/qs?q=search-for-something');*/ | 
					
						
							|  |  |  |                    async.done(); | 
					
						
							|  |  |  |                  }); | 
					
						
							|  |  |  |                  router.navigate('/qs?q=search-for-something'); | 
					
						
							|  |  |  |                  rootTC.detectChanges(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'hello-cmp'}) | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  | @View({template: 'hello'}) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | class HelloCmp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  | @Component({selector: 'hello2-cmp'}) | 
					
						
							|  |  |  | @View({template: 'hello2'}) | 
					
						
							|  |  |  | class Hello2Cmp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | @Component({selector: 'app-cmp'}) | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  | @View({template: "outer { <router-outlet></router-outlet> }", directives: routerDirectives}) | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  | @RouteConfig([new Route({path: '/', component: HelloCmp})]) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | class AppCmp { | 
					
						
							| 
									
										
										
										
											2015-06-22 12:14:19 -07:00
										 |  |  |   constructor(public router: Router, public location: LocationStrategy) {} | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'parent-cmp'}) | 
					
						
							|  |  |  | @View({template: `parent { <router-outlet></router-outlet> }`, directives: routerDirectives}) | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  | @RouteConfig([new Route({path: '/child', component: HelloCmp})]) | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  | class ParentCmp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  | @Component({selector: 'super-parent-cmp'}) | 
					
						
							|  |  |  | @View({template: `super-parent { <router-outlet></router-outlet> }`, directives: routerDirectives}) | 
					
						
							|  |  |  | @RouteConfig([new Route({path: '/child', component: Hello2Cmp})]) | 
					
						
							|  |  |  | class SuperParentCmp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  | @Component({selector: 'app-cmp'}) | 
					
						
							|  |  |  | @View({template: `root { <router-outlet></router-outlet> }`, directives: routerDirectives}) | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  | @RouteConfig([ | 
					
						
							|  |  |  |   new Route({path: '/parent/...', component: ParentCmp}), | 
					
						
							|  |  |  |   new Route({path: '/super-parent/...', component: SuperParentCmp}) | 
					
						
							|  |  |  | ]) | 
					
						
							| 
									
										
										
										
											2015-06-15 15:41:09 -07:00
										 |  |  | class HierarchyAppCmp { | 
					
						
							| 
									
										
										
										
											2015-06-22 12:14:19 -07:00
										 |  |  |   constructor(public router: Router, public location: LocationStrategy) {} | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-08 18:04:13 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  | @Component({selector: 'qs-cmp'}) | 
					
						
							|  |  |  | @View({template: "qParam = {{q}}"}) | 
					
						
							|  |  |  | class QSCmp { | 
					
						
							|  |  |  |   q: string; | 
					
						
							|  |  |  |   constructor(params: RouteParams) { this.q = params.get('q'); } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'app-cmp'}) | 
					
						
							|  |  |  | @View({template: `<router-outlet></router-outlet>`, directives: routerDirectives}) | 
					
						
							|  |  |  | @RouteConfig([new Route({path: '/qs', component: QSCmp})]) | 
					
						
							|  |  |  | class QueryStringAppCmp { | 
					
						
							|  |  |  |   constructor(public router: Router, public location: LocationStrategy) {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-08 18:04:13 -07:00
										 |  |  | @Component({selector: 'oops-cmp'}) | 
					
						
							|  |  |  | @View({template: "oh no"}) | 
					
						
							|  |  |  | class BrokenCmp { | 
					
						
							|  |  |  |   constructor() { throw new BaseException('oops!'); } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'app-cmp'}) | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  | @View({template: `outer { <router-outlet></router-outlet> }`, directives: routerDirectives}) | 
					
						
							|  |  |  | @RouteConfig([new Route({path: '/cause-error', component: BrokenCmp})]) | 
					
						
							| 
									
										
										
										
											2015-06-08 18:04:13 -07:00
										 |  |  | class BrokenAppCmp { | 
					
						
							| 
									
										
										
										
											2015-06-22 12:14:19 -07:00
										 |  |  |   constructor(public router: Router, public location: LocationStrategy) {} | 
					
						
							| 
									
										
										
										
											2015-06-08 18:04:13 -07:00
										 |  |  | } |