| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import 'package:flutter/material.dart'; |
| import 'package:flutter_test/flutter_test.dart'; |
| import 'package:go_router/go_router.dart'; |
| |
| import 'test_helpers.dart'; |
| |
| void main() { |
| testWidgets('routing config works', (WidgetTester tester) async { |
| final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>( |
| RoutingConfig( |
| routes: <RouteBase>[ |
| GoRoute(path: '/', builder: (_, __) => const Text('home')), |
| ], |
| redirect: (_, __) => '/', |
| ), |
| ); |
| final GoRouter router = await createRouterWithRoutingConfig(config, tester); |
| expect(find.text('home'), findsOneWidget); |
| |
| router.go('/abcd'); // should be redirected to home |
| await tester.pumpAndSettle(); |
| expect(find.text('home'), findsOneWidget); |
| }); |
| |
| testWidgets('routing config works after builder changes', |
| (WidgetTester tester) async { |
| final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>( |
| RoutingConfig( |
| routes: <RouteBase>[ |
| GoRoute(path: '/', builder: (_, __) => const Text('home')), |
| ], |
| ), |
| ); |
| await createRouterWithRoutingConfig(config, tester); |
| expect(find.text('home'), findsOneWidget); |
| |
| config.value = RoutingConfig( |
| routes: <RouteBase>[ |
| GoRoute(path: '/', builder: (_, __) => const Text('home1')), |
| ], |
| ); |
| await tester.pumpAndSettle(); |
| expect(find.text('home1'), findsOneWidget); |
| }); |
| |
| testWidgets('routing config works after routing changes', |
| (WidgetTester tester) async { |
| final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>( |
| RoutingConfig( |
| routes: <RouteBase>[ |
| GoRoute(path: '/', builder: (_, __) => const Text('home')), |
| ], |
| ), |
| ); |
| final GoRouter router = await createRouterWithRoutingConfig( |
| config, |
| tester, |
| errorBuilder: (_, __) => const Text('error'), |
| ); |
| expect(find.text('home'), findsOneWidget); |
| // Sanity check. |
| router.go('/abc'); |
| await tester.pumpAndSettle(); |
| expect(find.text('error'), findsOneWidget); |
| |
| config.value = RoutingConfig( |
| routes: <RouteBase>[ |
| GoRoute(path: '/', builder: (_, __) => const Text('home')), |
| GoRoute(path: '/abc', builder: (_, __) => const Text('/abc')), |
| ], |
| ); |
| await tester.pumpAndSettle(); |
| expect(find.text('/abc'), findsOneWidget); |
| }); |
| |
| testWidgets('routing config works after routing changes case 2', |
| (WidgetTester tester) async { |
| final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>( |
| RoutingConfig( |
| routes: <RouteBase>[ |
| GoRoute(path: '/', builder: (_, __) => const Text('home')), |
| GoRoute(path: '/abc', builder: (_, __) => const Text('/abc')), |
| ], |
| ), |
| ); |
| final GoRouter router = await createRouterWithRoutingConfig( |
| config, |
| tester, |
| errorBuilder: (_, __) => const Text('error'), |
| ); |
| expect(find.text('home'), findsOneWidget); |
| // Sanity check. |
| router.go('/abc'); |
| await tester.pumpAndSettle(); |
| expect(find.text('/abc'), findsOneWidget); |
| |
| config.value = RoutingConfig( |
| routes: <RouteBase>[ |
| GoRoute(path: '/', builder: (_, __) => const Text('home')), |
| ], |
| ); |
| await tester.pumpAndSettle(); |
| expect(find.text('error'), findsOneWidget); |
| }); |
| } |