diff options
| author | Misko Hevery | 2011-08-23 22:30:14 -0700 |
|---|---|---|
| committer | Igor Minar | 2011-08-31 14:31:23 -0700 |
| commit | ad3cc16eef0a13844e6e05abcb18c46a370f0814 (patch) | |
| tree | 48c90bd555d26eef0164b7deeff5417e300a6536 /test/service | |
| parent | 08d09ecbaa07564bf3cf6a62e0be4c41b355d23b (diff) | |
| download | angular.js-ad3cc16eef0a13844e6e05abcb18c46a370f0814.tar.bz2 | |
feat($route): add events before/after route change
BREAKING CHANGE
* removing `onChange`
FEATURE
* adding three events: $beforeRouteChange, $afterRouteChange, $routeReload
Diffstat (limited to 'test/service')
| -rw-r--r-- | test/service/routeParamsSpec.js | 41 | ||||
| -rw-r--r-- | test/service/routeSpec.js | 94 |
2 files changed, 109 insertions, 26 deletions
diff --git a/test/service/routeParamsSpec.js b/test/service/routeParamsSpec.js new file mode 100644 index 00000000..58a37f2e --- /dev/null +++ b/test/service/routeParamsSpec.js @@ -0,0 +1,41 @@ +'use strict'; + +describe('$routeParams', function(){ + it('should publish the params into a service', function(){ + var scope = angular.scope(), + $location = scope.$service('$location'), + $route = scope.$service('$route'), + $routeParams = scope.$service('$routeParams'); + + $route.when('/foo'); + $route.when('/bar/:barId'); + + $location.hash = '/foo?a=b'; + scope.$digest(); + expect($routeParams).toEqual({a:'b'}); + + $location.hash = '/bar/123?x=abc'; + scope.$digest(); + expect($routeParams).toEqual({barId:'123', x:'abc'}); + }); + + + it('should preserve object identity during route reloads', function(){ + var scope = angular.scope(), + $location = scope.$service('$location'), + $route = scope.$service('$route'), + $routeParams = scope.$service('$routeParams'), + firstRouteParams = $routeParams; + + $route.when('/foo'); + $route.when('/bar/:barId'); + + $location.hash = '/foo?a=b'; + scope.$digest(); + expect(scope.$service('$routeParams')).toBe(firstRouteParams); + + $location.hash = '/bar/123?x=abc'; + scope.$digest(); + expect(scope.$service('$routeParams')).toBe(firstRouteParams); + }); +}); diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index 72c7745c..b1dde915 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -15,37 +15,48 @@ describe('$route', function() { it('should route and fire change event', function(){ var log = '', - $location, $route; + $location, $route, + lastRoute, + nextRoute; function BookChapter() { - log += '<init>'; + 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'); - $route.onChange(function(){ - log += 'onChange();'; + scope.$on('$beforeRouteChange', function(event, next, current){ + log += 'before();'; + expect(current).toBe($route.current); + lastRoute = current; + nextRoute = next; + }); + scope.$on('$afterRouteChange', function(event, current, last){ + log += 'after();'; + expect(current).toBe($route.current); + expect(lastRoute).toBe(last); + expect(nextRoute).toBe(current); }); $location.update('http://server#/Book/Moby/Chapter/Intro?p=123'); scope.$digest(); - expect(log).toEqual('onChange();<init>'); + 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'); scope.$digest(); - expect(log).toEqual('onChange();'); + 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'); scope.$digest(); - expect(log).toEqual('onChange();'); + expect(log).toEqual('before();after();'); expect($route.current).toEqual(null); $route.when('/NONE', {template:'instant update'}); @@ -54,15 +65,6 @@ describe('$route', function() { }); - it('should return fn registered with onChange()', function() { - var scope = angular.scope(), - $route = scope.$service('$route'), - fn = function() {}; - - expect($route.onChange(fn)).toBe(fn); - }); - - it('should allow routes to be defined with just templates without controllers', function() { var scope = angular.scope(), $location = scope.$service('$location'), @@ -70,7 +72,7 @@ describe('$route', function() { onChangeSpy = jasmine.createSpy('onChange'); $route.when('/foo', {template: 'foo.html'}); - $route.onChange(onChangeSpy); + scope.$on('$beforeRouteChange', onChangeSpy); expect($route.current).toBeUndefined(); expect(onChangeSpy).not.toHaveBeenCalled(); @@ -93,7 +95,7 @@ describe('$route', function() { $route.when('/foo', {template: 'foo.html'}); $route.otherwise({template: '404.html', controller: NotFoundCtrl}); - $route.onChange(onChangeSpy); + scope.$on('$beforeRouteChange', onChangeSpy); expect($route.current).toBeUndefined(); expect(onChangeSpy).not.toHaveBeenCalled(); @@ -163,7 +165,7 @@ describe('$route', function() { $route.when('/bar', {template: 'bar.html'}); $route.when('/baz', {redirectTo: '/bar'}); $route.otherwise({template: '404.html'}); - $route.onChange(onChangeSpy); + scope.$on('$beforeRouteChange', onChangeSpy); expect($route.current).toBeUndefined(); expect(onChangeSpy).not.toHaveBeenCalled(); @@ -172,7 +174,8 @@ describe('$route', function() { expect($location.hash).toBe('/foo'); expect($route.current.template).toBe('foo.html'); - expect(onChangeSpy.callCount).toBe(1); + expect(onChangeSpy.callCount).toBe(2); + onChangeSpy.reset(); $location.updateHash(''); @@ -181,7 +184,7 @@ describe('$route', function() { expect($location.hash).toBe('/foo'); expect($route.current.template).toBe('foo.html'); - expect(onChangeSpy.callCount).toBe(1); + expect(onChangeSpy.callCount).toBe(2); onChangeSpy.reset(); $location.updateHash('/baz'); @@ -190,7 +193,7 @@ describe('$route', function() { expect($location.hash).toBe('/bar'); expect($route.current.template).toBe('bar.html'); - expect(onChangeSpy.callCount).toBe(1); + expect(onChangeSpy.callCount).toBe(2); }); @@ -267,10 +270,11 @@ describe('$route', function() { var scope = angular.scope(), $location = scope.$service('$location'), $route = scope.$service('$route'), + $rouetParams = scope.$service('$routeParams'), reloaded = jasmine.createSpy('route reload'); $route.when('/foo', {controller: FooCtrl}); - $route.onChange(reloaded); + scope.$on('$beforeRouteChange', reloaded); function FooCtrl() { reloaded(); @@ -279,12 +283,14 @@ describe('$route', function() { $location.updateHash('/foo'); scope.$digest(); expect(reloaded).toHaveBeenCalled(); + expect($rouetParams).toEqual({}); reloaded.reset(); // trigger reload $location.hashSearch.foo = 'bar'; scope.$digest(); expect(reloaded).toHaveBeenCalled(); + expect($rouetParams).toEqual({foo:'bar'}); }); @@ -293,13 +299,15 @@ describe('$route', function() { var scope = angular.scope(), $location = scope.$service('$location'), $route = scope.$service('$route'), - reloaded = jasmine.createSpy('route reload'); + reloaded = jasmine.createSpy('route reload'), + routeUpdateEvent = jasmine.createSpy('route reload'); $route.when('/foo', {controller: FooCtrl, reloadOnSearch: false}); - $route.onChange(reloaded); + scope.$on('$beforeRouteChange', reloaded); function FooCtrl() { reloaded(); + this.$on('$routeUpdate', routeUpdateEvent); } expect(reloaded).not.toHaveBeenCalled(); @@ -307,12 +315,14 @@ describe('$route', function() { $location.updateHash('/foo'); scope.$digest(); expect(reloaded).toHaveBeenCalled(); + expect(routeUpdateEvent).not.toHaveBeenCalled(); reloaded.reset(); // don't trigger reload $location.hashSearch.foo = 'bar'; scope.$digest(); expect(reloaded).not.toHaveBeenCalled(); + expect(routeUpdateEvent).toHaveBeenCalled(); }); @@ -324,7 +334,7 @@ describe('$route', function() { onRouteChange = jasmine.createSpy('onRouteChange'); $route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false}); - $route.onChange(onRouteChange); + scope.$on('$beforeRouteChange', onRouteChange); function FooCtrl() { reloaded(); @@ -394,5 +404,37 @@ describe('$route', function() { scope.$digest(); expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'}); }); + + + 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'), + count = 0; + + $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); + + function FooCtrl() { count ++; } + + $location.updateHash('/bar/123'); + scope.$digest(); + expect($routeParams).toEqual({barId:'123'}); + expect(count).toEqual(1); + + $location.hash = '/bar/123?a=b'; + scope.$digest(); + expect($routeParams).toEqual({barId:'123', a:'b'}); + expect(count).toEqual(1); + + $route.reload(); + scope.$digest(); + expect($routeParams).toEqual({barId:'123', a:'b'}); + expect(count).toEqual(2); + }); + }); + }); }); |
