| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   it, iit, | 
					
						
							|  |  |  |   ddescribe, expect, | 
					
						
							|  |  |  |   inject, beforeEach, | 
					
						
							|  |  |  |   SpyObject} from 'angular2/test_lib'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {RouteRecognizer} from 'angular2/src/router/route_recognizer'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('RouteRecognizer', () => { | 
					
						
							|  |  |  |     var recognizer; | 
					
						
							|  |  |  |     var handler = { | 
					
						
							|  |  |  |       'components': { 'a': 'b' } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  |     var handler2 = { | 
					
						
							|  |  |  |       'components': { 'b': 'c' } | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       recognizer = new RouteRecognizer(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  |     it('should recognize a static segment', () => { | 
					
						
							|  |  |  |       recognizer.addConfig('/test', handler); | 
					
						
							|  |  |  |       expect(recognizer.recognize('/test')[0].handler).toEqual(handler); | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-05 15:19:06 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  |     it('should recognize a single slash', () => { | 
					
						
							|  |  |  |       recognizer.addConfig('/', handler); | 
					
						
							|  |  |  |       var solution = recognizer.recognize('/')[0]; | 
					
						
							|  |  |  |       expect(solution.handler).toEqual(handler); | 
					
						
							| 
									
										
										
										
											2015-05-05 15:19:06 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should recognize a dynamic segment', () => { | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  |       recognizer.addConfig('/user/:name', handler); | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  |       var solution = recognizer.recognize('/user/brian')[0]; | 
					
						
							|  |  |  |       expect(solution.handler).toEqual(handler); | 
					
						
							|  |  |  |       expect(solution.params).toEqual({ 'name': 'brian' }); | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should recognize a star segment', () => { | 
					
						
							|  |  |  |       recognizer.addConfig('/first/*rest', handler); | 
					
						
							|  |  |  |       var solution = recognizer.recognize('/first/second/third')[0]; | 
					
						
							|  |  |  |       expect(solution.handler).toEqual(handler); | 
					
						
							|  |  |  |       expect(solution.params).toEqual({ 'rest': 'second/third' }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw when given two routes that start with the same static segment', () => { | 
					
						
							|  |  |  |       recognizer.addConfig('/hello', handler); | 
					
						
							|  |  |  |       expect(() => recognizer.addConfig('/hello', handler2)).toThrowError( | 
					
						
							|  |  |  |         'Configuration \'/hello\' conflicts with existing route \'/hello\'' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw when given two routes that have dynamic segments in the same order', () => { | 
					
						
							|  |  |  |       recognizer.addConfig('/hello/:person/how/:doyoudou', handler); | 
					
						
							|  |  |  |       expect(() => recognizer.addConfig('/hello/:friend/how/:areyou', handler2)).toThrowError( | 
					
						
							|  |  |  |         'Configuration \'/hello/:friend/how/:areyou\' conflicts with existing route \'/hello/:person/how/:doyoudou\'' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should recognize redirects', () => { | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  |       recognizer.addRedirect('/a', '/b'); | 
					
						
							|  |  |  |       recognizer.addConfig('/b', handler); | 
					
						
							|  |  |  |       var solutions = recognizer.recognize('/a'); | 
					
						
							|  |  |  |       expect(solutions.length).toBe(1); | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       var solution = solutions[0]; | 
					
						
							|  |  |  |       expect(solution.handler).toEqual(handler); | 
					
						
							|  |  |  |       expect(solution.matchedUrl).toEqual('/b'); | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  |     it('should generate URLs', () => { | 
					
						
							|  |  |  |       recognizer.addConfig('/app/user/:name', handler, 'user'); | 
					
						
							|  |  |  |       expect(recognizer.generate('user', {'name' : 'misko'})).toEqual('/app/user/misko'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-06 18:30:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-15 02:05:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 18:30:37 -07:00
										 |  |  |     it('should throw in the absence of required params URLs', () => { | 
					
						
							|  |  |  |       recognizer.addConfig('/app/user/:name', handler, 'user'); | 
					
						
							|  |  |  |       expect(() => recognizer.generate('user', {})).toThrowError( | 
					
						
							|  |  |  |         'Route generator for \'name\' was not included in parameters passed.'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-04-17 09:59:56 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } |