diff options
Diffstat (limited to 'test/service/routeSpec.js')
| -rw-r--r-- | test/service/routeSpec.js | 71 |
1 files changed, 52 insertions, 19 deletions
diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index fc2c7f9d..6c6c0868 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -18,7 +18,7 @@ describe('$route', function() { $location, $route; function BookChapter() { - this.log = '<init>'; + log += '<init>'; } scope = compile('<div></div>')(); $location = scope.$service('$location'); @@ -28,28 +28,28 @@ describe('$route', function() { $route.onChange(function(){ log += 'onChange();'; }); + $location.update('http://server#/Book/Moby/Chapter/Intro?p=123'); - scope.$eval(); - expect(log).toEqual('onChange();'); + scope.$digest(); + expect(log).toEqual('onChange();<init>'); expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'}); - expect($route.current.scope.log).toEqual('<init>'); var lastId = $route.current.scope.$id; log = ''; $location.update('http://server#/Blank?ignore'); - scope.$eval(); + scope.$digest(); expect(log).toEqual('onChange();'); expect($route.current.params).toEqual({ignore:true}); expect($route.current.scope.$id).not.toEqual(lastId); log = ''; $location.update('http://server#/NONE'); - scope.$eval(); + scope.$digest(); expect(log).toEqual('onChange();'); expect($route.current).toEqual(null); $route.when('/NONE', {template:'instant update'}); - scope.$eval(); + scope.$digest(); expect($route.current.template).toEqual('instant update'); }); @@ -75,7 +75,7 @@ describe('$route', function() { expect(onChangeSpy).not.toHaveBeenCalled(); $location.updateHash('/foo'); - scope.$eval(); + scope.$digest(); expect($route.current.template).toEqual('foo.html'); expect($route.current.controller).toBeUndefined(); @@ -98,7 +98,7 @@ describe('$route', function() { expect(onChangeSpy).not.toHaveBeenCalled(); $location.updateHash('/unknownRoute'); - scope.$eval(); + scope.$digest(); expect($route.current.template).toBe('404.html'); expect($route.current.controller).toBe(NotFoundCtrl); @@ -107,7 +107,7 @@ describe('$route', function() { onChangeSpy.reset(); $location.updateHash('/foo'); - scope.$eval(); + scope.$digest(); expect($route.current.template).toEqual('foo.html'); expect($route.current.controller).toBeUndefined(); @@ -115,6 +115,39 @@ describe('$route', function() { expect(onChangeSpy).toHaveBeenCalled(); }); + 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'); + scope.$digest(); + expect(scope.$$childHead).toBeTruthy(); + expect(scope.$$childHead).toEqual(scope.$$childTail); + + $location.updateHash('/bar'); + scope.$digest(); + expect(scope.$$childHead).toBeTruthy(); + expect(scope.$$childHead).toEqual(scope.$$childTail); + return + + $location.updateHash('/baz'); + scope.$digest(); + expect(scope.$$childHead).toBeTruthy(); + expect(scope.$$childHead).toEqual(scope.$$childTail); + + $location.updateHash('/'); + scope.$digest(); + expect(scope.$$childHead).toEqual(null); + expect(scope.$$childTail).toEqual(null); + }); + describe('redirection', function() { @@ -134,7 +167,7 @@ describe('$route', function() { expect($route.current).toBeNull(); expect(onChangeSpy).not.toHaveBeenCalled(); - scope.$eval(); //triggers initial route change - match the redirect route + 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'); @@ -143,7 +176,7 @@ describe('$route', function() { onChangeSpy.reset(); $location.updateHash(''); - scope.$eval(); //match the redirect route + update $browser + scope.$digest(); //match the redirect route + update $browser $browser.defer.flush(); //match the route we redirected to expect($location.hash).toBe('/foo'); @@ -152,7 +185,7 @@ describe('$route', function() { onChangeSpy.reset(); $location.updateHash('/baz'); - scope.$eval(); //match the redirect route + update $browser + scope.$digest(); //match the redirect route + update $browser $browser.defer.flush(); //match the route we redirected to expect($location.hash).toBe('/bar'); @@ -170,10 +203,10 @@ describe('$route', function() { $route.when('/foo/:id/foo/:subid/:extraId', {redirectTo: '/bar/:id/:subid/23'}); $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); - scope.$eval(); + scope.$digest(); $location.updateHash('/foo/id1/foo/subid3/gah'); - scope.$eval(); //triggers initial route change - match the redirect route + 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('/bar/id1/subid3/23?extraId=gah'); @@ -190,10 +223,10 @@ describe('$route', function() { $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); $route.when('/foo/:id/:extra', {redirectTo: '/bar/:id/:subid/99'}); - scope.$eval(); + scope.$digest(); $location.hash = '/foo/id3/eId?subid=sid1&appended=true'; - scope.$eval(); //triggers initial route change - match the redirect route + 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('/bar/id3/sid1/99?appended=true&extra=eId'); @@ -210,10 +243,10 @@ describe('$route', function() { $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); $route.when('/foo/:id', {redirectTo: customRedirectFn}); - scope.$eval(); + scope.$digest(); $location.hash = '/foo/id3?subid=sid1&appended=true'; - scope.$eval(); //triggers initial route change - match the redirect route + 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('custom'); |
