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

Commit 65e57a7

Browse files
committed
test($route): add tests for matching 'otherwise' routes
1 parent 6f71e80 commit 65e57a7

File tree

1 file changed

+88
-33
lines changed

1 file changed

+88
-33
lines changed

Diff for: test/ng/routeSpec.js

+88-33
Original file line numberDiff line numberDiff line change
@@ -285,53 +285,108 @@ describe('$route', function() {
285285
});
286286

287287

288-
it('should handle unknown routes with "otherwise" route definition', function() {
289-
function NotFoundCtrl() {}
290-
288+
it('should chain whens and otherwise', function() {
291289
module(function($routeProvider){
292-
$routeProvider.when('/foo', {templateUrl: 'foo.html'});
293-
$routeProvider.otherwise({templateUrl: '404.html', controller: NotFoundCtrl});
290+
$routeProvider.when('/foo', {templateUrl: 'foo.html'}).
291+
otherwise({templateUrl: 'bar.html'}).
292+
when('/baz', {templateUrl: 'baz.html'});
294293
});
295294

296295
inject(function($route, $location, $rootScope) {
297-
var onChangeSpy = jasmine.createSpy('onChange');
298-
299-
$rootScope.$on('$routeChangeStart', onChangeSpy);
300-
expect($route.current).toBeUndefined();
301-
expect(onChangeSpy).not.toHaveBeenCalled();
302-
303-
$location.path('/unknownRoute');
304296
$rootScope.$digest();
297+
expect($route.current.templateUrl).toBe('bar.html');
305298

306-
expect($route.current.templateUrl).toBe('404.html');
307-
expect($route.current.controller).toBe(NotFoundCtrl);
308-
expect(onChangeSpy).toHaveBeenCalled();
309-
310-
onChangeSpy.reset();
311-
$location.path('/foo');
299+
$location.url('/baz');
312300
$rootScope.$digest();
313-
314-
expect($route.current.templateUrl).toEqual('foo.html');
315-
expect($route.current.controller).toBeUndefined();
316-
expect(onChangeSpy).toHaveBeenCalled();
301+
expect($route.current.templateUrl).toBe('baz.html');
317302
});
318303
});
319304

320305

321-
it('should chain whens and otherwise', function() {
322-
module(function($routeProvider){
323-
$routeProvider.when('/foo', {templateUrl: 'foo.html'}).
324-
otherwise({templateUrl: 'bar.html'}).
325-
when('/baz', {templateUrl: 'baz.html'});
306+
describe('otherwise', function() {
307+
308+
it('should handle unknown routes with "otherwise" route definition', function() {
309+
function NotFoundCtrl() {}
310+
311+
module(function($routeProvider){
312+
$routeProvider.when('/foo', {templateUrl: 'foo.html'});
313+
$routeProvider.otherwise({templateUrl: '404.html', controller: NotFoundCtrl});
314+
});
315+
316+
inject(function($route, $location, $rootScope) {
317+
var onChangeSpy = jasmine.createSpy('onChange');
318+
319+
$rootScope.$on('$routeChangeStart', onChangeSpy);
320+
expect($route.current).toBeUndefined();
321+
expect(onChangeSpy).not.toHaveBeenCalled();
322+
323+
$location.path('/unknownRoute');
324+
$rootScope.$digest();
325+
326+
expect($route.current.templateUrl).toBe('404.html');
327+
expect($route.current.controller).toBe(NotFoundCtrl);
328+
expect(onChangeSpy).toHaveBeenCalled();
329+
330+
onChangeSpy.reset();
331+
$location.path('/foo');
332+
$rootScope.$digest();
333+
334+
expect($route.current.templateUrl).toEqual('foo.html');
335+
expect($route.current.controller).toBeUndefined();
336+
expect(onChangeSpy).toHaveBeenCalled();
337+
});
326338
});
327339

328-
inject(function($route, $location, $rootScope) {
329-
$rootScope.$digest();
330-
expect($route.current.templateUrl).toBe('bar.html');
331340

332-
$location.url('/baz');
333-
$rootScope.$digest();
334-
expect($route.current.templateUrl).toBe('baz.html');
341+
it('should update $route.current and $route.next when default route is matched', function() {
342+
module(function($routeProvider){
343+
$routeProvider.when('/foo', {templateUrl: 'foo.html'});
344+
$routeProvider.otherwise({templateUrl: '404.html'});
345+
});
346+
347+
inject(function($route, $location, $rootScope) {
348+
var currentRoute, nextRoute,
349+
onChangeSpy = jasmine.createSpy('onChange').andCallFake(function(e, next) {
350+
currentRoute = $route.current;
351+
nextRoute = next;
352+
});
353+
354+
355+
// init
356+
$rootScope.$on('$routeChangeStart', onChangeSpy);
357+
expect($route.current).toBeUndefined();
358+
expect(onChangeSpy).not.toHaveBeenCalled();
359+
360+
361+
// match otherwise route
362+
$location.path('/unknownRoute');
363+
$rootScope.$digest();
364+
365+
expect(currentRoute).toBeUndefined();
366+
expect(nextRoute.templateUrl).toBe('404.html');
367+
expect($route.current.templateUrl).toBe('404.html');
368+
expect(onChangeSpy).toHaveBeenCalled();
369+
onChangeSpy.reset();
370+
371+
// match regular route
372+
$location.path('/foo');
373+
$rootScope.$digest();
374+
375+
expect(currentRoute.templateUrl).toBe('404.html');
376+
expect(nextRoute.templateUrl).toBe('foo.html');
377+
expect($route.current.templateUrl).toEqual('foo.html');
378+
expect(onChangeSpy).toHaveBeenCalled();
379+
onChangeSpy.reset();
380+
381+
// match otherwise route again
382+
$location.path('/anotherUnknownRoute');
383+
$rootScope.$digest();
384+
385+
expect(currentRoute.templateUrl).toBe('foo.html');
386+
expect(nextRoute.templateUrl).toBe('404.html');
387+
expect($route.current.templateUrl).toEqual('404.html');
388+
expect(onChangeSpy).toHaveBeenCalled();
389+
});
335390
});
336391
});
337392

0 commit comments

Comments
 (0)