aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorIgor Minar2013-03-08 11:59:41 -0800
committerIgor Minar2013-03-08 12:00:34 -0800
commit65e57a7c3d53fad536797301fe59b5912ed4db16 (patch)
treeb4090ef75f0d6e78419329998ecc5783b40db128 /test
parent6f71e809141bf89501e55c378921d6e7ec9512bc (diff)
downloadangular.js-65e57a7c3d53fad536797301fe59b5912ed4db16.tar.bz2
test($route): add tests for matching 'otherwise' routes
Diffstat (limited to 'test')
-rw-r--r--test/ng/routeSpec.js121
1 files changed, 88 insertions, 33 deletions
diff --git a/test/ng/routeSpec.js b/test/ng/routeSpec.js
index 1f714f62..c2651286 100644
--- a/test/ng/routeSpec.js
+++ b/test/ng/routeSpec.js
@@ -285,53 +285,108 @@ describe('$route', function() {
});
- it('should handle unknown routes with "otherwise" route definition', function() {
- function NotFoundCtrl() {}
-
+ it('should chain whens and otherwise', function() {
module(function($routeProvider){
- $routeProvider.when('/foo', {templateUrl: 'foo.html'});
- $routeProvider.otherwise({templateUrl: '404.html', controller: NotFoundCtrl});
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'}).
+ otherwise({templateUrl: 'bar.html'}).
+ when('/baz', {templateUrl: 'baz.html'});
});
inject(function($route, $location, $rootScope) {
- var onChangeSpy = jasmine.createSpy('onChange');
-
- $rootScope.$on('$routeChangeStart', onChangeSpy);
- expect($route.current).toBeUndefined();
- expect(onChangeSpy).not.toHaveBeenCalled();
-
- $location.path('/unknownRoute');
$rootScope.$digest();
+ expect($route.current.templateUrl).toBe('bar.html');
- expect($route.current.templateUrl).toBe('404.html');
- expect($route.current.controller).toBe(NotFoundCtrl);
- expect(onChangeSpy).toHaveBeenCalled();
-
- onChangeSpy.reset();
- $location.path('/foo');
+ $location.url('/baz');
$rootScope.$digest();
-
- expect($route.current.templateUrl).toEqual('foo.html');
- expect($route.current.controller).toBeUndefined();
- expect(onChangeSpy).toHaveBeenCalled();
+ expect($route.current.templateUrl).toBe('baz.html');
});
});
- it('should chain whens and otherwise', function() {
- module(function($routeProvider){
- $routeProvider.when('/foo', {templateUrl: 'foo.html'}).
- otherwise({templateUrl: 'bar.html'}).
- when('/baz', {templateUrl: 'baz.html'});
+ describe('otherwise', function() {
+
+ it('should handle unknown routes with "otherwise" route definition', function() {
+ function NotFoundCtrl() {}
+
+ module(function($routeProvider){
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'});
+ $routeProvider.otherwise({templateUrl: '404.html', controller: NotFoundCtrl});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ var onChangeSpy = jasmine.createSpy('onChange');
+
+ $rootScope.$on('$routeChangeStart', onChangeSpy);
+ expect($route.current).toBeUndefined();
+ expect(onChangeSpy).not.toHaveBeenCalled();
+
+ $location.path('/unknownRoute');
+ $rootScope.$digest();
+
+ expect($route.current.templateUrl).toBe('404.html');
+ expect($route.current.controller).toBe(NotFoundCtrl);
+ expect(onChangeSpy).toHaveBeenCalled();
+
+ onChangeSpy.reset();
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect($route.current.templateUrl).toEqual('foo.html');
+ expect($route.current.controller).toBeUndefined();
+ expect(onChangeSpy).toHaveBeenCalled();
+ });
});
- inject(function($route, $location, $rootScope) {
- $rootScope.$digest();
- expect($route.current.templateUrl).toBe('bar.html');
- $location.url('/baz');
- $rootScope.$digest();
- expect($route.current.templateUrl).toBe('baz.html');
+ it('should update $route.current and $route.next when default route is matched', function() {
+ module(function($routeProvider){
+ $routeProvider.when('/foo', {templateUrl: 'foo.html'});
+ $routeProvider.otherwise({templateUrl: '404.html'});
+ });
+
+ inject(function($route, $location, $rootScope) {
+ var currentRoute, nextRoute,
+ onChangeSpy = jasmine.createSpy('onChange').andCallFake(function(e, next) {
+ currentRoute = $route.current;
+ nextRoute = next;
+ });
+
+
+ // init
+ $rootScope.$on('$routeChangeStart', onChangeSpy);
+ expect($route.current).toBeUndefined();
+ expect(onChangeSpy).not.toHaveBeenCalled();
+
+
+ // match otherwise route
+ $location.path('/unknownRoute');
+ $rootScope.$digest();
+
+ expect(currentRoute).toBeUndefined();
+ expect(nextRoute.templateUrl).toBe('404.html');
+ expect($route.current.templateUrl).toBe('404.html');
+ expect(onChangeSpy).toHaveBeenCalled();
+ onChangeSpy.reset();
+
+ // match regular route
+ $location.path('/foo');
+ $rootScope.$digest();
+
+ expect(currentRoute.templateUrl).toBe('404.html');
+ expect(nextRoute.templateUrl).toBe('foo.html');
+ expect($route.current.templateUrl).toEqual('foo.html');
+ expect(onChangeSpy).toHaveBeenCalled();
+ onChangeSpy.reset();
+
+ // match otherwise route again
+ $location.path('/anotherUnknownRoute');
+ $rootScope.$digest();
+
+ expect(currentRoute.templateUrl).toBe('foo.html');
+ expect(nextRoute.templateUrl).toBe('404.html');
+ expect($route.current.templateUrl).toEqual('404.html');
+ expect(onChangeSpy).toHaveBeenCalled();
+ });
});
});