diff options
| -rw-r--r-- | test/ng/routeSpec.js | 121 |
1 files changed, 88 insertions, 33 deletions
diff --git a/test/ng/routeSpec.js b/test/ng/routeSpec.js index 1f714f62..c2651286 100644 --- a/test/ng/routeSpec.js +++ b/test/ng/routeSpec.js @@ -285,53 +285,108 @@ describe('$route', function() { }); - it('should handle unknown routes with "otherwise" route definition', function() { - function NotFoundCtrl() {} - + it('should chain whens and otherwise', function() { module(function($routeProvider){ - $routeProvider.when('/foo', {templateUrl: 'foo.html'}); - $routeProvider.otherwise({templateUrl: '404.html', controller: NotFoundCtrl}); + $routeProvider.when('/foo', {templateUrl: 'foo.html'}). + otherwise({templateUrl: 'bar.html'}). + when('/baz', {templateUrl: 'baz.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('/unknownRoute'); $rootScope.$digest(); + expect($route.current.templateUrl).toBe('bar.html'); - expect($route.current.templateUrl).toBe('404.html'); - expect($route.current.controller).toBe(NotFoundCtrl); - expect(onChangeSpy).toHaveBeenCalled(); - - onChangeSpy.reset(); - $location.path('/foo'); + $location.url('/baz'); $rootScope.$digest(); - - expect($route.current.templateUrl).toEqual('foo.html'); - expect($route.current.controller).toBeUndefined(); - expect(onChangeSpy).toHaveBeenCalled(); + expect($route.current.templateUrl).toBe('baz.html'); }); }); - 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'}); + 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(); + }); }); - 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'); + 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(); + }); }); }); |
