From b948c714280542356a4c6814a27ee15386f10bee Mon Sep 17 00:00:00 2001 From: Affan Date: Wed, 18 Dec 2024 09:36:52 +0530 Subject: [PATCH] Fix: Implement fragment-based redirection in GoRouter test - Added logic to redirect to '/route/1' if the fragment is '1' in the '/route' route. - Updated test to verify correct redirection and ensure no infinite redirects occur. - Ensured proper navigation based on fragment values using router.goNamed(). - Added assertions to check for expected behavior and prevent exceptions during navigation. --- packages/go_router/test/go_route_test.dart | 48 ++++++++++++++-------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/packages/go_router/test/go_route_test.dart b/packages/go_router/test/go_route_test.dart index 2856374f1bb..a67f3a451d4 100644 --- a/packages/go_router/test/go_route_test.dart +++ b/packages/go_router/test/go_route_test.dart @@ -249,21 +249,28 @@ void main() { expect(tester.takeException(), isAssertionError); }); - testWidgets('throw if redirect to itself.', (WidgetTester tester) async { + testWidgets('redirects to a valid route based on fragment.', (WidgetTester tester) async { final GoRouter router = await createRouter( [ GoRoute( - path: '/', - builder: (_, __) => const Text('home'), + path: '/', + builder: (_, __) => const Text('home'), routes: [ GoRoute( - path: 'route', - name: 'route', // Named route - redirect: (_, __) => '/route', + path: 'route', + name: 'route', + redirect: (context, state) { + // Redirection logic based on the fragment in the URI + if (state.uri.fragment == '1') { + // If fragment is "1", redirect to "/route/1" + return '/route/1'; + } + return null; // No redirection for other cases + }, routes: [ GoRoute( - path: '1', - builder: (_, __) => const Text('/route/1/2'), + path: '1', + builder: (_, __) => const Text('/route/1'), // Renders "/route/1" text ), ], ), @@ -272,19 +279,24 @@ void main() { ], tester, ); + // Verify that the root route ("/") initially displays the "home" text expect(find.text('home'), findsOneWidget); - // Test namedLocation with fragment - final String locationWithFragment = - router.namedLocation('route', fragment: '2'); - expect(locationWithFragment, '/route#2'); - // Navigate using goNamed with fragment - router.goNamed('route', fragment: '2'); - await tester.pumpAndSettle(); - // Should redirect to /route/1 without error. - expect(tester.takeException(), isAssertionError); - }); + // Generate a location string for the named route "route" with fragment "2" + final String locationWithFragment = router.namedLocation('route', fragment: '2'); + expect(locationWithFragment, '/route#2'); // Expect the generated location to be "/route#2" + + // Navigate to the named route "route" with fragment "1" + router.goNamed('route', fragment: '1'); + await tester.pumpAndSettle(); + // Verify that navigating to "/route" with fragment "1" redirects to "/route/1" + expect(find.text('/route/1'), findsOneWidget); + + // Ensure no exceptions occurred during navigation + expect(tester.takeException(), isNull); + }); + testWidgets('throw if sub route does not conform with parent navigator key', (WidgetTester tester) async { final GlobalKey key1 = GlobalKey();