Skip to content

Commit

Permalink
Fix: Implement fragment-based redirection in GoRouter test
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
AffanShaikhsurab committed Dec 18, 2024
1 parent 62b6027 commit b948c71
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions packages/go_router/test/go_route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<RouteBase>[
GoRoute(
path: '/',
builder: (_, __) => const Text('home'),
path: '/',
builder: (_, __) => const Text('home'),
routes: <RouteBase>[
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: <RouteBase>[
GoRoute(
path: '1',
builder: (_, __) => const Text('/route/1/2'),
path: '1',
builder: (_, __) => const Text('/route/1'), // Renders "/route/1" text
),
],
),
Expand All @@ -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<NavigatorState> key1 = GlobalKey<NavigatorState>();
Expand Down

0 comments on commit b948c71

Please sign in to comment.