diff options
| author | Misko Hevery | 2012-02-15 15:45:07 -0800 |
|---|---|---|
| committer | Vojta Jina | 2012-02-28 17:46:58 -0800 |
| commit | f16bd2f747ed94547eabdc4c337775a22a365255 (patch) | |
| tree | bffa267eec9e90a8f1bc12c0063402c4c021d685 /test/service/routeSpec.js | |
| parent | ef7346ff70c745178d5c615e6ae5e559a4549f32 (diff) | |
| download | angular.js-f16bd2f747ed94547eabdc4c337775a22a365255.tar.bz2 | |
refactor($route): move when/otherwise to provider
Diffstat (limited to 'test/service/routeSpec.js')
| -rw-r--r-- | test/service/routeSpec.js | 677 |
1 files changed, 369 insertions, 308 deletions
diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index d6f7d753..6c6828bc 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -1,7 +1,7 @@ 'use strict'; describe('$route', function() { - it('should route and fire change event', inject(function($route, $location, $rootScope) { + it('should route and fire change event', function() { var log = '', lastRoute, nextRoute; @@ -9,367 +9,415 @@ describe('$route', function() { function BookChapter() { log += '<init>;'; } - - $route.when('/Book/:book/Chapter/:chapter', - {controller: BookChapter, template: 'Chapter.html'}); - $route.when('/Blank'); - $rootScope.$on('$beforeRouteChange', function(event, next, current) { - log += 'before();'; - expect(current).toBe($route.current); - lastRoute = current; - nextRoute = next; - }); - $rootScope.$on('$afterRouteChange', function(event, current, last) { - log += 'after();'; - expect(current).toBe($route.current); - expect(lastRoute).toBe(last); - expect(nextRoute).toBe(current); + module(function($routeProvider) { + $routeProvider.when('/Book/:book/Chapter/:chapter', + {controller: BookChapter, template: 'Chapter.html'}); + $routeProvider.when('/Blank'); }); + inject(function($route, $location, $rootScope) { + $rootScope.$on('$beforeRouteChange', function(event, next, current) { + log += 'before();'; + expect(current).toBe($route.current); + lastRoute = current; + nextRoute = next; + }); + $rootScope.$on('$afterRouteChange', 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(); - expect(log).toEqual('before();<init>;after();'); - expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'}); - var lastId = $route.current.scope.$id; - - log = ''; - $location.path('/Blank').search('ignore'); - $rootScope.$digest(); - expect(log).toEqual('before();after();'); - expect($route.current.params).toEqual({ignore:true}); - expect($route.current.scope.$id).not.toEqual(lastId); + $location.path('/Book/Moby/Chapter/Intro').search('p=123'); + $rootScope.$digest(); + expect(log).toEqual('before();<init>;after();'); + expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'}); + var lastId = $route.current.scope.$id; - log = ''; - $location.path('/NONE'); - $rootScope.$digest(); - expect(log).toEqual('before();after();'); - expect($route.current).toEqual(null); + log = ''; + $location.path('/Blank').search('ignore'); + $rootScope.$digest(); + expect(log).toEqual('before();after();'); + expect($route.current.params).toEqual({ignore:true}); + expect($route.current.scope.$id).not.toEqual(lastId); - $route.when('/NONE', {template:'instant update'}); - $rootScope.$digest(); - expect($route.current.template).toEqual('instant update'); - })); + log = ''; + $location.path('/NONE'); + $rootScope.$digest(); + expect(log).toEqual('before();after();'); + expect($route.current).toEqual(null); + }); + }); - it('should match a route that contains special chars in the path', - inject(function($route, $location, $rootScope) { - $route.when('/$test.23/foo(bar)/:baz', {template: 'test.html'}); + it('should match a route that contains special chars in the path', function() { + module(function($routeProvider) { + $routeProvider.when('/$test.23/foo(bar)/:baz', {template: 'test.html'}); + }); + inject(function($route, $location, $rootScope) { - $location.path('/test'); - $rootScope.$digest(); - expect($route.current).toBeUndefined(); + $location.path('/test'); + $rootScope.$digest(); + expect($route.current).toBeUndefined(); - $location.path('/$testX23/foo(bar)/222'); - $rootScope.$digest(); - expect($route.current).toBeUndefined(); + $location.path('/$testX23/foo(bar)/222'); + $rootScope.$digest(); + expect($route.current).toBeUndefined(); - $location.path('/$test.23/foo(bar)/222'); - $rootScope.$digest(); - expect($route.current).toBeDefined(); + $location.path('/$test.23/foo(bar)/222'); + $rootScope.$digest(); + expect($route.current).toBeDefined(); - $location.path('/$test.23/foo\\(bar)/222'); - $rootScope.$digest(); - expect($route.current).toBeUndefined(); - })); + $location.path('/$test.23/foo\\(bar)/222'); + $rootScope.$digest(); + expect($route.current).toBeUndefined(); + }); + }); - it('should change route even when only search param changes', - inject(function($route, $location, $rootScope) { - var callback = jasmine.createSpy('onRouteChange'); + it('should change route even when only search param changes', function() { + module(function($routeProvider) { + $routeProvider.when('/test', {template: 'test.html'}); + }); - $route.when('/test', {template: 'test.html'}); - $rootScope.$on('$beforeRouteChange', callback); - $location.path('/test'); - $rootScope.$digest(); - callback.reset(); + inject(function($route, $location, $rootScope) { + var callback = jasmine.createSpy('onRouteChange'); - $location.search({any: true}); - $rootScope.$digest(); + $rootScope.$on('$beforeRouteChange', callback); + $location.path('/test'); + $rootScope.$digest(); + callback.reset(); - expect(callback).toHaveBeenCalled(); - })); + $location.search({any: true}); + $rootScope.$digest(); + expect(callback).toHaveBeenCalled(); + }); + }); - it('should allow routes to be defined with just templates without controllers', - inject(function($route, $location, $rootScope) { - var onChangeSpy = jasmine.createSpy('onChange'); - $route.when('/foo', {template: 'foo.html'}); - $rootScope.$on('$beforeRouteChange', onChangeSpy); - expect($route.current).toBeUndefined(); - expect(onChangeSpy).not.toHaveBeenCalled(); + it('should allow routes to be defined with just templates without controllers', function() { + module(function($routeProvider) { + $routeProvider.when('/foo', {template: 'foo.html'}); + }); - $location.path('/foo'); - $rootScope.$digest(); + inject(function($route, $location, $rootScope) { + var onChangeSpy = jasmine.createSpy('onChange'); - expect($route.current.template).toEqual('foo.html'); - expect($route.current.controller).toBeUndefined(); - expect(onChangeSpy).toHaveBeenCalled(); - })); + $rootScope.$on('$beforeRouteChange', onChangeSpy); + expect($route.current).toBeUndefined(); + expect(onChangeSpy).not.toHaveBeenCalled(); + $location.path('/foo'); + $rootScope.$digest(); - it('should handle unknown routes with "otherwise" route definition', - inject(function($route, $location, $rootScope) { - var onChangeSpy = jasmine.createSpy('onChange'); + expect($route.current.template).toEqual('foo.html'); + expect($route.current.controller).toBeUndefined(); + expect(onChangeSpy).toHaveBeenCalled(); + }); + }); - function NotFoundCtrl($scope) {$scope.notFoundProp = 'not found!';} - $route.when('/foo', {template: 'foo.html'}); - $route.otherwise({template: '404.html', controller: NotFoundCtrl}); - $rootScope.$on('$beforeRouteChange', onChangeSpy); - expect($route.current).toBeUndefined(); - expect(onChangeSpy).not.toHaveBeenCalled(); + it('should handle unknown routes with "otherwise" route definition', function() { + function NotFoundCtrl($scope) { + $scope.notFoundProp = 'not found!'; + } - $location.path('/unknownRoute'); - $rootScope.$digest(); + module(function($routeProvider){ + $routeProvider.when('/foo', {template: 'foo.html'}); + $routeProvider.otherwise({template: '404.html', controller: NotFoundCtrl}); + }); - expect($route.current.template).toBe('404.html'); - expect($route.current.controller).toBe(NotFoundCtrl); - expect($route.current.scope.notFoundProp).toBe('not found!'); - expect(onChangeSpy).toHaveBeenCalled(); + inject(function($route, $location, $rootScope) { + var onChangeSpy = jasmine.createSpy('onChange'); - onChangeSpy.reset(); - $location.path('/foo'); - $rootScope.$digest(); + $rootScope.$on('$beforeRouteChange', onChangeSpy); + expect($route.current).toBeUndefined(); + expect(onChangeSpy).not.toHaveBeenCalled(); - expect($route.current.template).toEqual('foo.html'); - expect($route.current.controller).toBeUndefined(); - expect($route.current.scope.notFoundProp).toBeUndefined(); - expect(onChangeSpy).toHaveBeenCalled(); - })); + $location.path('/unknownRoute'); + $rootScope.$digest(); + expect($route.current.template).toBe('404.html'); + expect($route.current.controller).toBe(NotFoundCtrl); + expect($route.current.scope.notFoundProp).toBe('not found!'); + expect(onChangeSpy).toHaveBeenCalled(); - it('should $destroy old routes', inject(function($route, $location, $rootScope) { - $route.when('/foo', {template: 'foo.html', controller: function() {this.name = 'FOO';}}); - $route.when('/bar', {template: 'bar.html', controller: function() {this.name = 'BAR';}}); - $route.when('/baz', {template: 'baz.html'}); + onChangeSpy.reset(); + $location.path('/foo'); + $rootScope.$digest(); - expect($rootScope.$childHead).toEqual(null); + expect($route.current.template).toEqual('foo.html'); + expect($route.current.controller).toBeUndefined(); + expect($route.current.scope.notFoundProp).toBeUndefined(); + expect(onChangeSpy).toHaveBeenCalled(); + }); + }); - $location.path('/foo'); - $rootScope.$digest(); - expect($rootScope.$$childHead.$id).toBeTruthy(); - expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id); - $location.path('/bar'); - $rootScope.$digest(); - expect($rootScope.$$childHead.$id).toBeTruthy(); - expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id); + it('should $destroy old routes', function() { + module(function($routeProvider) { + $routeProvider.when('/foo', {template: 'foo.html', controller: function() {this.name = 'FOO';}}); + $routeProvider.when('/bar', {template: 'bar.html', controller: function() {this.name = 'BAR';}}); + $routeProvider.when('/baz', {template: 'baz.html'}); + }); - $location.path('/baz'); - $rootScope.$digest(); - expect($rootScope.$$childHead.$id).toBeTruthy(); - expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id); + inject(function($route, $location, $rootScope) { + expect($rootScope.$childHead).toEqual(null); - $location.path('/'); - $rootScope.$digest(); - expect($rootScope.$$childHead).toEqual(null); - expect($rootScope.$$childTail).toEqual(null); - })); + $location.path('/foo'); + $rootScope.$digest(); + expect($rootScope.$$childHead.$id).toBeTruthy(); + expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id); + $location.path('/bar'); + $rootScope.$digest(); + expect($rootScope.$$childHead.$id).toBeTruthy(); + expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id); - it('should infer arguments in injection', inject(function($route, $location, $rootScope) { - var injectedRoute; - $route.when('/test', {controller: function($route) {injectedRoute = $route;}}); - $location.path('/test'); - $rootScope.$digest(); - expect(injectedRoute).toBe($route); - })); + $location.path('/baz'); + $rootScope.$digest(); + expect($rootScope.$$childHead.$id).toBeTruthy(); + expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id); + $location.path('/'); + $rootScope.$digest(); + expect($rootScope.$$childHead).toEqual(null); + expect($rootScope.$$childTail).toEqual(null); + }); + }); - describe('redirection', function() { - it('should support redirection via redirectTo property by updating $location', - inject(function($route, $location, $rootScope) { - var onChangeSpy = jasmine.createSpy('onChange'); - $route.when('/', {redirectTo: '/foo'}); - $route.when('/foo', {template: 'foo.html'}); - $route.when('/bar', {template: 'bar.html'}); - $route.when('/baz', {redirectTo: '/bar'}); - $route.otherwise({template: '404.html'}); - $rootScope.$on('$beforeRouteChange', onChangeSpy); - expect($route.current).toBeUndefined(); - expect(onChangeSpy).not.toHaveBeenCalled(); + it('should infer arguments in injection', function() { + var injectedRoute; + module(function($routeProvider) { + $routeProvider.when('/test', {controller: function($route) {injectedRoute = $route;}}); + }); - $location.path('/'); + inject(function($route, $location, $rootScope) { + $location.path('/test'); $rootScope.$digest(); - expect($location.path()).toBe('/foo'); - expect($route.current.template).toBe('foo.html'); - expect(onChangeSpy.callCount).toBe(2); + expect(injectedRoute).toBe($route); + }); + }); - onChangeSpy.reset(); - $location.path('/baz'); - $rootScope.$digest(); - expect($location.path()).toBe('/bar'); - expect($route.current.template).toBe('bar.html'); - expect(onChangeSpy.callCount).toBe(2); - })); + describe('redirection', function() { + it('should support redirection via redirectTo property by updating $location', function() { + module(function($routeProvider) { + $routeProvider.when('/', {redirectTo: '/foo'}); + $routeProvider.when('/foo', {template: 'foo.html'}); + $routeProvider.when('/bar', {template: 'bar.html'}); + $routeProvider.when('/baz', {redirectTo: '/bar'}); + $routeProvider.otherwise({template: '404.html'}); + }); - it('should interpolate route vars in the redirected path from original path', - inject(function($route, $location, $rootScope) { - $route.when('/foo/:id/foo/:subid/:extraId', {redirectTo: '/bar/:id/:subid/23'}); - $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); + inject(function($route, $location, $rootScope) { + var onChangeSpy = jasmine.createSpy('onChange'); - $location.path('/foo/id1/foo/subid3/gah'); - $rootScope.$digest(); + $rootScope.$on('$beforeRouteChange', onChangeSpy); + expect($route.current).toBeUndefined(); + expect(onChangeSpy).not.toHaveBeenCalled(); - expect($location.path()).toEqual('/bar/id1/subid3/23'); - expect($location.search()).toEqual({extraId: 'gah'}); - expect($route.current.template).toEqual('bar.html'); - })); + $location.path('/'); + $rootScope.$digest(); + expect($location.path()).toBe('/foo'); + expect($route.current.template).toBe('foo.html'); + expect(onChangeSpy.callCount).toBe(2); + onChangeSpy.reset(); + $location.path('/baz'); + $rootScope.$digest(); + expect($location.path()).toBe('/bar'); + expect($route.current.template).toBe('bar.html'); + expect(onChangeSpy.callCount).toBe(2); + }); + }); - it('should interpolate route vars in the redirected path from original search', - inject(function($route, $location, $rootScope) { - $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); - $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id/:subid/99'}); - $location.path('/foo/id3/eId').search('subid=sid1&appended=true'); - $rootScope.$digest(); + 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', {template: 'bar.html'}); + }); - expect($location.path()).toEqual('/bar/id3/sid1/99'); - expect($location.search()).toEqual({appended: 'true', extra: 'eId'}); - expect($route.current.template).toEqual('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.template).toEqual('bar.html'); + }); + }); - it('should allow custom redirectTo function to be used', - inject(function($route, $location, $rootScope) { - $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); - $route.when('/foo/:id', {redirectTo: customRedirectFn}); - $location.path('/foo/id3').search('subid=sid1&appended=true'); - $rootScope.$digest(); + it('should interpolate route vars in the redirected path from original search', function() { + module(function($routeProvider) { + $routeProvider.when('/bar/:id/:subid/:subsubid', {template: '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.template).toEqual('bar.html'); + }); + }); - expect($location.path()).toEqual('/custom'); + it('should allow custom redirectTo function to be used', function() { function customRedirectFn(routePathParams, path, search) { expect(routePathParams).toEqual({id: 'id3'}); - expect(path).toEqual($location.path()); - expect(search).toEqual($location.search()); + expect(path).toEqual('/foo/id3'); + expect(search).toEqual({ subid: 'sid1', appended: 'true' }); return '/custom'; } - })); + module(function($routeProvider){ + $routeProvider.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); + $routeProvider.when('/foo/:id', {redirectTo: customRedirectFn}); + }); - it('should replace the url when redirecting', inject(function($route, $location, $rootScope) { - $route.when('/bar/:id', {template: 'bar.html'}); - $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id'}); + inject(function($route, $location, $rootScope) { + $location.path('/foo/id3').search('subid=sid1&appended=true'); + $rootScope.$digest(); - var replace; - $rootScope.$watch(function() { - if (isUndefined(replace)) replace = $location.$$replace; + expect($location.path()).toEqual('/custom'); }); + }); - $location.path('/foo/id3/eId'); - $rootScope.$digest(); - expect($location.path()).toEqual('/bar/id3'); - expect(replace).toBe(true); - })); + it('should replace the url when redirecting', function() { + module(function($routeProvider) { + $routeProvider.when('/bar/:id', {template: 'bar.html'}); + $routeProvider.when('/foo/:id/:extra', {redirectTo: '/bar/:id'}); + }); + inject(function($route, $location, $rootScope) { + var replace; + $rootScope.$watch(function() { + if (isUndefined(replace)) replace = $location.$$replace; + }); + + $location.path('/foo/id3/eId'); + $rootScope.$digest(); + + expect($location.path()).toEqual('/bar/id3'); + expect(replace).toBe(true); + }); + }); }); describe('reloadOnSearch', function() { - it('should reload a route when reloadOnSearch is enabled and .search() changes', - inject(function($route, $location, $rootScope, $routeParams) { + it('should reload a route when reloadOnSearch is enabled and .search() changes', function() { var reloaded = jasmine.createSpy('route reload'); - $route.when('/foo', {controller: FooCtrl}); - $rootScope.$on('$beforeRouteChange', reloaded); - function FooCtrl() { reloaded(); } - $location.path('/foo'); - $rootScope.$digest(); - expect(reloaded).toHaveBeenCalled(); - expect($routeParams).toEqual({}); - reloaded.reset(); + module(function($routeProvider) { + $routeProvider.when('/foo', {controller: FooCtrl}); + }); - // trigger reload - $location.search({foo: 'bar'}); - $rootScope.$digest(); - expect(reloaded).toHaveBeenCalled(); - expect($routeParams).toEqual({foo:'bar'}); - })); + inject(function($route, $location, $rootScope, $routeParams) { + $rootScope.$on('$beforeRouteChange', 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', - inject(function($route, $location, $rootScope) { + + it('should not reload a route when reloadOnSearch is disabled and only .search() changes', function() { var reloaded = jasmine.createSpy('route reload'), routeUpdateEvent = jasmine.createSpy('route reload'); - $route.when('/foo', {controller: FooCtrl, reloadOnSearch: false}); - $rootScope.$on('$beforeRouteChange', reloaded); - function FooCtrl($scope) { reloaded(); $scope.$on('$routeUpdate', routeUpdateEvent); } - expect(reloaded).not.toHaveBeenCalled(); + module(function($routeProvider) { + $routeProvider.when('/foo', {controller: FooCtrl, reloadOnSearch: false}); + }); - $location.path('/foo'); - $rootScope.$digest(); - expect(reloaded).toHaveBeenCalled(); - expect(routeUpdateEvent).not.toHaveBeenCalled(); - reloaded.reset(); + inject(function($route, $location, $rootScope) { + $rootScope.$on('$beforeRouteChange', reloaded); - // don't trigger reload - $location.search({foo: 'bar'}); - $rootScope.$digest(); - expect(reloaded).not.toHaveBeenCalled(); - expect(routeUpdateEvent).toHaveBeenCalled(); - })); + expect(reloaded).not.toHaveBeenCalled(); + $location.path('/foo'); + $rootScope.$digest(); + expect(reloaded).toHaveBeenCalled(); + expect(routeUpdateEvent).not.toHaveBeenCalled(); + reloaded.reset(); - it('should reload reloadOnSearch route when url differs only in route path param', - inject(function($route, $location, $rootScope) { + // don't trigger reload + $location.search({foo: 'bar'}); + $rootScope.$digest(); + expect(reloaded).not.toHaveBeenCalled(); + expect(routeUpdateEvent).toHaveBeenCalled(); + }); + }); + + + it('should reload reloadOnSearch route when url differs only in route path param', function() { var reloaded = jasmine.createSpy('routeReload'), onRouteChange = jasmine.createSpy('onRouteChange'); - $route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false}); - $rootScope.$on('$beforeRouteChange', onRouteChange); - function FooCtrl() { reloaded(); } - expect(reloaded).not.toHaveBeenCalled(); - expect(onRouteChange).not.toHaveBeenCalled(); + module(function($routeProvider) { + $routeProvider.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false}); + }); - $location.path('/foo/aaa'); - $rootScope.$digest(); - expect(reloaded).toHaveBeenCalled(); - expect(onRouteChange).toHaveBeenCalled(); - reloaded.reset(); - onRouteChange.reset(); + inject(function($route, $location, $rootScope) { + $rootScope.$on('$beforeRouteChange', onRouteChange); - $location.path('/foo/bbb'); - $rootScope.$digest(); - expect(reloaded).toHaveBeenCalled(); - expect(onRouteChange).toHaveBeenCalled(); - reloaded.reset(); - onRouteChange.reset(); + expect(reloaded).not.toHaveBeenCalled(); + expect(onRouteChange).not.toHaveBeenCalled(); - $location.search({foo: 'bar'}); - $rootScope.$digest(); - expect(reloaded).not.toHaveBeenCalled(); - expect(onRouteChange).not.toHaveBeenCalled(); - })); + $location.path('/foo/aaa'); + $rootScope.$digest(); + expect(reloaded).toHaveBeenCalled(); + expect(onRouteChange).toHaveBeenCalled(); + reloaded.reset(); + onRouteChange.reset(); + $location.path('/foo/bbb'); + $rootScope.$digest(); + expect(reloaded).toHaveBeenCalled(); + expect(onRouteChange).toHaveBeenCalled(); + reloaded.reset(); + onRouteChange.reset(); - it('should update params when reloadOnSearch is disabled and .search() changes', - inject(function($route, $location, $rootScope) { - var routeParams = jasmine.createSpy('routeParams'); + $location.search({foo: 'bar'}); + $rootScope.$digest(); + expect(reloaded).not.toHaveBeenCalled(); + expect(onRouteChange).not.toHaveBeenCalled(); + }); + }); - $route.when('/foo', {controller: FooCtrl}); - $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); - function FooCtrl($scope) { + it('should update params when reloadOnSearch is disabled and .search() changes', function() { + var routeParams = jasmine.createSpy('routeParams'); + + function FooCtrl($scope, $route) { $scope.$watch(function() { return $route.current.params; }, function(value) { @@ -377,33 +425,39 @@ describe('$route', function() { }); } - expect(routeParams).not.toHaveBeenCalled(); + module(function($routeProvider) { + $routeProvider.when('/foo', {controller: FooCtrl}); + $routeProvider.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); + }); - $location.path('/foo'); - $rootScope.$digest(); - expect(routeParams).toHaveBeenCalledWith({}); - routeParams.reset(); + inject(function($route, $location, $rootScope) { + expect(routeParams).not.toHaveBeenCalled(); - // trigger reload - $location.search({foo: 'bar'}); - $rootScope.$digest(); - expect(routeParams).toHaveBeenCalledWith({foo: 'bar'}); - routeParams.reset(); + $location.path('/foo'); + $rootScope.$digest(); + expect(routeParams).toHaveBeenCalledWith({}); + routeParams.reset(); - $location.path('/bar/123').search({}); - $rootScope.$digest(); - expect(routeParams).toHaveBeenCalledWith({barId: '123'}); - routeParams.reset(); + // trigger reload + $location.search({foo: 'bar'}); + $rootScope.$digest(); + expect(routeParams).toHaveBeenCalledWith({foo: 'bar'}); + routeParams.reset(); - // don't trigger reload - $location.search({foo: 'bar'}); - $rootScope.$digest(); - expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'}); - })); + $location.path('/bar/123').search({}); + $rootScope.$digest(); + expect(routeParams).toHaveBeenCalledWith({barId: '123'}); + routeParams.reset(); + + // don't trigger reload + $location.search({foo: 'bar'}); + $rootScope.$digest(); + expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'}); + }); + }); - it('should $destroy scope after update and reload', - inject(function($route, $location, $rootScope) { + 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 = []; @@ -422,48 +476,55 @@ describe('$route', function() { }; } - $route.when('/foo', {controller: createController('foo'), reloadOnSearch: false}); - $route.when('/bar', {controller: createController('bar')}); + module(function($routeProvider) { + $routeProvider.when('/foo', {controller: createController('foo'), reloadOnSearch: false}); + $routeProvider.when('/bar', {controller: createController('bar')}); + }); - $location.url('/foo'); - $rootScope.$digest(); - expect(log).toEqual(['init-foo']); + inject(function($route, $location, $rootScope) { + $location.url('/foo'); + $rootScope.$digest(); + expect(log).toEqual(['init-foo']); - $location.search({q: 'some'}); - $rootScope.$digest(); - expect(log).toEqual(['init-foo', 'route-update']); + $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']); - })); + $location.url('/bar'); + $rootScope.$digest(); + expect(log).toEqual(['init-foo', 'route-update', 'destroy-foo', 'init-bar']); + }); + }); describe('reload', function() { - it('should reload even if reloadOnSearch is false', - inject(function($route, $location, $rootScope, $routeParams) { + it('should reload even if reloadOnSearch is false', function() { var count = 0; - $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); - function FooCtrl() { count ++; } - $location.path('/bar/123'); - $rootScope.$digest(); - expect($routeParams).toEqual({barId:'123'}); - expect(count).toEqual(1); - - $location.path('/bar/123').search('a=b'); - $rootScope.$digest(); - expect($routeParams).toEqual({barId:'123', a:'b'}); - expect(count).toEqual(1); + module(function($routeProvider) { + $routeProvider.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); + }); - $route.reload(); - $rootScope.$digest(); - expect($routeParams).toEqual({barId:'123', a:'b'}); - expect(count).toEqual(2); - })); + inject(function($route, $location, $rootScope, $routeParams) { + $location.path('/bar/123'); + $rootScope.$digest(); + expect($routeParams).toEqual({barId:'123'}); + expect(count).toEqual(1); + + $location.path('/bar/123').search('a=b'); + $rootScope.$digest(); + expect($routeParams).toEqual({barId:'123', a:'b'}); + expect(count).toEqual(1); + + $route.reload(); + $rootScope.$digest(); + expect($routeParams).toEqual({barId:'123', a:'b'}); + expect(count).toEqual(2); + }); + }); }); }); }); |
