@@ -285,53 +285,108 @@ describe('$route', function() {
285
285
} ) ;
286
286
287
287
288
- it ( 'should handle unknown routes with "otherwise" route definition' , function ( ) {
289
- function NotFoundCtrl ( ) { }
290
-
288
+ it ( 'should chain whens and otherwise' , function ( ) {
291
289
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' } ) ;
294
293
} ) ;
295
294
296
295
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' ) ;
304
296
$rootScope . $digest ( ) ;
297
+ expect ( $route . current . templateUrl ) . toBe ( 'bar.html' ) ;
305
298
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' ) ;
312
300
$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' ) ;
317
302
} ) ;
318
303
} ) ;
319
304
320
305
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
+ } ) ;
326
338
} ) ;
327
339
328
- inject ( function ( $route , $location , $rootScope ) {
329
- $rootScope . $digest ( ) ;
330
- expect ( $route . current . templateUrl ) . toBe ( 'bar.html' ) ;
331
340
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
+ } ) ;
335
390
} ) ;
336
391
} ) ;
337
392
0 commit comments