diff options
| author | Vojta Jina | 2011-07-12 02:18:17 +0200 |
|---|---|---|
| committer | Vojta Jina | 2011-09-08 23:00:59 +0200 |
| commit | 22cb600280cecabf719ba1878719c907aa01ba18 (patch) | |
| tree | 3c2247033a6d722e3b4eea0720ab466589ea247e /test/service/routeSpec.js | |
| parent | 5ba227c7cd3ddfcd3bffc3fd15daf8d6ec9b8713 (diff) | |
| download | angular.js-22cb600280cecabf719ba1878719c907aa01ba18.tar.bz2 | |
fix($route): update $route to reflect $location changes
* update $route to reflect new $location
* add some more unit tests to $route
* fix some other failing unit tests
* redirect overrides the url now
Breaks $route custom redirect fn has only 3 params now
Diffstat (limited to 'test/service/routeSpec.js')
| -rw-r--r-- | test/service/routeSpec.js | 234 |
1 files changed, 96 insertions, 138 deletions
diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index b1dde915..63ce4663 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -1,30 +1,24 @@ 'use strict'; describe('$route', function() { - var scope; + var scope, $route, $location; beforeEach(function(){ scope = angular.scope(); - }); - - - afterEach(function(){ - dealoc(scope); + $location = scope.$service('$location'); + $route = scope.$service('$route'); }); it('should route and fire change event', function(){ var log = '', - $location, $route, lastRoute, nextRoute; function BookChapter() { log += '<init>;'; } - scope = compile('<div></div>')(); - $location = scope.$service('$location'); - $route = scope.$service('$route'); + $route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'}); $route.when('/Blank'); scope.$on('$beforeRouteChange', function(event, next, current){ @@ -40,21 +34,21 @@ describe('$route', function() { expect(nextRoute).toBe(current); }); - $location.update('http://server#/Book/Moby/Chapter/Intro?p=123'); + $location.path('/Book/Moby/Chapter/Intro').search('p=123'); scope.$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.update('http://server#/Blank?ignore'); + $location.path('/Blank').search('ignore'); scope.$digest(); expect(log).toEqual('before();after();'); expect($route.current.params).toEqual({ignore:true}); expect($route.current.scope.$id).not.toEqual(lastId); log = ''; - $location.update('http://server#/NONE'); + $location.path('/NONE'); scope.$digest(); expect(log).toEqual('before();after();'); expect($route.current).toEqual(null); @@ -64,19 +58,31 @@ describe('$route', function() { expect($route.current.template).toEqual('instant update'); }); + it('should change route even when only search param changes', function() { + var callback = jasmine.createSpy('onRouteChange'); + + $route.when('/test', {template: 'test.html'}); + scope.$on('$beforeRouteChange', callback); + $location.path('/test'); + scope.$digest(); + callback.reset(); + + $location.search({any: true}); + scope.$digest(); + + expect(callback).toHaveBeenCalled(); + }); + it('should allow routes to be defined with just templates without controllers', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'), - onChangeSpy = jasmine.createSpy('onChange'); + var onChangeSpy = jasmine.createSpy('onChange'); $route.when('/foo', {template: 'foo.html'}); scope.$on('$beforeRouteChange', onChangeSpy); expect($route.current).toBeUndefined(); expect(onChangeSpy).not.toHaveBeenCalled(); - $location.updateHash('/foo'); + $location.path('/foo'); scope.$digest(); expect($route.current.template).toEqual('foo.html'); @@ -86,10 +92,7 @@ describe('$route', function() { it('should handle unknown routes with "otherwise" route definition', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'), - onChangeSpy = jasmine.createSpy('onChange'); + var onChangeSpy = jasmine.createSpy('onChange'); function NotFoundCtrl() {this.notFoundProp = 'not found!';} @@ -99,7 +102,7 @@ describe('$route', function() { expect($route.current).toBeUndefined(); expect(onChangeSpy).not.toHaveBeenCalled(); - $location.updateHash('/unknownRoute'); + $location.path('/unknownRoute'); scope.$digest(); expect($route.current.template).toBe('404.html'); @@ -108,7 +111,7 @@ describe('$route', function() { expect(onChangeSpy).toHaveBeenCalled(); onChangeSpy.reset(); - $location.updateHash('/foo'); + $location.path('/foo'); scope.$digest(); expect($route.current.template).toEqual('foo.html'); @@ -118,33 +121,28 @@ describe('$route', function() { }); it('should $destroy old routes', function(){ - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'); - $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'}); expect(scope.$childHead).toEqual(null); - $location.updateHash('/foo'); + $location.path('/foo'); scope.$digest(); expect(scope.$$childHead).toBeTruthy(); expect(scope.$$childHead).toEqual(scope.$$childTail); - $location.updateHash('/bar'); + $location.path('/bar'); scope.$digest(); expect(scope.$$childHead).toBeTruthy(); expect(scope.$$childHead).toEqual(scope.$$childTail); - return - $location.updateHash('/baz'); + $location.path('/baz'); scope.$digest(); expect(scope.$$childHead).toBeTruthy(); expect(scope.$$childHead).toEqual(scope.$$childTail); - $location.updateHash('/'); + $location.path('/'); scope.$digest(); expect(scope.$$childHead).toEqual(null); expect(scope.$$childTail).toEqual(null); @@ -152,15 +150,10 @@ describe('$route', function() { describe('redirection', function() { - it('should support redirection via redirectTo property by updating $location', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $browser = scope.$service('$browser'), - $route = scope.$service('$route'), - onChangeSpy = jasmine.createSpy('onChange'); + var onChangeSpy = jasmine.createSpy('onChange'); - $route.when('', {redirectTo: '/foo'}); + $route.when('/', {redirectTo: '/foo'}); $route.when('/foo', {template: 'foo.html'}); $route.when('/bar', {template: 'bar.html'}); $route.when('/baz', {redirectTo: '/bar'}); @@ -169,108 +162,85 @@ describe('$route', function() { expect($route.current).toBeUndefined(); expect(onChangeSpy).not.toHaveBeenCalled(); - scope.$digest(); //triggers initial route change - match the redirect route - $browser.defer.flush(); //triger route change - match the route we redirected to - - expect($location.hash).toBe('/foo'); - expect($route.current.template).toBe('foo.html'); - expect(onChangeSpy.callCount).toBe(2); - - - onChangeSpy.reset(); - $location.updateHash(''); - scope.$digest(); //match the redirect route + update $browser - $browser.defer.flush(); //match the route we redirected to - - expect($location.hash).toBe('/foo'); + $location.path('/'); + scope.$digest(); + expect($location.path()).toBe('/foo'); expect($route.current.template).toBe('foo.html'); expect(onChangeSpy.callCount).toBe(2); onChangeSpy.reset(); - $location.updateHash('/baz'); - scope.$digest(); //match the redirect route + update $browser - $browser.defer.flush(); //match the route we redirected to - - expect($location.hash).toBe('/bar'); + $location.path('/baz'); + scope.$digest(); + expect($location.path()).toBe('/bar'); expect($route.current.template).toBe('bar.html'); expect(onChangeSpy.callCount).toBe(2); }); - it('should interpolate route variables in the redirected hashPath from the original hashPath', - function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $browser = scope.$service('$browser'), - $route = scope.$service('$route'); - + it('should interpolate route vars in the redirected path from original path', function() { $route.when('/foo/:id/foo/:subid/:extraId', {redirectTo: '/bar/:id/:subid/23'}); $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); - scope.$digest(); - $location.updateHash('/foo/id1/foo/subid3/gah'); - scope.$digest(); //triggers initial route change - match the redirect route - $browser.defer.flush(); //triger route change - match the route we redirected to + $location.path('/foo/id1/foo/subid3/gah'); + scope.$digest(); - expect($location.hash).toBe('/bar/id1/subid3/23?extraId=gah'); - expect($route.current.template).toBe('bar.html'); + expect($location.path()).toEqual('/bar/id1/subid3/23'); + expect($location.search()).toEqual({extraId: 'gah'}); + expect($route.current.template).toEqual('bar.html'); }); - it('should interpolate route variables in the redirected hashPath from the original hashSearch', - function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $browser = scope.$service('$browser'), - $route = scope.$service('$route'); - + it('should interpolate route vars in the redirected path from original search', function() { $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id/:subid/99'}); - scope.$digest(); - $location.hash = '/foo/id3/eId?subid=sid1&appended=true'; - scope.$digest(); //triggers initial route change - match the redirect route - $browser.defer.flush(); //triger route change - match the route we redirected to + $location.path('/foo/id3/eId').search('subid=sid1&appended=true'); + scope.$digest(); - expect($location.hash).toBe('/bar/id3/sid1/99?appended=true&extra=eId'); - expect($route.current.template).toBe('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'); }); it('should allow custom redirectTo function to be used', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $browser = scope.$service('$browser'), - $route = scope.$service('$route'); - $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); - $route.when('/foo/:id', - {redirectTo: customRedirectFn}); - scope.$digest(); + $route.when('/foo/:id', {redirectTo: customRedirectFn}); - $location.hash = '/foo/id3?subid=sid1&appended=true'; - scope.$digest(); //triggers initial route change - match the redirect route - $browser.defer.flush(); //triger route change - match the route we redirected to + $location.path('/foo/id3').search('subid=sid1&appended=true'); + scope.$digest(); - expect($location.hash).toBe('custom'); + expect($location.path()).toEqual('/custom'); - function customRedirectFn(routePathParams, hash, hashPath, hashSearch) { + function customRedirectFn(routePathParams, path, search) { expect(routePathParams).toEqual({id: 'id3'}); - expect(hash).toEqual($location.hash); - expect(hashPath).toEqual($location.hashPath); - expect(hashSearch).toEqual($location.hashSearch); - return 'custom'; + expect(path).toEqual($location.path()); + expect(search).toEqual($location.search()); + return '/custom'; } }); + + it('should replace the url when redirecting', function() { + $route.when('/bar/:id', {template: 'bar.html'}); + $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id'}); + + var replace; + scope.$watch(function() { + if (isUndefined(replace)) replace = $location.$$replace; + }); + + $location.path('/foo/id3/eId'); + scope.$digest(); + + expect($location.path()).toEqual('/bar/id3'); + expect(replace).toBe(true); + }); }); describe('reloadOnSearch', function() { - it('should reload a route when reloadOnSearch is enabled and hashSearch changes', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'), - $rouetParams = scope.$service('$routeParams'), + it('should reload a route when reloadOnSearch is enabled and .search() changes', function() { + var $rouetParams = scope.$service('$routeParams'), reloaded = jasmine.createSpy('route reload'); $route.when('/foo', {controller: FooCtrl}); @@ -280,26 +250,23 @@ describe('$route', function() { reloaded(); } - $location.updateHash('/foo'); + $location.path('/foo'); scope.$digest(); expect(reloaded).toHaveBeenCalled(); expect($rouetParams).toEqual({}); reloaded.reset(); // trigger reload - $location.hashSearch.foo = 'bar'; + $location.search({foo: 'bar'}); scope.$digest(); expect(reloaded).toHaveBeenCalled(); expect($rouetParams).toEqual({foo:'bar'}); }); - it('should not reload a route when reloadOnSearch is disabled and only hashSearch changes', + it('should not reload a route when reloadOnSearch is disabled and only .search() changes', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'), - reloaded = jasmine.createSpy('route reload'), + var reloaded = jasmine.createSpy('route reload'), routeUpdateEvent = jasmine.createSpy('route reload'); $route.when('/foo', {controller: FooCtrl, reloadOnSearch: false}); @@ -312,14 +279,14 @@ describe('$route', function() { expect(reloaded).not.toHaveBeenCalled(); - $location.updateHash('/foo'); + $location.path('/foo'); scope.$digest(); expect(reloaded).toHaveBeenCalled(); expect(routeUpdateEvent).not.toHaveBeenCalled(); reloaded.reset(); // don't trigger reload - $location.hashSearch.foo = 'bar'; + $location.search({foo: 'bar'}); scope.$digest(); expect(reloaded).not.toHaveBeenCalled(); expect(routeUpdateEvent).toHaveBeenCalled(); @@ -327,10 +294,7 @@ describe('$route', function() { it('should reload reloadOnSearch route when url differs only in route path param', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'), - reloaded = jasmine.createSpy('routeReload'), + var reloaded = jasmine.createSpy('routeReload'), onRouteChange = jasmine.createSpy('onRouteChange'); $route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false}); @@ -343,32 +307,29 @@ describe('$route', function() { expect(reloaded).not.toHaveBeenCalled(); expect(onRouteChange).not.toHaveBeenCalled(); - $location.updateHash('/foo/aaa'); + $location.path('/foo/aaa'); scope.$digest(); expect(reloaded).toHaveBeenCalled(); expect(onRouteChange).toHaveBeenCalled(); reloaded.reset(); onRouteChange.reset(); - $location.updateHash('/foo/bbb'); + $location.path('/foo/bbb'); scope.$digest(); expect(reloaded).toHaveBeenCalled(); expect(onRouteChange).toHaveBeenCalled(); reloaded.reset(); onRouteChange.reset(); - $location.hashSearch.foo = 'bar'; + $location.search({foo: 'bar'}); scope.$digest(); expect(reloaded).not.toHaveBeenCalled(); expect(onRouteChange).not.toHaveBeenCalled(); }); - it('should update route params when reloadOnSearch is disabled and hashSearch', function() { - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'), - routeParams = jasmine.createSpy('routeParams'); + it('should update params when reloadOnSearch is disabled and .search() changes', function() { + var routeParams = jasmine.createSpy('routeParams'); $route.when('/foo', {controller: FooCtrl}); $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); @@ -383,24 +344,24 @@ describe('$route', function() { expect(routeParams).not.toHaveBeenCalled(); - $location.updateHash('/foo'); + $location.path('/foo'); scope.$digest(); expect(routeParams).toHaveBeenCalledWith({}); routeParams.reset(); // trigger reload - $location.hashSearch.foo = 'bar'; + $location.search({foo: 'bar'}); scope.$digest(); expect(routeParams).toHaveBeenCalledWith({foo: 'bar'}); routeParams.reset(); - $location.updateHash('/bar/123'); + $location.path('/bar/123').search({}); scope.$digest(); expect(routeParams).toHaveBeenCalledWith({barId: '123'}); routeParams.reset(); // don't trigger reload - $location.hashSearch.foo = 'bar'; + $location.search({foo: 'bar'}); scope.$digest(); expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'}); }); @@ -409,22 +370,19 @@ describe('$route', function() { describe('reload', function(){ it('should reload even if reloadOnSearch is false', function(){ - var scope = angular.scope(), - $location = scope.$service('$location'), - $route = scope.$service('$route'), - $routeParams = scope.$service('$routeParams'), + var $routeParams = scope.$service('$routeParams'), count = 0; $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); function FooCtrl() { count ++; } - $location.updateHash('/bar/123'); + $location.path('/bar/123'); scope.$digest(); expect($routeParams).toEqual({barId:'123'}); expect(count).toEqual(1); - $location.hash = '/bar/123?a=b'; + $location.path('/bar/123').search('a=b'); scope.$digest(); expect($routeParams).toEqual({barId:'123', a:'b'}); expect(count).toEqual(1); |
