Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
test($route): add tests for matching 'otherwise' routes
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar committed Mar 8, 2013
1 parent 6f71e80 commit 65e57a7
Showing 1 changed file with 88 additions and 33 deletions.
121 changes: 88 additions & 33 deletions test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down

0 comments on commit 65e57a7

Please sign in to comment.