1
1
'use strict' ;
2
- var assert = require ( 'power-assert' ) ;
2
+ var assert = require ( 'assert' ) ;
3
+ var assign = Object . assign || require ( 'object-assign' ) ;
3
4
var React = require ( 'react' ) ;
4
5
var ReactDOM = require ( 'react-dom' ) ;
5
6
var ReactTestUtils = require ( 'react/lib/ReactTestUtils' ) ;
@@ -230,15 +231,19 @@ describe('Routing', function() {
230
231
} ) ;
231
232
232
233
describe ( 'Navigation lifecycle callbacks' , function ( ) {
233
- it ( 'calls onBeforeNaviation and onNavigation' , function ( done ) {
234
+ it ( 'calls onBeforeNavigation and onNavigation' , function ( done ) {
234
235
assertRendered ( 'mainpage' ) ;
235
236
var called = [ ] ;
236
237
app . setState ( {
237
- beforeNavigationHandler : function ( nextPath ) {
238
+ beforeNavigationHandler : function ( nextPath , navigation , match ) {
238
239
called . push ( nextPath ) ;
240
+ assert . equal ( match . match . slug , 'hello' ) ;
241
+ assert ( ! navigation . match ) ;
239
242
} ,
240
- navigationHandler : function ( path ) {
243
+ navigationHandler : function ( path , navigation , match ) {
241
244
called . push ( path ) ;
245
+ assert . equal ( match . match . slug , 'hello' ) ;
246
+ assert ( ! navigation . match ) ;
242
247
}
243
248
} ) ;
244
249
router . navigate ( '/__zuul/hello' , function ( ) {
@@ -390,7 +395,7 @@ describe('Nested routers', function() {
390
395
var NestedRouter = React . createClass ( {
391
396
render : function ( ) {
392
397
return div ( null ,
393
- Locations ( null ,
398
+ Locations ( this . props ,
394
399
Location ( {
395
400
path : '/__zuul/nested/' ,
396
401
handler : div ( null , 'nested/root' )
@@ -405,9 +410,17 @@ describe('Nested routers', function() {
405
410
406
411
var App = React . createClass ( {
407
412
413
+ getInitialState : function ( ) {
414
+ return { } ;
415
+ } ,
416
+
408
417
render : function ( ) {
409
418
return div ( null ,
410
- Locations ( { ref : 'router' , className : 'App' } ,
419
+ Locations ( {
420
+ ref : 'router' , className : 'App' ,
421
+ onNavigation : this . state . navigationHandler ,
422
+ onBeforeNavigation : this . state . beforeNavigationHandler
423
+ } ,
411
424
Location ( {
412
425
path : '/__zuul' ,
413
426
foo : 'bar' ,
@@ -420,7 +433,9 @@ describe('Nested routers', function() {
420
433
} ) ,
421
434
Location ( {
422
435
path : '/__zuul/nested/*' ,
423
- handler : NestedRouter
436
+ handler : NestedRouter ,
437
+ onNavigation : this . state . navigationHandler ,
438
+ onBeforeNavigation : this . state . beforeNavigationHandler
424
439
} )
425
440
) ,
426
441
CaptureClicks ( { gotoURL : this . gotoURL } ,
@@ -457,6 +472,42 @@ describe('Nested routers', function() {
457
472
} ) ;
458
473
} ) ;
459
474
475
+ it ( 'calls onBeforeNaviation and onNavigation with correct match objects w/ multiple routers' , function ( done ) {
476
+ assertRendered ( 'mainpage' ) ;
477
+ var called = [ ] ;
478
+ router . navigate ( '/__zuul/nested/' ) ;
479
+ // Goes beforeNav, beforeNav, nav, nav
480
+ app . setState ( {
481
+ beforeNavigationHandler : function ( nextPath , navigation , match ) {
482
+ called . push ( nextPath ) ;
483
+ if ( called . length === 1 ) {
484
+ assert . equal ( match . match . _ [ 0 ] , 'page' ) ;
485
+ assert . equal ( match . matchedPath , '/__zuul/nested/' ) ;
486
+ } else {
487
+ assert . equal ( match . matchedPath , '/__zuul/nested/page' ) ;
488
+ }
489
+ assert ( ! navigation . match ) ;
490
+ } ,
491
+ navigationHandler : function ( path , navigation , match ) {
492
+ called . push ( path ) ;
493
+ if ( called . length === 3 ) {
494
+ assert . equal ( match . match . _ [ 0 ] , 'page' ) ;
495
+ assert . equal ( match . matchedPath , '/__zuul/nested/' ) ;
496
+ } else {
497
+ assert . equal ( match . matchedPath , '/__zuul/nested/page' ) ;
498
+ }
499
+ assert ( ! navigation . match ) ;
500
+ }
501
+ } ) ;
502
+ router . navigate ( '/__zuul/nested/page' , function ( ) {
503
+ assert . equal ( called . length , 4 ) ;
504
+ called . forEach ( function ( c ) {
505
+ assert . equal ( c , '/__zuul/nested/page' ) ;
506
+ } ) ;
507
+ done ( ) ;
508
+ } ) ;
509
+ } ) ;
510
+
460
511
describe ( 'CaptureClicks component' , function ( ) {
461
512
it ( 'navigates to a subroute via onClick event' , function ( done ) {
462
513
assertRendered ( 'mainpage' ) ;
@@ -498,7 +549,7 @@ describe('Contextual routers', function() {
498
549
499
550
render : function ( ) {
500
551
return div ( null ,
501
- Locations ( { ref : 'router' , contextual : true } ,
552
+ Locations ( assign ( { ref : 'router' , contextual : true } , this . props ) ,
502
553
Location ( {
503
554
path : '/' ,
504
555
handler : div ( null , 'subcat/root' )
@@ -519,16 +570,26 @@ describe('Contextual routers', function() {
519
570
520
571
var App = React . createClass ( {
521
572
573
+ getInitialState : function ( ) {
574
+ return { } ;
575
+ } ,
576
+
522
577
render : function ( ) {
523
- return Locations ( { ref : 'router' } ,
578
+ return Locations ( {
579
+ ref : 'router' ,
580
+ onNavigation : this . state . navigationHandler ,
581
+ onBeforeNavigation : this . state . beforeNavigationHandler
582
+ } ,
524
583
Location ( {
525
584
path : '/__zuul' ,
526
585
handler : div ( null , "mainpage" )
527
586
} ) ,
528
587
Location ( {
529
588
path : '/__zuul/subcat/*' ,
530
589
handler : SubCat ,
531
- ref : 'subcat'
590
+ ref : 'subcat' ,
591
+ onNavigation : this . state . navigationHandler ,
592
+ onBeforeNavigation : this . state . beforeNavigationHandler
532
593
} )
533
594
) ;
534
595
}
@@ -587,6 +648,39 @@ describe('Contextual routers', function() {
587
648
} ) ;
588
649
} ) ;
589
650
} ) ;
651
+
652
+ it ( 'calls onBeforeNaviation and onNavigation with correct match objects w/ contextual routers' , function ( done ) {
653
+ assertRendered ( 'mainpage' ) ;
654
+ var called = [ ] ;
655
+ router . navigate ( '/__zuul/subcat/' ) ;
656
+ // Goes beforeNav, beforeNav, nav, nav
657
+ app . setState ( {
658
+ beforeNavigationHandler : function ( nextPath , navigation , match ) {
659
+ called . push ( nextPath ) ;
660
+ if ( called . length === 1 ) {
661
+ assert . equal ( match . match . _ [ 0 ] , 'page' ) ;
662
+ assert . equal ( match . matchedPath , '/__zuul/subcat/' ) ;
663
+ } else {
664
+ assert . equal ( match . matchedPath , '/page' ) ;
665
+ }
666
+ assert ( ! navigation . match ) ;
667
+ } ,
668
+ navigationHandler : function ( path , navigation , match ) {
669
+ called . push ( path ) ;
670
+ if ( called . length === 3 ) {
671
+ assert . equal ( match . match . _ [ 0 ] , 'page' ) ;
672
+ assert . equal ( match . matchedPath , '/__zuul/subcat/' ) ;
673
+ } else {
674
+ assert . equal ( match . matchedPath , '/page' ) ;
675
+ }
676
+ assert ( ! navigation . match ) ;
677
+ }
678
+ } ) ;
679
+ router . navigate ( '/__zuul/subcat/page' , function ( ) {
680
+ assert . equal ( called . length , 4 ) ;
681
+ done ( ) ;
682
+ } ) ;
683
+ } ) ;
590
684
} ) ;
591
685
592
686
describe ( 'Multiple active routers' , function ( ) {
0 commit comments