Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[go_router_builder] Add test for onExit #6614

Merged
4 changes: 4 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

- Adds an example and a test with `onExit`.

## 2.6.1

* Fixes typo in `durationDecoderHelperName`.
Expand Down
96 changes: 96 additions & 0 deletions packages/go_router_builder/example/lib/on_exit_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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.

// ignore_for_file: public_member_api_docs, unreachable_from_main

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

part 'on_exit_example.g.dart';

void main() => runApp(App());

class App extends StatelessWidget {
App({super.key});

@override
Widget build(BuildContext context) => MaterialApp.router(
routerConfig: _router,
title: _appTitle,
);

final GoRouter _router = GoRouter(routes: $appRoutes);
}

@TypedGoRoute<HomeRoute>(
path: '/',
routes: <TypedGoRoute<GoRouteData>>[
TypedGoRoute<SubRoute>(path: 'sub-route')
],
)
class HomeRoute extends GoRouteData {
const HomeRoute();

@override
Widget build(BuildContext context, GoRouterState state) => const HomeScreen();
}

class SubRoute extends GoRouteData {
const SubRoute();

@override
Future<bool> onExit(BuildContext context, GoRouterState state) async {
final bool? confirmed = await showDialog<bool>(
context: context,
builder: (_) => AlertDialog(
content: const Text('Are you sure to leave this page?'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Confirm'),
),
],
),
);
return confirmed ?? false;
}

@override
Widget build(BuildContext context, GoRouterState state) => const SubScreen();
}

class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text(_appTitle)),
body: Center(
child: ElevatedButton(
onPressed: () => const SubRoute().go(context),
child: const Text('Go to sub screen'),
),
));
}

class SubScreen extends StatelessWidget {
const SubScreen({super.key});

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('$_appTitle Sub screen')),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Go back'),
),
),
);
}

const String _appTitle = 'GoRouter Example: builder';
58 changes: 58 additions & 0 deletions packages/go_router_builder/example/lib/on_exit_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/go_router_builder/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
collection: ^1.15.0
flutter:
sdk: flutter
go_router: ^10.0.0
go_router: ^14.0.0
provider: 6.0.5

dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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_builder_example/on_exit_example.dart';

void main() {
testWidgets('It should trigger the on exit when leaving the route',
(WidgetTester tester) async {
await tester.pumpWidget(App());
expect(find.byType(HomeScreen), findsOne);

await tester.tap(find.widgetWithText(ElevatedButton, 'Go to sub screen'));
await tester.pumpAndSettle();

expect(find.byType(SubScreen), findsOne);

await tester.tap(find.widgetWithText(ElevatedButton, 'Go back'));
await tester.pumpAndSettle();

expect(
find.widgetWithText(AlertDialog, 'Are you sure to leave this page?'),
findsOne,
);
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();

expect(find.byType(HomeScreen), findsNothing);
expect(find.byType(SubScreen), findsOne);

await tester.tap(find.widgetWithText(ElevatedButton, 'Go back'));
await tester.pumpAndSettle();

expect(
find.widgetWithText(AlertDialog, 'Are you sure to leave this page?'),
findsOne,
);
await tester.tap(find.text('Confirm'));
await tester.pumpAndSettle();

expect(find.byType(HomeScreen), findsOne);
expect(find.byType(SubScreen), findsNothing);
});
}