| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   xdescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   beforeEachProviders, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit | 
					
						
							| 
									
										
										
										
											2016-05-02 10:36:58 -07:00
										 |  |  | } from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 10:36:58 -07:00
										 |  |  | import {recognize} from '../src/recognize'; | 
					
						
							|  |  |  | import {Routes, Route} from '@angular/router'; | 
					
						
							|  |  |  | import {provide, Component, ComponentResolver} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  | import {UrlSegment, RouteTree, UrlTree, createEmptyRouteTree} from '../src/segments'; | 
					
						
							| 
									
										
										
										
											2016-05-02 10:36:58 -07:00
										 |  |  | import {DefaultRouterUrlSerializer} from '../src/router_url_serializer'; | 
					
						
							|  |  |  | import {DEFAULT_OUTLET_NAME} from '../src/constants'; | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:40:17 -07:00
										 |  |  |   describe('recognize', () => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |     let emptyRouteTree = createEmptyRouteTree(ComponentA); | 
					
						
							| 
									
										
										
										
											2016-05-04 15:01:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |     it('should handle position args', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("b/paramB/c/paramC/d"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |              .then(r => { | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |                let a = r.root; | 
					
						
							|  |  |  |                expect(stringifyUrl(a.urlSegments)).toEqual([""]); | 
					
						
							|  |  |  |                expect(a.type).toBe(ComponentA); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                let b = r.firstChild(r.root); | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |                expect(stringifyUrl(b.urlSegments)).toEqual(["b", "paramB"]); | 
					
						
							|  |  |  |                expect(b.type).toBe(ComponentB); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |                let c = r.firstChild(r.firstChild(r.root)); | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |                expect(stringifyUrl(c.urlSegments)).toEqual(["c", "paramC"]); | 
					
						
							|  |  |  |                expect(c.type).toBe(ComponentC); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |                let d = r.firstChild(r.firstChild(r.firstChild(r.root))); | 
					
						
							|  |  |  |                expect(stringifyUrl(d.urlSegments)).toEqual(["d"]); | 
					
						
							|  |  |  |                expect(d.type).toBe(ComponentD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 18:33:48 -07:00
										 |  |  |     it('should support empty routes', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("f"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-28 18:33:48 -07:00
										 |  |  |              .then(r => { | 
					
						
							|  |  |  |                let a = r.root; | 
					
						
							|  |  |  |                expect(stringifyUrl(a.urlSegments)).toEqual([""]); | 
					
						
							|  |  |  |                expect(a.type).toBe(ComponentA); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                let f = r.firstChild(r.root); | 
					
						
							|  |  |  |                expect(stringifyUrl(f.urlSegments)).toEqual(["f"]); | 
					
						
							|  |  |  |                expect(f.type).toBe(ComponentF); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                let d = r.firstChild(r.firstChild(r.root)); | 
					
						
							|  |  |  |                expect(stringifyUrl(d.urlSegments)).toEqual([]); | 
					
						
							|  |  |  |                expect(d.type).toBe(ComponentD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |     it('should handle aux routes', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("b/paramB(/d//right:d)"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |              .then(r => { | 
					
						
							|  |  |  |                let c = r.children(r.root); | 
					
						
							|  |  |  |                expect(stringifyUrl(c[0].urlSegments)).toEqual(["b", "paramB"]); | 
					
						
							|  |  |  |                expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME); | 
					
						
							|  |  |  |                expect(c[0].type).toBe(ComponentB); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(stringifyUrl(c[1].urlSegments)).toEqual(["d"]); | 
					
						
							|  |  |  |                expect(c[1].outlet).toEqual("aux"); | 
					
						
							|  |  |  |                expect(c[1].type).toBe(ComponentD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(stringifyUrl(c[2].urlSegments)).toEqual(["d"]); | 
					
						
							|  |  |  |                expect(c[2].outlet).toEqual("right"); | 
					
						
							|  |  |  |                expect(c[2].type).toBe(ComponentD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should error when two segments with the same outlet name", | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("b/paramB(right:d//right:e)"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |              .catch(e => { | 
					
						
							|  |  |  |                expect(e.message).toEqual( | 
					
						
							|  |  |  |                    "Two segments cannot have the same outlet name: 'right:d' and 'right:e'."); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should handle nested aux routes', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("b/paramB(/d(right:e))"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |              .then(r => { | 
					
						
							|  |  |  |                let c = r.children(r.root); | 
					
						
							|  |  |  |                expect(stringifyUrl(c[0].urlSegments)).toEqual(["b", "paramB"]); | 
					
						
							|  |  |  |                expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME); | 
					
						
							|  |  |  |                expect(c[0].type).toBe(ComponentB); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(stringifyUrl(c[1].urlSegments)).toEqual(["d"]); | 
					
						
							|  |  |  |                expect(c[1].outlet).toEqual("aux"); | 
					
						
							|  |  |  |                expect(c[1].type).toBe(ComponentD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(stringifyUrl(c[2].urlSegments)).toEqual(["e"]); | 
					
						
							|  |  |  |                expect(c[2].outlet).toEqual("right"); | 
					
						
							|  |  |  |                expect(c[2].type).toBe(ComponentE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 15:36:11 -07:00
										 |  |  |     it('should handle non top-level aux routes', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree('b/paramB/d(e)'), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-27 15:36:11 -07:00
										 |  |  |              .then(r => { | 
					
						
							|  |  |  |                let c = r.children(r.firstChild(r.root)); | 
					
						
							|  |  |  |                expect(stringifyUrl(c[0].urlSegments)).toEqual(["d"]); | 
					
						
							|  |  |  |                expect(c[0].outlet).toEqual(DEFAULT_OUTLET_NAME); | 
					
						
							|  |  |  |                expect(c[0].type).toBe(ComponentD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(stringifyUrl(c[1].urlSegments)).toEqual(["e"]); | 
					
						
							|  |  |  |                expect(c[1].outlet).toEqual("aux"); | 
					
						
							|  |  |  |                expect(c[1].type).toBe(ComponentE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |     it('should handle matrix parameters', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("b/paramB;b1=1;b2=2(/d;d1=1;d2=2)"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |              .then(r => { | 
					
						
							|  |  |  |                let c = r.children(r.root); | 
					
						
							|  |  |  |                expect(c[0].parameters).toEqual({'b': 'paramB', 'b1': '1', 'b2': '2'}); | 
					
						
							|  |  |  |                expect(c[1].parameters).toEqual({'d1': '1', 'd2': '2'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 18:08:26 -07:00
										 |  |  |     it('should match a wildcard', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentG, tree("a;aa=1/b;bb=2"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-30 18:08:26 -07:00
										 |  |  |              .then(r => { | 
					
						
							|  |  |  |                let c = r.children(r.root); | 
					
						
							|  |  |  |                expect(c.length).toEqual(1); | 
					
						
							|  |  |  |                expect(stringifyUrl(c[0].urlSegments)).toEqual([]); | 
					
						
							|  |  |  |                expect(c[0].parameters).toEqual(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |     it('should error when no matching routes', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("invalid"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |              .catch(e => { | 
					
						
							| 
									
										
										
										
											2016-04-28 18:33:48 -07:00
										 |  |  |                expect(e.message).toContain("Cannot match any routes"); | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should handle no matching routes (too short)', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("b"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |              .catch(e => { | 
					
						
							| 
									
										
										
										
											2016-04-28 18:33:48 -07:00
										 |  |  |                expect(e.message).toContain("Cannot match any routes"); | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should error when a component doesn't have @Routes", | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("d/invalid"), emptyRouteTree) | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |              .catch(e => { | 
					
						
							|  |  |  |                expect(e.message) | 
					
						
							|  |  |  |                    .toEqual("Component 'ComponentD' does not have route configuration"); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-05-04 14:43:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it("should reuse existing segments", | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, ComponentResolver], (async, resolver) => { | 
					
						
							| 
									
										
										
										
											2016-05-04 15:01:27 -07:00
										 |  |  |          recognize(resolver, ComponentA, tree("/b/1/d"), emptyRouteTree) | 
					
						
							|  |  |  |              .then(t1 => { | 
					
						
							|  |  |  |                recognize(resolver, ComponentA, tree("/b/1/e"), t1) | 
					
						
							|  |  |  |                    .then(t2 => { | 
					
						
							|  |  |  |                      expect(t1.root).toBe(t2.root); | 
					
						
							|  |  |  |                      expect(t1.firstChild(t1.root)).toBe(t2.firstChild(t2.root)); | 
					
						
							|  |  |  |                      expect(t1.firstChild(t1.firstChild(t1.root))) | 
					
						
							|  |  |  |                          .not.toBe(t2.firstChild(t2.firstChild(t2.root))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                      async.done(); | 
					
						
							|  |  |  |                    }); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-01 13:45:14 -07:00
										 |  |  | function tree(url: string): UrlTree { | 
					
						
							| 
									
										
										
										
											2016-04-27 15:36:11 -07:00
										 |  |  |   return new DefaultRouterUrlSerializer().parse(url); | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function stringifyUrl(segments: UrlSegment[]): string[] { | 
					
						
							|  |  |  |   return segments.map(s => s.segment); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'd', template: 't'}) | 
					
						
							|  |  |  | class ComponentD { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  | @Component({selector: 'e', template: 't'}) | 
					
						
							|  |  |  | class ComponentE { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 18:33:48 -07:00
										 |  |  | @Component({selector: 'f', template: 't'}) | 
					
						
							|  |  |  | @Routes([new Route({path: "/", component: ComponentD})]) | 
					
						
							|  |  |  | class ComponentF { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  | @Component({selector: 'c', template: 't'}) | 
					
						
							|  |  |  | @Routes([new Route({path: "d", component: ComponentD})]) | 
					
						
							|  |  |  | class ComponentC { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | @Component({selector: 'b', template: 't'}) | 
					
						
							| 
									
										
										
										
											2016-04-27 15:36:11 -07:00
										 |  |  | @Routes([ | 
					
						
							|  |  |  |   new Route({path: "d", component: ComponentD}), | 
					
						
							|  |  |  |   new Route({path: "e", component: ComponentE}), | 
					
						
							|  |  |  |   new Route({path: "c/:c", component: ComponentC}) | 
					
						
							|  |  |  | ]) | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | class ComponentB { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 18:08:26 -07:00
										 |  |  | @Component({selector: 'g', template: 't'}) | 
					
						
							|  |  |  | @Routes( | 
					
						
							|  |  |  |     [new Route({path: "d", component: ComponentD}), new Route({path: "*", component: ComponentE})]) | 
					
						
							|  |  |  | class ComponentG { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | @Component({selector: 'a', template: 't'}) | 
					
						
							|  |  |  | @Routes([ | 
					
						
							|  |  |  |   new Route({path: "b/:b", component: ComponentB}), | 
					
						
							| 
									
										
										
										
											2016-04-25 16:57:15 -07:00
										 |  |  |   new Route({path: "d", component: ComponentD}), | 
					
						
							| 
									
										
										
										
											2016-04-28 18:33:48 -07:00
										 |  |  |   new Route({path: "e", component: ComponentE}), | 
					
						
							|  |  |  |   new Route({path: "f", component: ComponentF}) | 
					
						
							| 
									
										
										
										
											2016-04-22 12:04:56 -07:00
										 |  |  | ]) | 
					
						
							|  |  |  | class ComponentA { | 
					
						
							| 
									
										
										
										
											2016-05-02 10:36:58 -07:00
										 |  |  | } |