aboutsummaryrefslogtreecommitdiffstats
path: root/test/ngRoute
diff options
context:
space:
mode:
Diffstat (limited to 'test/ngRoute')
-rw-r--r--test/ngRoute/directive/ngViewSpec.js669
-rw-r--r--test/ngRoute/routeParamsSpec.js48
-rw-r--r--test/ngRoute/routeSpec.js976
3 files changed, 1693 insertions, 0 deletions
diff --git a/test/ngRoute/directive/ngViewSpec.js b/test/ngRoute/directive/ngViewSpec.js
new file mode 100644
index 00000000..6beb3d27
--- /dev/null
+++ b/test/ngRoute/directive/ngViewSpec.js
@@ -0,0 +1,669 @@
+'use strict';
+
+describe('ngView', function() {
+ var element;
+
+ beforeEach(module('ngRoute'));
+
+ beforeEach(module(function($provide) {
+ $provide.value('$window', angular.mock.createMockWindow());
+ return function($rootScope, $compile, $animator) {
+ element = $compile('<ng:view onload="load()"></ng:view>')($rootScope);
+ $animator.enabled(true);
+ };
+ }));
+
+
+ afterEach(function(){
+ dealoc(element);
+ });
+
+
+ it('should do nothing when no routes are defined',
+ inject(function($rootScope, $compile, $location) {
+ $location.path('/unknown');
+ $rootScope.$digest();
+ expect(element.text()).toEqual('');
+ }));
+
+
+ it('should instantiate controller after compiling the content', function() {
+ var log = [], controllerScope,
+ Ctrl = function($scope) {
+ controllerScope = $scope;
+ log.push('ctrl-init');
+ };
+
+ module(function($compileProvider, $routeProvider) {
+ $compileProvider.directive('compileLog', function() {
+ return {
+ compile: function() {
+ log.push('compile');
+ }
+ };
+ });
+
+ $routeProvider.when('/some', {templateUrl: '/tpl.html', controller: Ctrl});
+ });
+
+ inject(function($route, $rootScope, $templateCache, $location) {
+ $templateCache.put('/tpl.html', [200, '<div compile-log>partial</div>', {}]);
+ $location.path('/some');
+ $rootScope.$digest();
+
+ expect(controllerScope.$parent).toBe($rootScope);
+ expect(controllerScope).toBe($route.current.scope);
+ expect(log).toEqual(['compile', 'ctrl-init']);
+ });
+ });
+
+
+ it('should instantiate controller with an alias', function() {
+ var log = [], controllerScope,
+ Ctrl = function($scope) {
+ this.name = 'alias';
+ controllerScope = $scope;
+ };
+
+ module(function($compileProvider, $routeProvider) {
+ $routeProvider.when('/some', {templateUrl: '/tpl.html', controller: Ctrl, controllerAs: 'ctrl'});
+ });
+
+ inject(function($route, $rootScope, $templateCache, $location) {
+ $templateCache.put('/tpl.html', [200, '<div></div>', {}]);
+ $location.path('/some');
+ $rootScope.$digest();
+
+ expect(controllerScope.ctrl.name).toBe('alias');
+ });
+ });
+
+
+ it('should support string controller declaration', function() {
+ var MyCtrl = jasmine.createSpy('MyCtrl');
+
+ module(function($controllerProvider, $routeProvider) {
+ $controllerProvider.register('MyCtrl', ['$scope', MyCtrl]);
+ $routeProvider.when('/foo', {controller: 'MyCtrl', templateUrl: '/tpl.html'});
+ });
+
+ inject(function($route, $location, $rootScope, $templateCache) {
+ $templateCache.put('/tpl.html', [200, '<div></div>', {}]);
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect($route.current.controller).toBe('MyCtrl');
+ expect(MyCtrl).toHaveBeenCalledWith(element.contents().scope());
+ });
+ });
+
+
+ it('should load content via xhr when route changes', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'myUrl1'});
+ $routeProvider.when('/bar', {templateUrl: 'myUrl2'});
+ });
+
+ inject(function($rootScope, $compile, $httpBackend, $location, $route) {
+ expect(element.text()).toEqual('');
+
+ $location.path('/foo');
+ $httpBackend.expect('GET', 'myUrl1').respond('<div>{{1+3}}</div>');
+ $rootScope.$digest();
+ $httpBackend.flush();
+ expect(element.text()).toEqual('4');
+
+ $location.path('/bar');
+ $httpBackend.expect('GET', 'myUrl2').respond('angular is da best');
+ $rootScope.$digest();
+ $httpBackend.flush();
+ expect(element.text()).toEqual('angular is da best');
+ });
+ });
+
+
+ it('should use inline content route changes', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {template: '<div>{{1+3}}</div>'});
+ $routeProvider.when('/bar', {template: 'angular is da best'});
+ $routeProvider.when('/blank', {template: ''});
+ });
+
+ inject(function($rootScope, $compile, $location, $route) {
+ expect(element.text()).toEqual('');
+
+ $location.path('/foo');
+ $rootScope.$digest();
+ expect(element.text()).toEqual('4');
+
+ $location.path('/bar');
+ $rootScope.$digest();
+ expect(element.text()).toEqual('angular is da best');
+
+ $location.path('/blank');
+ $rootScope.$digest();
+ expect(element.text()).toEqual('');
+ });
+ });
+
+
+ it('should remove all content when location changes to an unknown route', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'myUrl1'});
+ });
+
+ inject(function($rootScope, $compile, $location, $httpBackend, $route) {
+ $location.path('/foo');
+ $httpBackend.expect('GET', 'myUrl1').respond('<div>{{1+3}}</div>');
+ $rootScope.$digest();
+ $httpBackend.flush();
+ expect(element.text()).toEqual('4');
+
+ $location.path('/unknown');
+ $rootScope.$digest();
+ expect(element.text()).toEqual('');
+ });
+ });
+
+
+ it('should chain scopes and propagate evals to the child scope', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'myUrl1'});
+ });
+
+ inject(function($rootScope, $compile, $location, $httpBackend, $route) {
+ $rootScope.parentVar = 'parent';
+
+ $location.path('/foo');
+ $httpBackend.expect('GET', 'myUrl1').respond('<div>{{parentVar}}</div>');
+ $rootScope.$digest();
+ $httpBackend.flush();
+ expect(element.text()).toEqual('parent');
+
+ $rootScope.parentVar = 'new parent';
+ $rootScope.$digest();
+ expect(element.text()).toEqual('new parent');
+ });
+ });
+
+
+ it('should be possible to nest ngView in ngInclude', function() {
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'viewPartial.html'});
+ });
+
+ inject(function($httpBackend, $location, $route, $compile, $rootScope) {
+ $httpBackend.whenGET('includePartial.html').respond('view: <ng:view></ng:view>');
+ $httpBackend.whenGET('viewPartial.html').respond('content');
+ $location.path('/foo');
+
+ var elm = $compile(
+ '<div>' +
+ 'include: <ng:include src="\'includePartial.html\'"> </ng:include>' +
+ '</div>')($rootScope);
+ $rootScope.$digest();
+ $httpBackend.flush();
+
+ expect(elm.text()).toEqual('include: view: content');
+ expect($route.current.templateUrl).toEqual('viewPartial.html');
+ dealoc(elm)
+ });
+ });
+
+
+ it('should initialize view template after the view controller was initialized even when ' +
+ 'templates were cached', function() {
+ //this is a test for a regression that was introduced by making the ng-view cache sync
+ function ParentCtrl($scope) {
+ $scope.log.push('parent');
+ }
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {controller: ParentCtrl, templateUrl: 'viewPartial.html'});
+ });
+
+
+ inject(function($rootScope, $compile, $location, $httpBackend, $route) {
+ $rootScope.log = [];
+
+ $rootScope.ChildCtrl = function($scope) {
+ $scope.log.push('child');
+ };
+
+ $location.path('/foo');
+ $httpBackend.expect('GET', 'viewPartial.html').
+ respond('<div ng-init="log.push(\'init\')">' +
+ '<div ng-controller="ChildCtrl"></div>' +
+ '</div>');
+ $rootScope.$apply();
+ $httpBackend.flush();
+
+ expect($rootScope.log).toEqual(['parent', 'init', 'child']);
+
+ $location.path('/');
+ $rootScope.$apply();
+ expect($rootScope.log).toEqual(['parent', 'init', 'child']);
+
+ $rootScope.log = [];
+ $location.path('/foo');
+ $rootScope.$apply();
+
+ expect($rootScope.log).toEqual(['parent', 'init', 'child']);
+ });
+ });
+
+
+ it('should discard pending xhr callbacks if a new route is requested before the current ' +
+ 'finished loading', function() {
+ // this is a test for a bad race condition that affected feedback
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'myUrl1'});
+ $routeProvider.when('/bar', {templateUrl: 'myUrl2'});
+ });
+
+ inject(function($route, $rootScope, $location, $httpBackend) {
+ expect(element.text()).toEqual('');
+
+ $location.path('/foo');
+ $httpBackend.expect('GET', 'myUrl1').respond('<div>{{1+3}}</div>');
+ $rootScope.$digest();
+ $location.path('/bar');
+ $httpBackend.expect('GET', 'myUrl2').respond('<div>{{1+1}}</div>');
+ $rootScope.$digest();
+ $httpBackend.flush(); // now that we have two requests pending, flush!
+
+ expect(element.text()).toEqual('2');
+ });
+ });
+
+
+ it('should be async even if served from cache', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {controller: noop, templateUrl: 'myUrl1'});
+ });
+
+ inject(function($route, $rootScope, $location, $templateCache) {
+ $templateCache.put('myUrl1', [200, 'my partial', {}]);
+ $location.path('/foo');
+
+ var called = 0;
+ // we want to assert only during first watch
+ $rootScope.$watch(function() {
+ if (!called++) expect(element.text()).toBe('');
+ });
+
+ $rootScope.$digest();
+ expect(element.text()).toBe('my partial');
+ });
+ });
+
+ it('should fire $contentLoaded event when content compiled and linked', function() {
+ var log = [];
+ var logger = function(name) {
+ return function() {
+ log.push(name);
+ };
+ };
+ var Ctrl = function($scope) {
+ $scope.value = 'bound-value';
+ log.push('init-ctrl');
+ };
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'tpl.html', controller: Ctrl});
+ });
+
+ inject(function($templateCache, $rootScope, $location) {
+ $rootScope.$on('$routeChangeStart', logger('$routeChangeStart'));
+ $rootScope.$on('$routeChangeSuccess', logger('$routeChangeSuccess'));
+ $rootScope.$on('$viewContentLoaded', logger('$viewContentLoaded'));
+
+ $templateCache.put('tpl.html', [200, '{{value}}', {}]);
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect(element.text()).toBe('bound-value');
+ expect(log).toEqual([
+ '$routeChangeStart', 'init-ctrl', '$viewContentLoaded', '$routeChangeSuccess' ]);
+ });
+ });
+
+ it('should destroy previous scope', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'tpl.html'});
+ });
+
+ inject(function($templateCache, $rootScope, $location) {
+ $templateCache.put('tpl.html', [200, 'partial', {}]);
+
+ expect($rootScope.$$childHead).toBeNull();
+ expect($rootScope.$$childTail).toBeNull();
+
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect(element.text()).toBe('partial');
+ expect($rootScope.$$childHead).not.toBeNull();
+ expect($rootScope.$$childTail).not.toBeNull();
+
+ $location.path('/non/existing/route');
+ $rootScope.$digest();
+
+ expect(element.text()).toBe('');
+ expect($rootScope.$$childHead).toBeNull();
+ expect($rootScope.$$childTail).toBeNull();
+ });
+ });
+
+
+ it('should destroy previous scope if multiple route changes occur before server responds',
+ function() {
+ var log = [];
+ var createCtrl = function(name) {
+ return function($scope) {
+ log.push('init-' + name);
+ $scope.$on('$destroy', function() {log.push('destroy-' + name);});
+ };
+ };
+
+ module(function($routeProvider) {
+ $routeProvider.when('/one', {templateUrl: 'one.html', controller: createCtrl('ctrl1')});
+ $routeProvider.when('/two', {templateUrl: 'two.html', controller: createCtrl('ctrl2')});
+ });
+
+ inject(function($httpBackend, $rootScope, $location) {
+ $httpBackend.whenGET('one.html').respond('content 1');
+ $httpBackend.whenGET('two.html').respond('content 2');
+
+ $location.path('/one');
+ $rootScope.$digest();
+ $location.path('/two');
+ $rootScope.$digest();
+
+ $httpBackend.flush();
+ expect(element.text()).toBe('content 2');
+ expect(log).toEqual(['init-ctrl2']);
+
+ $location.path('/non-existing');
+ $rootScope.$digest();
+
+ expect(element.text()).toBe('');
+ expect(log).toEqual(['init-ctrl2', 'destroy-ctrl2']);
+
+ expect($rootScope.$$childHead).toBeNull();
+ expect($rootScope.$$childTail).toBeNull();
+ });
+ });
+
+
+ it('should $destroy scope after update and reload', function() {
+ // this is a regression of bug, where $route doesn't copy scope when only updating
+
+ var log = [];
+
+ function logger(msg) {
+ return function() {
+ log.push(msg);
+ };
+ }
+
+ function createController(name) {
+ return function($scope) {
+ log.push('init-' + name);
+ $scope.$on('$destroy', logger('destroy-' + name));
+ $scope.$on('$routeUpdate', logger('route-update'));
+ };
+ }
+
+ module(function($routeProvider) {
+ $routeProvider.when('/bar', {templateUrl: 'tpl.html', controller: createController('bar')});
+ $routeProvider.when('/foo', {
+ templateUrl: 'tpl.html', controller: createController('foo'), reloadOnSearch: false});
+ });
+
+ inject(function($templateCache, $location, $rootScope) {
+ $templateCache.put('tpl.html', [200, 'partial', {}]);
+
+ $location.url('/foo');
+ $rootScope.$digest();
+ expect(log).toEqual(['init-foo']);
+
+ $location.search({q: 'some'});
+ $rootScope.$digest();
+ expect(log).toEqual(['init-foo', 'route-update']);
+
+ $location.url('/bar');
+ $rootScope.$digest();
+ expect(log).toEqual(['init-foo', 'route-update', 'destroy-foo', 'init-bar']);
+ });
+ });
+
+
+ it('should evaluate onload expression after linking the content', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'tpl.html'});
+ });
+
+ inject(function($templateCache, $location, $rootScope) {
+ $templateCache.put('tpl.html', [200, '{{1+1}}', {}]);
+ $rootScope.load = jasmine.createSpy('onload');
+
+ $location.url('/foo');
+ $rootScope.$digest();
+ expect($rootScope.load).toHaveBeenCalledOnce();
+ });
+ });
+
+
+ it('should set $scope and $controllerController on the view', function() {
+ function MyCtrl($scope) {
+ $scope.state = 'WORKS';
+ $scope.ctrl = this;
+ }
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'tpl.html', controller: MyCtrl});
+ });
+
+ inject(function($templateCache, $location, $rootScope, $route) {
+ $templateCache.put('tpl.html', [200, '<div>{{state}}</div>', {}]);
+
+ $location.url('/foo');
+ $rootScope.$digest();
+ expect(element.text()).toEqual('WORKS');
+
+ var div = element.find('div');
+ expect(div.parent()[0].nodeName.toUpperCase()).toBeOneOf('NG:VIEW', 'VIEW');
+
+ expect(div.scope()).toBe($route.current.scope);
+ expect(div.scope().hasOwnProperty('state')).toBe(true);
+ expect(div.scope().state).toEqual('WORKS');
+
+ expect(div.controller()).toBe($route.current.scope.ctrl);
+ });
+ });
+
+ it('should not set $scope or $controllerController on top level text elements in the view', function() {
+ function MyCtrl($scope) {}
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'tpl.html', controller: MyCtrl});
+ });
+
+ inject(function($templateCache, $location, $rootScope, $route) {
+ $templateCache.put('tpl.html', '<div></div> ');
+ $location.url('/foo');
+ $rootScope.$digest();
+
+ forEach(element.contents(), function(node) {
+ if ( node.nodeType == 3 /* text node */) {
+ expect(jqLite(node).scope()).not.toBe($route.current.scope);
+ expect(jqLite(node).controller()).not.toBeDefined();
+ } else {
+ expect(jqLite(node).scope()).toBe($route.current.scope);
+ expect(jqLite(node).controller()).toBeDefined();
+ }
+ });
+ });
+ });
+
+ describe('ngAnimate ', function() {
+ var window, vendorPrefix;
+ var body, element;
+
+ function html(html) {
+ body.html(html);
+ element = body.children().eq(0);
+ return element;
+ }
+
+ function applyCSS(element, cssProp, cssValue) {
+ element.css(cssProp, cssValue);
+ element.css(vendorPrefix + cssProp, cssValue);
+ }
+
+ beforeEach(function() {
+ // we need to run animation on attached elements;
+ body = jqLite(document.body);
+ });
+
+ afterEach(function(){
+ dealoc(body);
+ dealoc(element);
+ });
+
+
+ beforeEach(module(function($provide, $routeProvider) {
+ $provide.value('$window', window = angular.mock.createMockWindow());
+ $routeProvider.when('/foo', {controller: noop, templateUrl: '/foo.html'});
+ return function($sniffer, $templateCache, $animator) {
+ vendorPrefix = '-' + $sniffer.vendorPrefix + '-';
+ $templateCache.put('/foo.html', [200, '<div>data</div>', {}]);
+ $animator.enabled(true);
+ }
+ }));
+
+ it('should fire off the enter animation + add and remove the css classes',
+ inject(function($compile, $rootScope, $sniffer, $location) {
+ element = $compile(html('<div ng-view ng-animate="{enter: \'custom-enter\'}"></div>'))($rootScope);
+
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ //if we add the custom css stuff here then it will get picked up before the animation takes place
+ var child = jqLite(element.children()[0]);
+ applyCSS(child, 'transition', '1s linear all');
+
+ if ($sniffer.transitions) {
+ expect(child.attr('class')).toContain('custom-enter');
+ window.setTimeout.expect(1).process();
+
+ expect(child.attr('class')).toContain('custom-enter-active');
+ window.setTimeout.expect(1000).process();
+ } else {
+ expect(window.setTimeout.queue).toEqual([]);
+ }
+
+ expect(child.attr('class')).not.toContain('custom-enter');
+ expect(child.attr('class')).not.toContain('custom-enter-active');
+ }));
+
+ it('should fire off the leave animation + add and remove the css classes',
+ inject(function($compile, $rootScope, $sniffer, $location, $templateCache) {
+ $templateCache.put('/foo.html', [200, '<div>foo</div>', {}]);
+ element = $compile(html('<div ng-view ng-animate="{leave: \'custom-leave\'}"></div>'))($rootScope);
+
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ //if we add the custom css stuff here then it will get picked up before the animation takes place
+ var child = jqLite(element.children()[0]);
+ applyCSS(child, 'transition', '1s linear all');
+
+ $location.path('/');
+ $rootScope.$digest();
+
+ if ($sniffer.transitions) {
+ expect(child.attr('class')).toContain('custom-leave');
+ window.setTimeout.expect(1).process();
+
+ expect(child.attr('class')).toContain('custom-leave-active');
+ window.setTimeout.expect(1000).process();
+ } else {
+ expect(window.setTimeout.queue).toEqual([]);
+ }
+
+ expect(child.attr('class')).not.toContain('custom-leave');
+ expect(child.attr('class')).not.toContain('custom-leave-active');
+ }));
+
+ it('should catch and use the correct duration for animations',
+ inject(function($compile, $rootScope, $sniffer, $location, $templateCache) {
+ $templateCache.put('/foo.html', [200, '<div>foo</div>', {}]);
+ element = $compile(html(
+ '<div ' +
+ 'ng-view ' +
+ 'ng-animate="{enter: \'customEnter\'}">' +
+ '</div>'
+ ))($rootScope);
+
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ //if we add the custom css stuff here then it will get picked up before the animation takes place
+ var child = jqLite(element.children()[0]);
+ applyCSS(child, 'transition', '0.5s linear all');
+
+ if($sniffer.transitions) {
+ window.setTimeout.expect(1).process();
+ window.setTimeout.expect($sniffer.transitions ? 500 : 0).process();
+ } else {
+ expect(window.setTimeout.queue).toEqual([]);
+ }
+ }));
+
+
+ it('should not double compile when route changes', function() {
+ module(function($routeProvider, $animationProvider, $provide) {
+ $routeProvider.when('/foo', {template: '<div ng-repeat="i in [1,2]">{{i}}</div>'});
+ $routeProvider.when('/bar', {template: '<div ng-repeat="i in [3,4]">{{i}}</div>'});
+ $animationProvider.register('my-animation-leave', function() {
+ return {
+ start: function(element, done) {
+ done();
+ }
+ };
+ });
+ });
+
+ inject(function($rootScope, $compile, $location, $route, $window, $rootElement, $sniffer) {
+ element = $compile(html('<ng:view onload="load()" ng-animate="\'my-animation\'"></ng:view>'))($rootScope);
+
+ $location.path('/foo');
+ $rootScope.$digest();
+ if ($sniffer.transitions) {
+ $window.setTimeout.expect(1).process();
+ $window.setTimeout.expect(0).process();
+ }
+ expect(element.text()).toEqual('12');
+
+ $location.path('/bar');
+ $rootScope.$digest();
+ expect(n(element.text())).toEqual('1234');
+ if ($sniffer.transitions) {
+ $window.setTimeout.expect(1).process();
+ $window.setTimeout.expect(1).process();
+ } else {
+ $window.setTimeout.expect(1).process();
+ }
+ expect(element.text()).toEqual('34');
+
+ function n(text) {
+ return text.replace(/\r\n/m, '').replace(/\r\n/m, '');
+ }
+ });
+ });
+ });
+});
diff --git a/test/ngRoute/routeParamsSpec.js b/test/ngRoute/routeParamsSpec.js
new file mode 100644
index 00000000..1391151c
--- /dev/null
+++ b/test/ngRoute/routeParamsSpec.js
@@ -0,0 +1,48 @@
+'use strict';
+
+describe('$routeParams', function() {
+
+ beforeEach(module('ngRoute'));
+
+
+ it('should publish the params into a service', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {});
+ $routeProvider.when('/bar/:barId', {});
+ });
+
+ inject(function($rootScope, $route, $location, $routeParams) {
+ $location.path('/foo').search('a=b');
+ $rootScope.$digest();
+ expect($routeParams).toEqual({a:'b'});
+
+ $location.path('/bar/123').search('x=abc');
+ $rootScope.$digest();
+ expect($routeParams).toEqual({barId:'123', x:'abc'});
+ });
+ });
+
+ it('should correctly extract the params when a param name is part of the route', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/bar/:foo/:bar', {});
+ });
+
+ inject(function($rootScope, $route, $location, $routeParams) {
+ $location.path('/bar/foovalue/barvalue');
+ $rootScope.$digest();
+ expect($routeParams).toEqual({bar:'barvalue', foo:'foovalue'});
+ });
+ });
+
+ it('should support route params not preceded by slashes', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/bar:barId/foo:fooId/', {});
+ });
+
+ inject(function($rootScope, $route, $location, $routeParams) {
+ $location.path('/barbarvalue/foofoovalue/');
+ $rootScope.$digest();
+ expect($routeParams).toEqual({barId: 'barvalue', fooId: 'foovalue'});
+ });
+ });
+});
diff --git a/test/ngRoute/routeSpec.js b/test/ngRoute/routeSpec.js
new file mode 100644
index 00000000..300ca2d7
--- /dev/null
+++ b/test/ngRoute/routeSpec.js
@@ -0,0 +1,976 @@
+'use strict';
+
+describe('$route', function() {
+ var $httpBackend;
+
+ beforeEach(module('ngRoute'));
+
+ beforeEach(module(function() {
+ return function(_$httpBackend_) {
+ $httpBackend = _$httpBackend_;
+ $httpBackend.when('GET', 'Chapter.html').respond('chapter');
+ $httpBackend.when('GET', 'test.html').respond('test');
+ $httpBackend.when('GET', 'foo.html').respond('foo');
+ $httpBackend.when('GET', 'baz.html').respond('baz');
+ $httpBackend.when('GET', 'bar.html').respond('bar');
+ $httpBackend.when('GET', '404.html').respond('not found');
+ };
+ }));
+
+ it('should route and fire change event', function() {
+ var log = '',
+ lastRoute,
+ nextRoute;
+
+ module(function($routeProvider) {
+ $routeProvider.when('/Book/:book/Chapter/:chapter',
+ {controller: noop, templateUrl: 'Chapter.html'});
+ $routeProvider.when('/Blank', {});
+ });
+ inject(function($route, $location, $rootScope) {
+ $rootScope.$on('$routeChangeStart', function(event, next, current) {
+ log += 'before();';
+ expect(current).toBe($route.current);
+ lastRoute = current;
+ nextRoute = next;
+ });
+ $rootScope.$on('$routeChangeSuccess', function(event, current, last) {
+ log += 'after();';
+ expect(current).toBe($route.current);
+ expect(lastRoute).toBe(last);
+ expect(nextRoute).toBe(current);
+ });
+
+ $location.path('/Book/Moby/Chapter/Intro').search('p=123');
+ $rootScope.$digest();
+ $httpBackend.flush();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
+
+ log = '';
+ $location.path('/Blank').search('ignore');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({ignore:true});
+
+ log = '';
+ $location.path('/NONE');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current).toEqual(null);
+ });
+ });
+
+ it('should route and fire change event when catch-all params are used', function() {
+ var log = '',
+ lastRoute,
+ nextRoute;
+
+ module(function($routeProvider) {
+ $routeProvider.when('/Book1/:book/Chapter/:chapter/*highlight/edit',
+ {controller: noop, templateUrl: 'Chapter.html'});
+ $routeProvider.when('/Book2/:book/*highlight/Chapter/:chapter',
+ {controller: noop, templateUrl: 'Chapter.html'});
+ $routeProvider.when('/Blank', {});
+ });
+ inject(function($route, $location, $rootScope) {
+ $rootScope.$on('$routeChangeStart', function(event, next, current) {
+ log += 'before();';
+ expect(current).toBe($route.current);
+ lastRoute = current;
+ nextRoute = next;
+ });
+ $rootScope.$on('$routeChangeSuccess', function(event, current, last) {
+ log += 'after();';
+ expect(current).toBe($route.current);
+ expect(lastRoute).toBe(last);
+ expect(nextRoute).toBe(current);
+ });
+
+ $location.path('/Book1/Moby/Chapter/Intro/one/edit').search('p=123');
+ $rootScope.$digest();
+ $httpBackend.flush();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one', p:'123'});
+
+ log = '';
+ $location.path('/Blank').search('ignore');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({ignore:true});
+
+ log = '';
+ $location.path('/Book1/Moby/Chapter/Intro/one/two/edit').search('p=123');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});
+
+ log = '';
+ $location.path('/Book2/Moby/one/two/Chapter/Intro').search('p=123');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});
+
+ log = '';
+ $location.path('/NONE');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current).toEqual(null);
+ });
+ });
+
+
+ it('should route and fire change event correctly whenever the case insensitive flag is utilized', function() {
+ var log = '',
+ lastRoute,
+ nextRoute;
+
+ module(function($routeProvider) {
+ $routeProvider.when('/Book1/:book/Chapter/:chapter/*highlight/edit',
+ {controller: noop, templateUrl: 'Chapter.html', caseInsensitiveMatch: true});
+ $routeProvider.when('/Book2/:book/*highlight/Chapter/:chapter',
+ {controller: noop, templateUrl: 'Chapter.html'});
+ $routeProvider.when('/Blank', {});
+ });
+ inject(function($route, $location, $rootScope) {
+ $rootScope.$on('$routeChangeStart', function(event, next, current) {
+ log += 'before();';
+ expect(current).toBe($route.current);
+ lastRoute = current;
+ nextRoute = next;
+ });
+ $rootScope.$on('$routeChangeSuccess', function(event, current, last) {
+ log += 'after();';
+ expect(current).toBe($route.current);
+ expect(lastRoute).toBe(last);
+ expect(nextRoute).toBe(current);
+ });
+
+ $location.path('/Book1/Moby/Chapter/Intro/one/edit').search('p=123');
+ $rootScope.$digest();
+ $httpBackend.flush();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one', p:'123'});
+
+ log = '';
+ $location.path('/BOOK1/Moby/CHAPTER/Intro/one/EDIT').search('p=123');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one', p:'123'});
+
+ log = '';
+ $location.path('/Blank').search('ignore');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({ignore:true});
+
+ log = '';
+ $location.path('/BLANK');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current).toEqual(null);
+
+ log = '';
+ $location.path('/Book2/Moby/one/two/Chapter/Intro').search('p=123');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});
+
+ log = '';
+ $location.path('/BOOK2/Moby/one/two/CHAPTER/Intro').search('p=123');
+ $rootScope.$digest();
+ expect(log).toEqual('before();after();');
+ expect($route.current).toEqual(null);
+ });
+ });
+
+
+ it('should not change route when location is canceled', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/somePath', {template: 'some path'});
+ });
+ inject(function($route, $location, $rootScope, $log) {
+ $rootScope.$on('$locationChangeStart', function(event) {
+ $log.info('$locationChangeStart');
+ event.preventDefault();
+ });
+
+ $rootScope.$on('$beforeRouteChange', function(event) {
+ throw new Error('Should not get here');
+ });
+
+ $location.path('/somePath');
+ $rootScope.$digest();
+
+ expect($log.info.logs.shift()).toEqual(['$locationChangeStart']);
+ });
+ });
+
+
+ describe('should match a route that contains special chars in the path', function() {
+ beforeEach(module(function($routeProvider) {
+ $routeProvider.when('/$test.23/foo*(bar)/:baz', {templateUrl: 'test.html'});
+ }));
+
+ it('matches the full path', inject(function($route, $location, $rootScope) {
+ $location.path('/test');
+ $rootScope.$digest();
+ expect($route.current).toBeUndefined();
+ }));
+
+ it('matches literal .', inject(function($route, $location, $rootScope) {
+ $location.path('/$testX23/foo*(bar)/222');
+ $rootScope.$digest();
+ expect($route.current).toBeUndefined();
+ }));
+
+ it('matches literal *', inject(function($route, $location, $rootScope) {
+ $location.path('/$test.23/foooo(bar)/222');
+ $rootScope.$digest();
+ expect($route.current).toBeUndefined();
+ }));
+
+ it('treats backslashes normally', inject(function($route, $location, $rootScope) {
+ $location.path('/$test.23/foo*\\(bar)/222');
+ $rootScope.$digest();
+ expect($route.current).toBeUndefined();
+ }));
+
+ it('matches a URL with special chars', inject(function($route, $location, $rootScope) {
+ $location.path('/$test.23/foo*(bar)/222');
+ $rootScope.$digest();
+ expect($route.current).toBeDefined();
+ }));
+ });
+
+
+ it('should change route even when only search param changes', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/test', {templateUrl: 'test.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ var callback = jasmine.createSpy('onRouteChange');
+
+ $rootScope.$on('$routeChangeStart', callback);
+ $location.path('/test');
+ $rootScope.$digest();
+ callback.reset();
+
+ $location.search({any: true});
+ $rootScope.$digest();
+
+ expect(callback).toHaveBeenCalled();
+ });
+ });
+
+
+ it('should allow routes to be defined with just templates without controllers', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ var onChangeSpy = jasmine.createSpy('onChange');
+
+ $rootScope.$on('$routeChangeStart', onChangeSpy);
+ expect($route.current).toBeUndefined();
+ expect(onChangeSpy).not.toHaveBeenCalled();
+
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect($route.current.templateUrl).toEqual('foo.html');
+ expect($route.current.controller).toBeUndefined();
+ expect(onChangeSpy).toHaveBeenCalled();
+ });
+ });
+
+
+ it('should chain whens and otherwise', function() {
+ module(function($routeProvider){
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'}).
+ otherwise({templateUrl: 'bar.html'}).
+ when('/baz', {templateUrl: 'baz.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $rootScope.$digest();
+ expect($route.current.templateUrl).toBe('bar.html');
+
+ $location.url('/baz');
+ $rootScope.$digest();
+ expect($route.current.templateUrl).toBe('baz.html');
+ });
+ });
+
+
+ describe('otherwise', function() {
+
+ it('should handle unknown routes with "otherwise" route definition', function() {
+ function NotFoundCtrl() {}
+
+ module(function($routeProvider){
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'});
+ $routeProvider.otherwise({templateUrl: '404.html', controller: NotFoundCtrl});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ var onChangeSpy = jasmine.createSpy('onChange');
+
+ $rootScope.$on('$routeChangeStart', onChangeSpy);
+ expect($route.current).toBeUndefined();
+ expect(onChangeSpy).not.toHaveBeenCalled();
+
+ $location.path('/unknownRoute');
+ $rootScope.$digest();
+
+ expect($route.current.templateUrl).toBe('404.html');
+ expect($route.current.controller).toBe(NotFoundCtrl);
+ expect(onChangeSpy).toHaveBeenCalled();
+
+ onChangeSpy.reset();
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect($route.current.templateUrl).toEqual('foo.html');
+ expect($route.current.controller).toBeUndefined();
+ expect(onChangeSpy).toHaveBeenCalled();
+ });
+ });
+
+
+ it('should update $route.current and $route.next when default route is matched', function() {
+ module(function($routeProvider){
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'});
+ $routeProvider.otherwise({templateUrl: '404.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ var currentRoute, nextRoute,
+ onChangeSpy = jasmine.createSpy('onChange').andCallFake(function(e, next) {
+ currentRoute = $route.current;
+ nextRoute = next;
+ });
+
+
+ // init
+ $rootScope.$on('$routeChangeStart', onChangeSpy);
+ expect($route.current).toBeUndefined();
+ expect(onChangeSpy).not.toHaveBeenCalled();
+
+
+ // match otherwise route
+ $location.path('/unknownRoute');
+ $rootScope.$digest();
+
+ expect(currentRoute).toBeUndefined();
+ expect(nextRoute.templateUrl).toBe('404.html');
+ expect($route.current.templateUrl).toBe('404.html');
+ expect(onChangeSpy).toHaveBeenCalled();
+ onChangeSpy.reset();
+
+ // match regular route
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect(currentRoute.templateUrl).toBe('404.html');
+ expect(nextRoute.templateUrl).toBe('foo.html');
+ expect($route.current.templateUrl).toEqual('foo.html');
+ expect(onChangeSpy).toHaveBeenCalled();
+ onChangeSpy.reset();
+
+ // match otherwise route again
+ $location.path('/anotherUnknownRoute');
+ $rootScope.$digest();
+
+ expect(currentRoute.templateUrl).toBe('foo.html');
+ expect(nextRoute.templateUrl).toBe('404.html');
+ expect($route.current.templateUrl).toEqual('404.html');
+ expect(onChangeSpy).toHaveBeenCalled();
+ });
+ });
+ });
+
+
+ describe('events', function() {
+ it('should not fire $after/beforeRouteChange during bootstrap (if no route)', function() {
+ var routeChangeSpy = jasmine.createSpy('route change');
+
+ module(function($routeProvider) {
+ $routeProvider.when('/one', {}); // no otherwise defined
+ });
+
+ inject(function($rootScope, $route, $location) {
+ $rootScope.$on('$routeChangeStart', routeChangeSpy);
+ $rootScope.$on('$routeChangeSuccess', routeChangeSpy);
+
+ $rootScope.$digest();
+ expect(routeChangeSpy).not.toHaveBeenCalled();
+
+ $location.path('/no-route-here');
+ $rootScope.$digest();
+ expect(routeChangeSpy).not.toHaveBeenCalled();
+ });
+ });
+
+ it('should fire $routeChangeStart and resolve promises', function() {
+ var deferA,
+ deferB;
+
+ module(function($provide, $routeProvider) {
+ $provide.factory('b', function($q) {
+ deferB = $q.defer();
+ return deferB.promise;
+ });
+ $routeProvider.when('/path', { templateUrl: 'foo.html', resolve: {
+ a: ['$q', function($q) {
+ deferA = $q.defer();
+ return deferA.promise;
+ }],
+ b: 'b'
+ } });
+ });
+
+ inject(function($location, $route, $rootScope, $httpBackend) {
+ var log = '';
+
+ $httpBackend.expectGET('foo.html').respond('FOO');
+
+ $location.path('/path');
+ $rootScope.$digest();
+ expect(log).toEqual('');
+ $httpBackend.flush();
+ expect(log).toEqual('');
+ deferA.resolve();
+ $rootScope.$digest();
+ expect(log).toEqual('');
+ deferB.resolve();
+ $rootScope.$digest();
+ expect($route.current.locals.$template).toEqual('FOO');
+ });
+ });
+
+
+ it('should fire $routeChangeError event on resolution error', function() {
+ var deferA;
+
+ module(function($provide, $routeProvider) {
+ $routeProvider.when('/path', { template: 'foo', resolve: {
+ a: function($q) {
+ deferA = $q.defer();
+ return deferA.promise;
+ }
+ } });
+ });
+
+ inject(function($location, $route, $rootScope) {
+ var log = '';
+
+ $rootScope.$on('$routeChangeStart', function() { log += 'before();'; });
+ $rootScope.$on('$routeChangeError', function(e, n, l, reason) { log += 'failed(' + reason + ');'; });
+
+ $location.path('/path');
+ $rootScope.$digest();
+ expect(log).toEqual('before();');
+
+ deferA.reject('MyError');
+ $rootScope.$digest();
+ expect(log).toEqual('before();failed(MyError);');
+ });
+ });
+
+
+ it('should fetch templates', function() {
+ module(function($routeProvider) {
+ $routeProvider.
+ when('/r1', { templateUrl: 'r1.html' }).
+ when('/r2', { templateUrl: 'r2.html' });
+ });
+
+ inject(function($route, $httpBackend, $location, $rootScope) {
+ var log = '';
+ $rootScope.$on('$routeChangeStart', function(e, next) { log += '$before(' + next.templateUrl + ');'});
+ $rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after(' + next.templateUrl + ');'});
+
+ $httpBackend.expectGET('r1.html').respond('R1');
+ $httpBackend.expectGET('r2.html').respond('R2');
+
+ $location.path('/r1');
+ $rootScope.$digest();
+ expect(log).toBe('$before(r1.html);');
+
+ $location.path('/r2');
+ $rootScope.$digest();
+ expect(log).toBe('$before(r1.html);$before(r2.html);');
+
+ $httpBackend.flush();
+ expect(log).toBe('$before(r1.html);$before(r2.html);$after(r2.html);');
+ expect(log).not.toContain('$after(r1.html);');
+ });
+ });
+
+
+ it('should not update $routeParams until $routeChangeSuccess', function() {
+ module(function($routeProvider) {
+ $routeProvider.
+ when('/r1/:id', { templateUrl: 'r1.html' }).
+ when('/r2/:id', { templateUrl: 'r2.html' });
+ });
+
+ inject(function($route, $httpBackend, $location, $rootScope, $routeParams) {
+ var log = '';
+ $rootScope.$on('$routeChangeStart', function(e, next) { log += '$before' + toJson($routeParams) + ';'});
+ $rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after' + toJson($routeParams) + ';'});
+
+ $httpBackend.whenGET('r1.html').respond('R1');
+ $httpBackend.whenGET('r2.html').respond('R2');
+
+ $location.path('/r1/1');
+ $rootScope.$digest();
+ expect(log).toBe('$before{};');
+ $httpBackend.flush();
+ expect(log).toBe('$before{};$after{"id":"1"};');
+
+ log = '';
+
+ $location.path('/r2/2');
+ $rootScope.$digest();
+ expect(log).toBe('$before{"id":"1"};');
+ $httpBackend.flush();
+ expect(log).toBe('$before{"id":"1"};$after{"id":"2"};');
+ });
+ });
+
+
+ it('should drop in progress route change when new route change occurs', function() {
+ module(function($routeProvider) {
+ $routeProvider.
+ when('/r1', { templateUrl: 'r1.html' }).
+ when('/r2', { templateUrl: 'r2.html' });
+ });
+
+ inject(function($route, $httpBackend, $location, $rootScope) {
+ var log = '';
+ $rootScope.$on('$routeChangeStart', function(e, next) { log += '$before(' + next.templateUrl + ');'});
+ $rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after(' + next.templateUrl + ');'});
+
+ $httpBackend.expectGET('r1.html').respond('R1');
+ $httpBackend.expectGET('r2.html').respond('R2');
+
+ $location.path('/r1');
+ $rootScope.$digest();
+ expect(log).toBe('$before(r1.html);');
+
+ $location.path('/r2');
+ $rootScope.$digest();
+ expect(log).toBe('$before(r1.html);$before(r2.html);');
+
+ $httpBackend.flush();
+ expect(log).toBe('$before(r1.html);$before(r2.html);$after(r2.html);');
+ expect(log).not.toContain('$after(r1.html);');
+ });
+ });
+
+
+ it('should drop in progress route change when new route change occurs and old fails', function() {
+ module(function($routeProvider) {
+ $routeProvider.
+ when('/r1', { templateUrl: 'r1.html' }).
+ when('/r2', { templateUrl: 'r2.html' });
+ });
+
+ inject(function($route, $httpBackend, $location, $rootScope) {
+ var log = '';
+ $rootScope.$on('$routeChangeError', function(e, next, last, error) {
+ log += '$failed(' + next.templateUrl + ', ' + error.status + ');';
+ });
+ $rootScope.$on('$routeChangeStart', function(e, next) { log += '$before(' + next.templateUrl + ');'});
+ $rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after(' + next.templateUrl + ');'});
+
+ $httpBackend.expectGET('r1.html').respond(404, 'R1');
+ $httpBackend.expectGET('r2.html').respond('R2');
+
+ $location.path('/r1');
+ $rootScope.$digest();
+ expect(log).toBe('$before(r1.html);');
+
+ $location.path('/r2');
+ $rootScope.$digest();
+ expect(log).toBe('$before(r1.html);$before(r2.html);');
+
+ $httpBackend.flush();
+ expect(log).toBe('$before(r1.html);$before(r2.html);$after(r2.html);');
+ expect(log).not.toContain('$after(r1.html);');
+ });
+ });
+
+
+ it('should catch local factory errors', function() {
+ var myError = new Error('MyError');
+ module(function($routeProvider, $exceptionHandlerProvider) {
+ $exceptionHandlerProvider.mode('log');
+ $routeProvider.when('/locals', {
+ resolve: {
+ a: function($q) {
+ throw myError;
+ }
+ }
+ });
+ });
+
+ inject(function($location, $route, $rootScope, $exceptionHandler) {
+ $location.path('/locals');
+ $rootScope.$digest();
+ expect($exceptionHandler.errors).toEqual([myError]);
+ });
+ });
+ });
+
+
+ it('should match route with and without trailing slash', function() {
+ module(function($routeProvider){
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'});
+ $routeProvider.when('/bar/', {templateUrl: 'bar.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $location.path('/foo');
+ $rootScope.$digest();
+ expect($location.path()).toBe('/foo');
+ expect($route.current.templateUrl).toBe('foo.html');
+
+ $location.path('/foo/');
+ $rootScope.$digest();
+ expect($location.path()).toBe('/foo');
+ expect($route.current.templateUrl).toBe('foo.html');
+
+ $location.path('/bar');
+ $rootScope.$digest();
+ expect($location.path()).toBe('/bar/');
+ expect($route.current.templateUrl).toBe('bar.html');
+
+ $location.path('/bar/');
+ $rootScope.$digest();
+ expect($location.path()).toBe('/bar/');
+ expect($route.current.templateUrl).toBe('bar.html');
+ });
+ });
+
+
+ describe('redirection', function() {
+ it('should support redirection via redirectTo property by updating $location', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/', {redirectTo: '/foo'});
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'});
+ $routeProvider.when('/bar', {templateUrl: 'bar.html'});
+ $routeProvider.when('/baz', {redirectTo: '/bar'});
+ $routeProvider.otherwise({templateUrl: '404.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ var onChangeSpy = jasmine.createSpy('onChange');
+
+ $rootScope.$on('$routeChangeStart', onChangeSpy);
+ expect($route.current).toBeUndefined();
+ expect(onChangeSpy).not.toHaveBeenCalled();
+
+ $location.path('/');
+ $rootScope.$digest();
+ expect($location.path()).toBe('/foo');
+ expect($route.current.templateUrl).toBe('foo.html');
+ expect(onChangeSpy.callCount).toBe(2);
+
+ onChangeSpy.reset();
+ $location.path('/baz');
+ $rootScope.$digest();
+ expect($location.path()).toBe('/bar');
+ expect($route.current.templateUrl).toBe('bar.html');
+ expect(onChangeSpy.callCount).toBe(2);
+ });
+ });
+
+
+ it('should interpolate route vars in the redirected path from original path', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/foo/:id/foo/:subid/:extraId', {redirectTo: '/bar/:id/:subid/23'});
+ $routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $location.path('/foo/id1/foo/subid3/gah');
+ $rootScope.$digest();
+
+ expect($location.path()).toEqual('/bar/id1/subid3/23');
+ expect($location.search()).toEqual({extraId: 'gah'});
+ expect($route.current.templateUrl).toEqual('bar.html');
+ });
+ });
+
+
+ it('should interpolate route vars in the redirected path from original search', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
+ $routeProvider.when('/foo/:id/:extra', {redirectTo: '/bar/:id/:subid/99'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $location.path('/foo/id3/eId').search('subid=sid1&appended=true');
+ $rootScope.$digest();
+
+ expect($location.path()).toEqual('/bar/id3/sid1/99');
+ expect($location.search()).toEqual({appended: 'true', extra: 'eId'});
+ expect($route.current.templateUrl).toEqual('bar.html');
+ });
+ });
+
+
+ it('should allow custom redirectTo function to be used', function() {
+ function customRedirectFn(routePathParams, path, search) {
+ expect(routePathParams).toEqual({id: 'id3'});
+ expect(path).toEqual('/foo/id3');
+ expect(search).toEqual({ subid: 'sid1', appended: 'true' });
+ return '/custom';
+ }
+
+ module(function($routeProvider){
+ $routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
+ $routeProvider.when('/foo/:id', {redirectTo: customRedirectFn});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $location.path('/foo/id3').search('subid=sid1&appended=true');
+ $rootScope.$digest();
+
+ expect($location.path()).toEqual('/custom');
+ });
+ });
+
+
+ it('should replace the url when redirecting', function() {
+ module(function($routeProvider) {
+ $routeProvider.when('/bar/:id', {templateUrl: 'bar.html'});
+ $routeProvider.when('/foo/:id/:extra', {redirectTo: '/bar/:id'});
+ });
+ inject(function($browser, $route, $location, $rootScope) {
+ var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
+
+ $location.path('/foo/id3/eId');
+ $rootScope.$digest();
+
+ expect($location.path()).toEqual('/bar/id3');
+ expect($browserUrl.mostRecentCall.args)
+ .toEqual(['http://server/#/bar/id3?extra=eId', true]);
+ });
+ });
+ });
+
+
+ describe('reloadOnSearch', function() {
+ it('should reload a route when reloadOnSearch is enabled and .search() changes', function() {
+ var reloaded = jasmine.createSpy('route reload');
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {controller: noop});
+ });
+
+ inject(function($route, $location, $rootScope, $routeParams) {
+ $rootScope.$on('$routeChangeStart', reloaded);
+ $location.path('/foo');
+ $rootScope.$digest();
+ expect(reloaded).toHaveBeenCalled();
+ expect($routeParams).toEqual({});
+ reloaded.reset();
+
+ // trigger reload
+ $location.search({foo: 'bar'});
+ $rootScope.$digest();
+ expect(reloaded).toHaveBeenCalled();
+ expect($routeParams).toEqual({foo:'bar'});
+ });
+ });
+
+
+ it('should not reload a route when reloadOnSearch is disabled and only .search() changes', function() {
+ var routeChange = jasmine.createSpy('route change'),
+ routeUpdate = jasmine.createSpy('route update');
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {controller: noop, reloadOnSearch: false});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $rootScope.$on('$routeChangeStart', routeChange);
+ $rootScope.$on('$routeChangeSuccess', routeChange);
+ $rootScope.$on('$routeUpdate', routeUpdate);
+
+ expect(routeChange).not.toHaveBeenCalled();
+
+ $location.path('/foo');
+ $rootScope.$digest();
+ expect(routeChange).toHaveBeenCalled();
+ expect(routeChange.callCount).toBe(2);
+ expect(routeUpdate).not.toHaveBeenCalled();
+ routeChange.reset();
+
+ // don't trigger reload
+ $location.search({foo: 'bar'});
+ $rootScope.$digest();
+ expect(routeChange).not.toHaveBeenCalled();
+ expect(routeUpdate).toHaveBeenCalled();
+ });
+ });
+
+
+ it('should reload reloadOnSearch route when url differs only in route path param', function() {
+ var routeChange = jasmine.createSpy('route change');
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo/:fooId', {controller: noop, reloadOnSearch: false});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $rootScope.$on('$routeChangeStart', routeChange);
+ $rootScope.$on('$routeChangeSuccess', routeChange);
+
+ expect(routeChange).not.toHaveBeenCalled();
+
+ $location.path('/foo/aaa');
+ $rootScope.$digest();
+ expect(routeChange).toHaveBeenCalled();
+ expect(routeChange.callCount).toBe(2);
+ routeChange.reset();
+
+ $location.path('/foo/bbb');
+ $rootScope.$digest();
+ expect(routeChange).toHaveBeenCalled();
+ expect(routeChange.callCount).toBe(2);
+ routeChange.reset();
+
+ $location.search({foo: 'bar'});
+ $rootScope.$digest();
+ expect(routeChange).not.toHaveBeenCalled();
+ });
+ });
+
+
+ it('should update params when reloadOnSearch is disabled and .search() changes', function() {
+ var routeParamsWatcher = jasmine.createSpy('routeParamsWatcher');
+
+ module(function($routeProvider) {
+ $routeProvider.when('/foo', {controller: noop});
+ $routeProvider.when('/bar/:barId', {controller: noop, reloadOnSearch: false});
+ });
+
+ inject(function($route, $location, $rootScope, $routeParams) {
+ $rootScope.$watch(function() {
+ return $routeParams;
+ }, function(value) {
+ routeParamsWatcher(value);
+ }, true);
+
+ expect(routeParamsWatcher).not.toHaveBeenCalled();
+
+ $location.path('/foo');
+ $rootScope.$digest();
+ expect(routeParamsWatcher).toHaveBeenCalledWith({});
+ routeParamsWatcher.reset();
+
+ // trigger reload
+ $location.search({foo: 'bar'});
+ $rootScope.$digest();
+ expect(routeParamsWatcher).toHaveBeenCalledWith({foo: 'bar'});
+ routeParamsWatcher.reset();
+
+ $location.path('/bar/123').search({});
+ $rootScope.$digest();
+ expect(routeParamsWatcher).toHaveBeenCalledWith({barId: '123'});
+ routeParamsWatcher.reset();
+
+ // don't trigger reload
+ $location.search({foo: 'bar'});
+ $rootScope.$digest();
+ expect(routeParamsWatcher).toHaveBeenCalledWith({barId: '123', foo: 'bar'});
+ });
+ });
+
+
+ it('should allow using a function as a template', function() {
+ var customTemplateWatcher = jasmine.createSpy('customTemplateWatcher');
+
+ function customTemplateFn(routePathParams) {
+ customTemplateWatcher(routePathParams);
+ expect(routePathParams).toEqual({id: 'id3'});
+ return '<h1>' + routePathParams.id + '</h1>';
+ }
+
+ module(function($routeProvider){
+ $routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
+ $routeProvider.when('/foo/:id', {template: customTemplateFn});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $location.path('/foo/id3');
+ $rootScope.$digest();
+
+ expect(customTemplateWatcher).toHaveBeenCalledWith({id: 'id3'});
+ });
+ });
+
+
+ it('should allow using a function as a templateUrl', function() {
+ var customTemplateUrlWatcher = jasmine.createSpy('customTemplateUrlWatcher');
+
+ function customTemplateUrlFn(routePathParams) {
+ customTemplateUrlWatcher(routePathParams);
+ expect(routePathParams).toEqual({id: 'id3'});
+ return 'foo.html';
+ }
+
+ module(function($routeProvider){
+ $routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
+ $routeProvider.when('/foo/:id', {templateUrl: customTemplateUrlFn});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ $location.path('/foo/id3');
+ $rootScope.$digest();
+
+ expect(customTemplateUrlWatcher).toHaveBeenCalledWith({id: 'id3'});
+ expect($route.current.loadedTemplateUrl).toEqual('foo.html');
+ });
+ });
+
+
+ describe('reload', function() {
+
+ it('should reload even if reloadOnSearch is false', function() {
+ var routeChangeSpy = jasmine.createSpy('route change');
+
+ module(function($routeProvider) {
+ $routeProvider.when('/bar/:barId', {controller: angular.noop, reloadOnSearch: false});
+ });
+
+ inject(function($route, $location, $rootScope, $routeParams) {
+ $rootScope.$on('$routeChangeSuccess', routeChangeSpy);
+
+ $location.path('/bar/123');
+ $rootScope.$digest();
+ expect($routeParams).toEqual({barId:'123'});
+ expect(routeChangeSpy).toHaveBeenCalledOnce();
+ routeChangeSpy.reset();
+
+ $location.path('/bar/123').search('a=b');
+ $rootScope.$digest();
+ expect($routeParams).toEqual({barId:'123', a:'b'});
+ expect(routeChangeSpy).not.toHaveBeenCalled();
+
+ $route.reload();
+ $rootScope.$digest();
+ expect($routeParams).toEqual({barId:'123', a:'b'});
+ expect(routeChangeSpy).toHaveBeenCalledOnce();
+ });
+ });
+ });
+ });
+});