@@ -440,4 +440,127 @@ describe('useBlocker', () => {
440
440
441
441
expect ( window . location . pathname ) . toBe ( '/invoices' )
442
442
} )
443
+
444
+ test ( 'should allow navigation from 404 page when blocker is active' , async ( ) => {
445
+ const rootRoute = createRootRoute ( {
446
+ notFoundComponent : ( ) => {
447
+ const navigate = useNavigate ( )
448
+
449
+ useBlocker ( { shouldBlockFn : ( ) => true } )
450
+
451
+ return (
452
+ < >
453
+ < h1 > Not Found</ h1 >
454
+ < button onClick = { ( ) => navigate ( { to : '/' } ) } > Go Home</ button >
455
+ < button onClick = { ( ) => navigate ( { to : '/posts' } ) } >
456
+ Go to Posts
457
+ </ button >
458
+ </ >
459
+ )
460
+ } ,
461
+ } )
462
+
463
+ const indexRoute = createRoute ( {
464
+ getParentRoute : ( ) => rootRoute ,
465
+ path : '/' ,
466
+ component : ( ) => {
467
+ return (
468
+ < >
469
+ < h1 > Index</ h1 >
470
+ </ >
471
+ )
472
+ } ,
473
+ } )
474
+
475
+ const postsRoute = createRoute ( {
476
+ getParentRoute : ( ) => rootRoute ,
477
+ path : '/posts' ,
478
+ component : ( ) => {
479
+ return (
480
+ < >
481
+ < h1 > Posts</ h1 >
482
+ </ >
483
+ )
484
+ } ,
485
+ } )
486
+
487
+ const router = createRouter ( {
488
+ routeTree : rootRoute . addChildren ( [ indexRoute , postsRoute ] ) ,
489
+ history,
490
+ } )
491
+
492
+ render ( < RouterProvider router = { router } /> )
493
+
494
+ await router . navigate ( { to : '/non-existent' as any } )
495
+
496
+ expect (
497
+ await screen . findByRole ( 'heading' , { name : 'Not Found' } ) ,
498
+ ) . toBeInTheDocument ( )
499
+
500
+ expect ( window . location . pathname ) . toBe ( '/non-existent' )
501
+
502
+ const homeButton = await screen . findByRole ( 'button' , { name : 'Go Home' } )
503
+ fireEvent . click ( homeButton )
504
+
505
+ expect (
506
+ await screen . findByRole ( 'heading' , { name : 'Index' } ) ,
507
+ ) . toBeInTheDocument ( )
508
+
509
+ expect ( window . location . pathname ) . toBe ( '/' )
510
+ } )
511
+
512
+ test ( 'should handle blocker navigation from 404 to another 404' , async ( ) => {
513
+ const rootRoute = createRootRoute ( {
514
+ notFoundComponent : ( ) => {
515
+ const navigate = useNavigate ( )
516
+
517
+ useBlocker ( { shouldBlockFn : ( ) => true } )
518
+
519
+ return (
520
+ < >
521
+ < h1 > Not Found</ h1 >
522
+ < button onClick = { ( ) => navigate ( { to : '/another-404' as any } ) } >
523
+ Go to Another 404
524
+ </ button >
525
+ </ >
526
+ )
527
+ } ,
528
+ } )
529
+
530
+ const indexRoute = createRoute ( {
531
+ getParentRoute : ( ) => rootRoute ,
532
+ path : '/' ,
533
+ component : ( ) => {
534
+ return (
535
+ < >
536
+ < h1 > Index</ h1 >
537
+ </ >
538
+ )
539
+ } ,
540
+ } )
541
+
542
+ const router = createRouter ( {
543
+ routeTree : rootRoute . addChildren ( [ indexRoute ] ) ,
544
+ history,
545
+ } )
546
+
547
+ render ( < RouterProvider router = { router } /> )
548
+
549
+ await router . navigate ( { to : '/non-existent' } )
550
+
551
+ expect (
552
+ await screen . findByRole ( 'heading' , { name : 'Not Found' } ) ,
553
+ ) . toBeInTheDocument ( )
554
+
555
+ const anotherButton = await screen . findByRole ( 'button' , {
556
+ name : 'Go to Another 404' ,
557
+ } )
558
+ fireEvent . click ( anotherButton )
559
+
560
+ expect (
561
+ await screen . findByRole ( 'heading' , { name : 'Not Found' } ) ,
562
+ ) . toBeInTheDocument ( )
563
+
564
+ expect ( window . location . pathname ) . toBe ( '/non-existent' )
565
+ } )
443
566
} )
0 commit comments