aboutsummaryrefslogtreecommitdiffstats
path: root/test/service/routeSpec.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/service/routeSpec.js')
-rw-r--r--test/service/routeSpec.js202
1 files changed, 100 insertions, 102 deletions
diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js
index 5aba2a1f..26ae17e5 100644
--- a/test/service/routeSpec.js
+++ b/test/service/routeSpec.js
@@ -1,16 +1,7 @@
'use strict';
describe('$route', function() {
- var scope, $route, $location;
-
- beforeEach(function() {
- scope = angular.scope();
- $location = scope.$service('$location');
- $route = scope.$service('$route');
- });
-
-
- it('should route and fire change event', function() {
+ it('should route and fire change event', inject(function($route, $location, $rootScope) {
var log = '',
lastRoute,
nextRoute;
@@ -21,13 +12,13 @@ describe('$route', function() {
$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template: 'Chapter.html'});
$route.when('/Blank');
- scope.$on('$beforeRouteChange', function(event, next, current) {
+ $rootScope.$on('$beforeRouteChange', function(event, next, current) {
log += 'before();';
expect(current).toBe($route.current);
lastRoute = current;
nextRoute = next;
});
- scope.$on('$afterRouteChange', function(event, current, last) {
+ $rootScope.$on('$afterRouteChange', function(event, current, last) {
log += 'after();';
expect(current).toBe($route.current);
expect(lastRoute).toBe(last);
@@ -35,97 +26,98 @@ describe('$route', function() {
});
$location.path('/Book/Moby/Chapter/Intro').search('p=123');
- scope.$digest();
+ $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');
- scope.$digest();
+ $rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({ignore:true});
expect($route.current.scope.$id).not.toEqual(lastId);
log = '';
$location.path('/NONE');
- scope.$digest();
+ $rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current).toEqual(null);
$route.when('/NONE', {template:'instant update'});
- scope.$digest();
+ $rootScope.$digest();
expect($route.current.template).toEqual('instant update');
- });
+ }));
- it('should match a route that contains special chars in the path', function() {
+ 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'});
$location.path('/test');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current).toBeUndefined();
$location.path('/$testX23/foo(bar)/222');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current).toBeUndefined();
$location.path('/$test.23/foo(bar)/222');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current).toBeDefined();
$location.path('/$test.23/foo\\(bar)/222');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current).toBeUndefined();
- });
+ }));
- it('should change route even when only search param changes', function() {
+ it('should change route even when only search param changes', inject(function($route, $location, $rootScope) {
var callback = jasmine.createSpy('onRouteChange');
$route.when('/test', {template: 'test.html'});
- scope.$on('$beforeRouteChange', callback);
+ $rootScope.$on('$beforeRouteChange', callback);
$location.path('/test');
- scope.$digest();
+ $rootScope.$digest();
callback.reset();
$location.search({any: true});
- scope.$digest();
+ $rootScope.$digest();
expect(callback).toHaveBeenCalled();
- });
+ }));
- it('should allow routes to be defined with just templates without controllers', function() {
+ 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'});
- scope.$on('$beforeRouteChange', onChangeSpy);
+ $rootScope.$on('$beforeRouteChange', onChangeSpy);
expect($route.current).toBeUndefined();
expect(onChangeSpy).not.toHaveBeenCalled();
$location.path('/foo');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current.template).toEqual('foo.html');
expect($route.current.controller).toBeUndefined();
expect(onChangeSpy).toHaveBeenCalled();
- });
+ }));
- it('should handle unknown routes with "otherwise" route definition', function() {
+ it('should handle unknown routes with "otherwise" route definition', inject(function($route, $location, $rootScope) {
var onChangeSpy = jasmine.createSpy('onChange');
function NotFoundCtrl() {this.notFoundProp = 'not found!';}
$route.when('/foo', {template: 'foo.html'});
$route.otherwise({template: '404.html', controller: NotFoundCtrl});
- scope.$on('$beforeRouteChange', onChangeSpy);
+ $rootScope.$on('$beforeRouteChange', onChangeSpy);
expect($route.current).toBeUndefined();
expect(onChangeSpy).not.toHaveBeenCalled();
$location.path('/unknownRoute');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current.template).toBe('404.html');
expect($route.current.controller).toBe(NotFoundCtrl);
@@ -134,54 +126,55 @@ describe('$route', function() {
onChangeSpy.reset();
$location.path('/foo');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current.template).toEqual('foo.html');
expect($route.current.controller).toBeUndefined();
expect($route.current.scope.notFoundProp).toBeUndefined();
expect(onChangeSpy).toHaveBeenCalled();
- });
+ }));
- it('should $destroy old routes', function() {
+ 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'});
- expect(scope.$childHead).toEqual(null);
+ expect($rootScope.$childHead).toEqual(null);
$location.path('/foo');
- scope.$digest();
- expect(scope.$$childHead.$id).toBeTruthy();
- expect(scope.$$childHead.$id).toEqual(scope.$$childTail.$id);
+ $rootScope.$digest();
+ expect($rootScope.$$childHead.$id).toBeTruthy();
+ expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id);
$location.path('/bar');
- scope.$digest();
- expect(scope.$$childHead.$id).toBeTruthy();
- expect(scope.$$childHead.$id).toEqual(scope.$$childTail.$id);
+ $rootScope.$digest();
+ expect($rootScope.$$childHead.$id).toBeTruthy();
+ expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id);
$location.path('/baz');
- scope.$digest();
- expect(scope.$$childHead.$id).toBeTruthy();
- expect(scope.$$childHead.$id).toEqual(scope.$$childTail.$id);
+ $rootScope.$digest();
+ expect($rootScope.$$childHead.$id).toBeTruthy();
+ expect($rootScope.$$childHead.$id).toEqual($rootScope.$$childTail.$id);
$location.path('/');
- scope.$digest();
- expect(scope.$$childHead).toEqual(null);
- expect(scope.$$childTail).toEqual(null);
- });
+ $rootScope.$digest();
+ expect($rootScope.$$childHead).toEqual(null);
+ expect($rootScope.$$childTail).toEqual(null);
+ }));
- it('should infer arguments in injection', function() {
+ it('should infer arguments in injection', inject(function($route, $location, $rootScope) {
$route.when('/test', {controller: function($route){ this.$route = $route; }});
$location.path('/test');
- scope.$digest();
+ $rootScope.$digest();
expect($route.current.scope.$route).toBe($route);
- });
+ }));
describe('redirection', function() {
- it('should support redirection via redirectTo property by updating $location', 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'});
@@ -189,57 +182,59 @@ describe('$route', function() {
$route.when('/bar', {template: 'bar.html'});
$route.when('/baz', {redirectTo: '/bar'});
$route.otherwise({template: '404.html'});
- scope.$on('$beforeRouteChange', onChangeSpy);
+ $rootScope.$on('$beforeRouteChange', onChangeSpy);
expect($route.current).toBeUndefined();
expect(onChangeSpy).not.toHaveBeenCalled();
$location.path('/');
- scope.$digest();
+ $rootScope.$digest();
expect($location.path()).toBe('/foo');
expect($route.current.template).toBe('foo.html');
expect(onChangeSpy.callCount).toBe(2);
onChangeSpy.reset();
$location.path('/baz');
- scope.$digest();
+ $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 path', function() {
+ 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'});
$location.path('/foo/id1/foo/subid3/gah');
- scope.$digest();
+ $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 interpolate route vars in the redirected path from original search', function() {
+ 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');
- scope.$digest();
+ $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');
- });
+ }));
- it('should allow custom redirectTo function to be used', function() {
+ 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');
- scope.$digest();
+ $rootScope.$digest();
expect($location.path()).toEqual('/custom');
@@ -249,60 +244,61 @@ describe('$route', function() {
expect(search).toEqual($location.search());
return '/custom';
}
- });
+ }));
- it('should replace the url when redirecting', function() {
+ 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'});
var replace;
- scope.$watch(function() {
+ $rootScope.$watch(function() {
if (isUndefined(replace)) replace = $location.$$replace;
});
$location.path('/foo/id3/eId');
- scope.$digest();
+ $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', function() {
- var $routeParams = scope.$service('$routeParams'),
+ it('should reload a route when reloadOnSearch is enabled and .search() changes',
+ inject(function($route, $location, $rootScope) {
+ var $routeParams = $rootScope.$service('$routeParams'),
reloaded = jasmine.createSpy('route reload');
$route.when('/foo', {controller: FooCtrl});
- scope.$on('$beforeRouteChange', reloaded);
+ $rootScope.$on('$beforeRouteChange', reloaded);
function FooCtrl() {
reloaded();
}
$location.path('/foo');
- scope.$digest();
+ $rootScope.$digest();
expect(reloaded).toHaveBeenCalled();
expect($routeParams).toEqual({});
reloaded.reset();
// trigger reload
$location.search({foo: 'bar'});
- scope.$digest();
+ $rootScope.$digest();
expect(reloaded).toHaveBeenCalled();
expect($routeParams).toEqual({foo:'bar'});
- });
+ }));
it('should not reload a route when reloadOnSearch is disabled and only .search() changes',
- function() {
+ inject(function($route, $location, $rootScope) {
var reloaded = jasmine.createSpy('route reload'),
routeUpdateEvent = jasmine.createSpy('route reload');
$route.when('/foo', {controller: FooCtrl, reloadOnSearch: false});
- scope.$on('$beforeRouteChange', reloaded);
+ $rootScope.$on('$beforeRouteChange', reloaded);
function FooCtrl() {
reloaded();
@@ -312,25 +308,26 @@ describe('$route', function() {
expect(reloaded).not.toHaveBeenCalled();
$location.path('/foo');
- scope.$digest();
+ $rootScope.$digest();
expect(reloaded).toHaveBeenCalled();
expect(routeUpdateEvent).not.toHaveBeenCalled();
reloaded.reset();
// don't trigger reload
$location.search({foo: 'bar'});
- scope.$digest();
+ $rootScope.$digest();
expect(reloaded).not.toHaveBeenCalled();
expect(routeUpdateEvent).toHaveBeenCalled();
- });
+ }));
- it('should reload reloadOnSearch route when url differs only in route path param', function() {
+ it('should reload reloadOnSearch route when url differs only in route path param',
+ inject(function($route, $location, $rootScope) {
var reloaded = jasmine.createSpy('routeReload'),
onRouteChange = jasmine.createSpy('onRouteChange');
$route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false});
- scope.$on('$beforeRouteChange', onRouteChange);
+ $rootScope.$on('$beforeRouteChange', onRouteChange);
function FooCtrl() {
reloaded();
@@ -340,27 +337,28 @@ describe('$route', function() {
expect(onRouteChange).not.toHaveBeenCalled();
$location.path('/foo/aaa');
- scope.$digest();
+ $rootScope.$digest();
expect(reloaded).toHaveBeenCalled();
expect(onRouteChange).toHaveBeenCalled();
reloaded.reset();
onRouteChange.reset();
$location.path('/foo/bbb');
- scope.$digest();
+ $rootScope.$digest();
expect(reloaded).toHaveBeenCalled();
expect(onRouteChange).toHaveBeenCalled();
reloaded.reset();
onRouteChange.reset();
$location.search({foo: 'bar'});
- scope.$digest();
+ $rootScope.$digest();
expect(reloaded).not.toHaveBeenCalled();
expect(onRouteChange).not.toHaveBeenCalled();
- });
+ }));
- it('should update params when reloadOnSearch is disabled and .search() changes', function() {
+ it('should update params when reloadOnSearch is disabled and .search() changes',
+ inject(function($route, $location, $rootScope) {
var routeParams = jasmine.createSpy('routeParams');
$route.when('/foo', {controller: FooCtrl});
@@ -377,32 +375,32 @@ describe('$route', function() {
expect(routeParams).not.toHaveBeenCalled();
$location.path('/foo');
- scope.$digest();
+ $rootScope.$digest();
expect(routeParams).toHaveBeenCalledWith({});
routeParams.reset();
// trigger reload
$location.search({foo: 'bar'});
- scope.$digest();
+ $rootScope.$digest();
expect(routeParams).toHaveBeenCalledWith({foo: 'bar'});
routeParams.reset();
$location.path('/bar/123').search({});
- scope.$digest();
+ $rootScope.$digest();
expect(routeParams).toHaveBeenCalledWith({barId: '123'});
routeParams.reset();
// don't trigger reload
$location.search({foo: 'bar'});
- scope.$digest();
+ $rootScope.$digest();
expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'});
- });
+ }));
describe('reload', function() {
- it('should reload even if reloadOnSearch is false', function() {
- var $routeParams = scope.$service('$routeParams'),
+ it('should reload even if reloadOnSearch is false', inject(function($route, $location, $rootScope) {
+ var $routeParams = $rootScope.$service('$routeParams'),
count = 0;
$route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false});
@@ -410,20 +408,20 @@ describe('$route', function() {
function FooCtrl() { count ++; }
$location.path('/bar/123');
- scope.$digest();
+ $rootScope.$digest();
expect($routeParams).toEqual({barId:'123'});
expect(count).toEqual(1);
$location.path('/bar/123').search('a=b');
- scope.$digest();
+ $rootScope.$digest();
expect($routeParams).toEqual({barId:'123', a:'b'});
expect(count).toEqual(1);
$route.reload();
- scope.$digest();
+ $rootScope.$digest();
expect($routeParams).toEqual({barId:'123', a:'b'});
expect(count).toEqual(2);
- });
+ }));
});
});
});