88using Microsoft . AspNetCore . Components . Routing ;
99using Microsoft . AspNetCore . InternalTesting ;
1010
11+ #nullable enable
12+
1113namespace Microsoft . AspNetCore . Components ;
1214
1315public class NavigationManagerTest
@@ -893,32 +895,30 @@ public void OnNavigateToCallback_WhenThrows_ShouldBeHandledGracefully()
893895 // Arrange
894896 var baseUri = "scheme://host/" ;
895897 var uri = "scheme://host/test" ;
896- var testNavManager = new TestNavigationManagerWithCallback ( ) ;
897- var exceptionThrown = false ;
898+ var testNavManager = new TestNavigationManagerWithExceptionHandling ( baseUri ) ;
898899 var expectedException = new InvalidOperationException ( "Test exception from OnNavigateTo" ) ;
899900
900- // Configure the onNavigateTo callback to throw an exception
901- testNavManager . ConfigureOnNavigateToCallback ( throwingUri =>
902- {
903- exceptionThrown = true ;
904- throw expectedException ;
905- } ) ;
901+ // First test: Initialize with a callback that throws exceptions
902+ testNavManager . Initialize ( baseUri , uri , uri => testNavManager . GetErrorHandledTask ( ThrowingMethod ( uri ) ) ) ;
906903
907- // Act
908- // Initialize the navigation manager with the callback
909- testNavManager . Initialize ( baseUri , uri , testNavManager . GetOnNavigateToCallback ( ) ) ;
904+ // Act & Assert
905+ // Verify that the wrapped callback handles the exception gracefully
906+ var wrappedException = testNavManager . TriggerOnNavigateToCallback ( uri ) ;
910907
911- // Assert
912- Assert . True ( testNavManager . IsInitialized ) ;
908+ // Should be null because the exception was handled gracefully
909+ Assert . Null ( wrappedException ) ;
913910
914- // When navigation is triggered, the exception should be handled gracefully
915- var thrownException = testNavManager . TriggerOnNavigateToCallback ( uri ) ;
911+ // Verify that the exception was logged
912+ Assert . Single ( testNavManager . HandledExceptions ) ;
913+ Assert . Same ( expectedException , testNavManager . HandledExceptions [ 0 ] ) ;
916914
917- // Assert
918- Assert . True ( exceptionThrown , "The OnNavigateTo callback should have been called and thrown an exception." ) ;
919- Assert . Same ( expectedException , thrownException ) ;
915+ async Task ThrowingMethod ( string param )
916+ {
917+ await Task . Yield ( ) ;
918+ throw expectedException ;
919+ }
920920 }
921-
921+
922922 private class TestNavigationManager : NavigationManager
923923 {
924924 public TestNavigationManager ( )
@@ -965,40 +965,27 @@ protected override void HandleLocationChangingHandlerException(Exception ex, Loc
965965 }
966966 }
967967
968- private class TestNavigationManagerWithCallback : TestNavigationManager , IHostEnvironmentNavigationManager
968+ private class TestNavigationManagerWithExceptionHandling : TestNavigationManager , IHostEnvironmentNavigationManager
969969 {
970970 private Func < string , Task > _onNavigateToCallback ;
971971
972- public TestNavigationManagerWithCallback ( )
972+ public List < Exception > HandledExceptions { get ; } = new ( ) ;
973+
974+ public TestNavigationManagerWithExceptionHandling ( string baseUri = null , string uri = null )
975+ : base ( baseUri , uri )
973976 {
974977 }
975978
976979 public void Initialize ( string baseUri , string uri , Func < string , Task > onNavigateTo )
977980 {
978981 _onNavigateToCallback = onNavigateTo ;
979- base . Initialize ( baseUri , uri ) ;
980- }
981-
982- public void ConfigureOnNavigateToCallback ( Func < string , Task > callback )
983- {
984- _onNavigateToCallback = callback ;
985- }
986-
987- public Func < string , Task > GetOnNavigateToCallback ( )
988- {
989- return _onNavigateToCallback ;
990982 }
991983
992- public Exception TriggerOnNavigateToCallback ( string uri )
984+ public Exception ? TriggerOnNavigateToCallback ( string uri )
993985 {
994- if ( _onNavigateToCallback == null )
995- {
996- return null ;
997- }
998-
999986 try
1000987 {
1001- // Simulate the fire-and-forget pattern used in RemoteNavigationManager
988+ // Simulate the fire-and-forget pattern of RemoteNavigationManager
1002989 _ = _onNavigateToCallback ( uri ) ;
1003990 return null ;
1004991 }
@@ -1008,19 +995,25 @@ public Exception TriggerOnNavigateToCallback(string uri)
1008995 }
1009996 }
1010997
1011- public bool IsInitialized
998+ protected override void NavigateToCore ( string uri , bool forceLoad )
1012999 {
1013- get
1000+ // Simulate the behavior where NavigateToCore calls the onNavigateTo callback
1001+ // in a fire-and-forget manner when JSRuntime is not available
1002+ if ( _onNavigateToCallback is not null )
10141003 {
1015- try
1016- {
1017- _ = BaseUri ; // This will throw if not initialized
1018- return true ;
1019- }
1020- catch ( InvalidOperationException )
1021- {
1022- return false ;
1023- }
1004+ _ = _onNavigateToCallback ( uri ) ;
1005+ }
1006+ }
1007+
1008+ public async Task GetErrorHandledTask ( Task taskToHandle )
1009+ {
1010+ try
1011+ {
1012+ await taskToHandle ;
1013+ }
1014+ catch ( Exception ex )
1015+ {
1016+ HandledExceptions . Add ( ex ) ;
10241017 }
10251018 }
10261019 }
0 commit comments