@@ -249,6 +249,58 @@ void main() {
249249 expect (tester.takeException (), isAssertionError);
250250 });
251251
252+ testWidgets ('redirects to a valid route based on fragment.' ,
253+ (WidgetTester tester) async {
254+ final GoRouter router = await createRouter (
255+ < RouteBase > [
256+ GoRoute (
257+ path: '/' ,
258+ builder: (_, __) => const Text ('home' ),
259+ routes: < RouteBase > [
260+ GoRoute (
261+ path: 'route' ,
262+ name: 'route' ,
263+ redirect: (BuildContext context, GoRouterState state) {
264+ // Redirection logic based on the fragment in the URI
265+ if (state.uri.fragment == '1' ) {
266+ // If fragment is "1", redirect to "/route/1"
267+ return '/route/1' ;
268+ }
269+ return null ; // No redirection for other cases
270+ },
271+ routes: < RouteBase > [
272+ GoRoute (
273+ path: '1' ,
274+ builder: (_, __) =>
275+ const Text ('/route/1' ), // Renders "/route/1" text
276+ ),
277+ ],
278+ ),
279+ ],
280+ ),
281+ ],
282+ tester,
283+ );
284+ // Verify that the root route ("/") initially displays the "home" text
285+ expect (find.text ('home' ), findsOneWidget);
286+
287+ // Generate a location string for the named route "route" with fragment "2"
288+ final String locationWithFragment =
289+ router.namedLocation ('route' , fragment: '2' );
290+ expect (locationWithFragment,
291+ '/route#2' ); // Expect the generated location to be "/route#2"
292+
293+ // Navigate to the named route "route" with fragment "1"
294+ router.goNamed ('route' , fragment: '1' );
295+ await tester.pumpAndSettle ();
296+
297+ // Verify that navigating to "/route" with fragment "1" redirects to "/route/1"
298+ expect (find.text ('/route/1' ), findsOneWidget);
299+
300+ // Ensure no exceptions occurred during navigation
301+ expect (tester.takeException (), isNull);
302+ });
303+
252304 testWidgets ('throw if sub route does not conform with parent navigator key' ,
253305 (WidgetTester tester) async {
254306 final GlobalKey <NavigatorState > key1 = GlobalKey <NavigatorState >();
0 commit comments