| 
									
										
										
										
											2016-10-04 20:39:20 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('router', function () { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   var elt, testMod; | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  |   beforeEach(function () { | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     testMod = angular.module('testMod', ['ngComponentRouter']) | 
					
						
							|  |  |  |       .value('$routerRootComponent', 'app'); | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   it('should work with a provided root component', function() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  |     registerComponent('homeCmp', { | 
					
						
							|  |  |  |       template: 'Home' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     registerComponent('app', { | 
					
						
							|  |  |  |       template: '<div ng-outlet></div>', | 
					
						
							|  |  |  |       $routeConfig: [ | 
					
						
							|  |  |  |         { path: '/', component: 'homeCmp' } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     module('testMod'); | 
					
						
							|  |  |  |     compileApp(); | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     inject(function($location, $rootScope) { | 
					
						
							|  |  |  |       $location.path('/'); | 
					
						
							|  |  |  |       $rootScope.$digest(); | 
					
						
							|  |  |  |       expect(elt.text()).toBe('Home'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   it('should bind the component to the current router', function() { | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  |     var router; | 
					
						
							|  |  |  |     registerComponent('homeCmp', { | 
					
						
							| 
									
										
										
										
											2016-03-21 16:27:17 +05:30
										 |  |  |       providers: { $router: '=' }, | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  |       controller: function($scope, $element) { | 
					
						
							|  |  |  |         this.$routerOnActivate = function() { | 
					
						
							| 
									
										
										
										
											2016-02-22 14:01:02 +00:00
										 |  |  |           router = this.$router; | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  |         }; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       template: 'Home' | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  |     registerComponent('app', { | 
					
						
							|  |  |  |       template: '<div ng-outlet></div>', | 
					
						
							|  |  |  |       $routeConfig: [ | 
					
						
							|  |  |  |         { path: '/', component: 'homeCmp' } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     module('testMod'); | 
					
						
							|  |  |  |     compileApp(); | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     inject(function($location, $rootScope) { | 
					
						
							|  |  |  |       $location.path('/'); | 
					
						
							|  |  |  |       $rootScope.$digest(); | 
					
						
							|  |  |  |       var homeElement = elt.find('home-cmp'); | 
					
						
							|  |  |  |       expect(homeElement.text()).toBe('Home'); | 
					
						
							|  |  |  |       expect(homeElement.isolateScope().$ctrl.$router).toBeDefined(); | 
					
						
							|  |  |  |       expect(router).toBeDefined(); | 
					
						
							| 
									
										
										
										
											2017-09-22 19:51:03 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   it('should work when an async route is provided route data', function() { | 
					
						
							|  |  |  |     registerComponent('homeCmp', { | 
					
						
							|  |  |  |       template: 'Home ({{$ctrl.isAdmin}})', | 
					
						
							| 
									
										
										
										
											2016-01-31 14:52:19 -06:00
										 |  |  |       $routerOnActivate: function(next, prev) { | 
					
						
							|  |  |  |         this.isAdmin = next.routeData.data.isAdmin; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     registerComponent('app', { | 
					
						
							| 
									
										
										
										
											2016-01-31 14:52:19 -06:00
										 |  |  |       template: '<div ng-outlet></div>', | 
					
						
							|  |  |  |       $routeConfig: [ | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |         { path: '/', loader: function($q) { return $q.when('homeCmp'); }, data: { isAdmin: true } } | 
					
						
							| 
									
										
										
										
											2016-01-31 14:52:19 -06:00
										 |  |  |       ] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     module('testMod'); | 
					
						
							|  |  |  |     compileApp(); | 
					
						
							| 
									
										
										
										
											2016-01-31 14:52:19 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     inject(function($location, $rootScope) { | 
					
						
							|  |  |  |       $location.path('/'); | 
					
						
							|  |  |  |       $rootScope.$digest(); | 
					
						
							|  |  |  |       expect(elt.text()).toBe('Home (true)'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should work with a templateUrl component', function() { | 
					
						
							| 
									
										
										
										
											2016-01-31 14:52:19 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 16:59:26 -08:00
										 |  |  |     var $routerOnActivate = jasmine.createSpy('$routerOnActivate'); | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 16:59:26 -08:00
										 |  |  |     registerComponent('homeCmp', { | 
					
						
							|  |  |  |       templateUrl: 'homeCmp.html', | 
					
						
							|  |  |  |       $routerOnActivate: $routerOnActivate | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     registerComponent('app', { | 
					
						
							|  |  |  |       template: '<div ng-outlet></div>', | 
					
						
							|  |  |  |       $routeConfig: [ | 
					
						
							|  |  |  |         { path: '/', component: 'homeCmp' } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     module('testMod'); | 
					
						
							| 
									
										
										
										
											2016-02-10 16:59:26 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     inject(function($location, $rootScope, $httpBackend) { | 
					
						
							| 
									
										
										
										
											2016-02-10 16:59:26 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |       $httpBackend.expectGET('homeCmp.html').respond('Home'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       compileApp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $location.path('/'); | 
					
						
							|  |  |  |       $rootScope.$digest(); | 
					
						
							|  |  |  |       $httpBackend.flush(); | 
					
						
							|  |  |  |       var homeElement = elt.find('home-cmp'); | 
					
						
							|  |  |  |       expect(homeElement.text()).toBe('Home'); | 
					
						
							|  |  |  |       expect($routerOnActivate).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2017-09-22 19:51:03 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should provide the current instruction', function() { | 
					
						
							| 
									
										
										
										
											2016-02-18 09:25:03 -06:00
										 |  |  |     registerComponent('homeCmp', { | 
					
						
							|  |  |  |       template: 'Home ({{homeCmp.isAdmin}})' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     registerComponent('app', { | 
					
						
							|  |  |  |       template: '<div ng-outlet></div>', | 
					
						
							|  |  |  |       $routeConfig: [ | 
					
						
							|  |  |  |         { path: '/', component: 'homeCmp', name: 'Home' } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     module('testMod'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     inject(function($rootScope, $rootRouter, $location) { | 
					
						
							|  |  |  |       compileApp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $location.path('/'); | 
					
						
							|  |  |  |       $rootScope.$digest(); | 
					
						
							|  |  |  |       var instruction = $rootRouter.generate(['/Home']); | 
					
						
							|  |  |  |       expect($rootRouter.currentInstruction).toEqual(instruction); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-02-18 09:25:03 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   it('should provide the root level router', function() { | 
					
						
							| 
									
										
										
										
											2016-02-18 09:25:03 -06:00
										 |  |  |     registerComponent('homeCmp', { | 
					
						
							|  |  |  |       template: 'Home ({{homeCmp.isAdmin}})', | 
					
						
							| 
									
										
										
										
											2016-03-21 16:27:17 +05:30
										 |  |  |       providers: { | 
					
						
							| 
									
										
										
										
											2016-02-18 09:25:03 -06:00
										 |  |  |         $router: '<' | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     registerComponent('app', { | 
					
						
							|  |  |  |       template: '<div ng-outlet></div>', | 
					
						
							|  |  |  |       $routeConfig: [ | 
					
						
							|  |  |  |         { path: '/', component: 'homeCmp', name: 'Home' } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     module('testMod'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     inject(function($rootScope, $rootRouter, $location) { | 
					
						
							|  |  |  |       compileApp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $location.path('/'); | 
					
						
							|  |  |  |       $rootScope.$digest(); | 
					
						
							|  |  |  |       var homeElement = elt.find('home-cmp'); | 
					
						
							|  |  |  |       expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  |   function registerComponent(name, options) { | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  |     var definition = { | 
					
						
							| 
									
										
										
										
											2016-03-21 16:27:17 +05:30
										 |  |  |       providers: options.providers, | 
					
						
							|  |  |  |       controller: getController(options) | 
					
						
							| 
									
										
										
										
											2016-02-10 16:59:26 -08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2016-02-10 17:00:34 -08:00
										 |  |  |     if (options.template) definition.template = options.template; | 
					
						
							|  |  |  |     if (options.templateUrl) definition.templateUrl = options.templateUrl; | 
					
						
							| 
									
										
										
										
											2016-02-10 16:59:26 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 13:09:31 +00:00
										 |  |  |     applyStaticProperties(definition.controller, options); | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |     angular.module('testMod').component(name, definition); | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 22:09:37 +00:00
										 |  |  |   function compileApp() { | 
					
						
							|  |  |  |     inject(function($compile, $rootScope) { | 
					
						
							|  |  |  |       elt = $compile('<div><app></app</div>')($rootScope); | 
					
						
							|  |  |  |       $rootScope.$digest(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-11-01 14:31:36 -06:00
										 |  |  |     return elt; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-02-07 20:40:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function getController(options) { | 
					
						
							|  |  |  |     var controller = options.controller || function () {}; | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       '$routerOnActivate', '$routerOnDeactivate', | 
					
						
							|  |  |  |       '$routerOnReuse', '$routerCanReuse', | 
					
						
							|  |  |  |       '$routerCanDeactivate' | 
					
						
							|  |  |  |     ].forEach(function (hookName) { | 
					
						
							|  |  |  |       if (options[hookName]) { | 
					
						
							|  |  |  |         controller.prototype[hookName] = options[hookName]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return controller; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function applyStaticProperties(target, options) { | 
					
						
							|  |  |  |     ['$canActivate', '$routeConfig'].forEach(function(property) { | 
					
						
							|  |  |  |       if (options[property]) { | 
					
						
							|  |  |  |         target[property] = options[property]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-01-31 14:52:19 -06:00
										 |  |  | }); |