mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-18 19:41:07 +00:00
test($route): add tests for matching 'otherwise' routes
This commit is contained in:
parent
6f71e80914
commit
65e57a7c3d
1 changed files with 90 additions and 35 deletions
|
|
@ -285,44 +285,11 @@ describe('$route', 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();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
it('should chain whens and otherwise', function() {
|
it('should chain whens and otherwise', function() {
|
||||||
module(function($routeProvider){
|
module(function($routeProvider){
|
||||||
$routeProvider.when('/foo', {templateUrl: 'foo.html'}).
|
$routeProvider.when('/foo', {templateUrl: 'foo.html'}).
|
||||||
otherwise({templateUrl: 'bar.html'}).
|
otherwise({templateUrl: 'bar.html'}).
|
||||||
when('/baz', {templateUrl: 'baz.html'});
|
when('/baz', {templateUrl: 'baz.html'});
|
||||||
});
|
});
|
||||||
|
|
||||||
inject(function($route, $location, $rootScope) {
|
inject(function($route, $location, $rootScope) {
|
||||||
|
|
@ -336,6 +303,94 @@ describe('$route', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('events', function() {
|
describe('events', function() {
|
||||||
it('should not fire $after/beforeRouteChange during bootstrap (if no route)', function() {
|
it('should not fire $after/beforeRouteChange during bootstrap (if no route)', function() {
|
||||||
var routeChangeSpy = jasmine.createSpy('route change');
|
var routeChangeSpy = jasmine.createSpy('route change');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue