-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[go_router] improve coverage #977
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
c185e4b
test: adds `GoRouterHelper` tests
Ascenio 4c5d57b
test: adds `InheritedGoRouter` tests
Ascenio 2b9b1ad
test: adds cupertino related tests
Ascenio 27be0f0
fix: switches from backticks to square brackets
Ascenio fb08dc9
test: adds material related tests
Ascenio 1c4a1e4
test: adds `GoRouterErrorScreen` tests
Ascenio 5543ace
test: adds transitions related tests
Ascenio 7a658f9
test: adds `GoRouterDelegate` tests
Ascenio 03c7a08
test: adds `GoRouter` tests
Ascenio 031a3bb
test: adds `GoRoute` tests
Ascenio a8811d4
test: adds more transitions related tests
Ascenio 8de564b
docs: updates changelog
Ascenio 4cf6986
refactor: moves the tests to correct folder
Ascenio f6dacbf
chore: adds license header to tests
Ascenio 0775847
fix: asserts on expected values of GoRouterTest
Ascenio fbe702b
refactor: renames extensions' tests
Ascenio 369a8b8
refactor: merges GoRouter related tests
Ascenio cffa96c
chore: applies latest lint changes
Ascenio f87f52e
refactor: tests `NoTransitionPage` based on widget's position
Ascenio 06933d8
refactor: small formatting on widget test
Ascenio 563205e
chore: bump version
Ascenio d191c94
refactor: move tests to `test/`
Ascenio f5128fc
fix: fixes import
Ascenio 28594db
Merge branch 'main' into main
Ascenio 7bca0ca
docs: updates CHANGELOG
Ascenio 836a8c6
refactor: rename `MockGoRouterRefreshStream` to `GoRouterRefreshStrea…
Ascenio e06b294
refactor: rename test
Ascenio bf52462
refactor: extract error screens' tests into helpers
Ascenio 8d105cc
docs: adds missing license to test helpers
Ascenio 36373d3
feat: make test helpers integrate better with IDEs
Ascenio fce5f44
feat: makes InheritedGoRouter update when goRouter changes
Ascenio d51df66
feat: warn users about required builder on `GoRoute`
Ascenio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/go_router/test/custom_transition_page_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // 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'; | ||
|
|
||
| void main() { | ||
| testWidgets('CustomTransitionPage builds its child using transitionsBuilder', | ||
| (WidgetTester tester) async { | ||
| const HomeScreen child = HomeScreen(); | ||
| final CustomTransitionPage<void> transition = CustomTransitionPage<void>( | ||
| transitionsBuilder: expectAsync4((_, __, ___, Widget child) => child), | ||
| child: child, | ||
| ); | ||
| final GoRouter router = GoRouter( | ||
| routes: <GoRoute>[ | ||
| GoRoute( | ||
| path: '/', | ||
| pageBuilder: (_, __) => transition, | ||
| ), | ||
| ], | ||
| ); | ||
| await tester.pumpWidget( | ||
| MaterialApp.router( | ||
| routeInformationParser: router.routeInformationParser, | ||
| routerDelegate: router.routerDelegate, | ||
| title: 'GoRouter Example', | ||
| ), | ||
| ); | ||
| expect(find.byWidget(child), findsOneWidget); | ||
| }); | ||
|
|
||
| testWidgets('NoTransitionPage does not apply any transition', | ||
| (WidgetTester tester) async { | ||
| final ValueNotifier<bool> showHomeValueNotifier = | ||
| ValueNotifier<bool>(false); | ||
| await tester.pumpWidget( | ||
| MaterialApp( | ||
| home: ValueListenableBuilder<bool>( | ||
| valueListenable: showHomeValueNotifier, | ||
| builder: (_, bool showHome, __) { | ||
| return Navigator( | ||
| pages: <Page<void>>[ | ||
| const NoTransitionPage<void>( | ||
| child: LoginScreen(), | ||
| ), | ||
| if (showHome) | ||
| const NoTransitionPage<void>( | ||
| child: HomeScreen(), | ||
| ), | ||
| ], | ||
| onPopPage: (Route<dynamic> route, dynamic result) { | ||
| return route.didPop(result); | ||
| }, | ||
| ); | ||
| }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| final Finder homeScreenFinder = find.byType(HomeScreen); | ||
|
|
||
| showHomeValueNotifier.value = true; | ||
| await tester.pump(); | ||
| final Offset homeScreenPositionInTheMiddleOfAddition = | ||
| tester.getTopLeft(homeScreenFinder); | ||
| await tester.pumpAndSettle(); | ||
| final Offset homeScreenPositionAfterAddition = | ||
| tester.getTopLeft(homeScreenFinder); | ||
|
|
||
| showHomeValueNotifier.value = false; | ||
| await tester.pump(); | ||
| final Offset homeScreenPositionInTheMiddleOfRemoval = | ||
| tester.getTopLeft(homeScreenFinder); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect( | ||
| homeScreenPositionInTheMiddleOfAddition, | ||
| homeScreenPositionAfterAddition, | ||
| ); | ||
| expect( | ||
| homeScreenPositionAfterAddition, | ||
| homeScreenPositionInTheMiddleOfRemoval, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| class HomeScreen extends StatelessWidget { | ||
| const HomeScreen({Key? key}) : super(key: key); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return const Scaffold( | ||
| body: Center( | ||
| child: Text('HomeScreen'), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class LoginScreen extends StatelessWidget { | ||
| const LoginScreen({Key? key}) : super(key: key); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return const Scaffold( | ||
| body: Center( | ||
| child: Text('LoginScreen'), | ||
| ), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // 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/cupertino.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:go_router/go_router.dart'; | ||
|
|
||
| import 'go_router_test.dart'; | ||
|
|
||
| WidgetTesterCallback testPageNotFound({required Widget widget}) { | ||
| return (WidgetTester tester) async { | ||
| await tester.pumpWidget(widget); | ||
| expect(find.text('page not found'), findsOneWidget); | ||
| }; | ||
| } | ||
|
|
||
| WidgetTesterCallback testPageShowsExceptionMessage({ | ||
| required Exception exception, | ||
| required Widget widget, | ||
| }) { | ||
| return (WidgetTester tester) async { | ||
| await tester.pumpWidget(widget); | ||
| expect(find.text('$exception'), findsOneWidget); | ||
| }; | ||
| } | ||
|
|
||
| WidgetTesterCallback testClickingTheButtonRedirectsToRoot({ | ||
| required Finder buttonFinder, | ||
| required Widget widget, | ||
| Widget Function(GoRouter router) appRouterBuilder = materialAppRouterBuilder, | ||
| }) { | ||
| return (WidgetTester tester) async { | ||
| final GoRouter router = GoRouter( | ||
| initialLocation: '/error', | ||
| routes: <GoRoute>[ | ||
| GoRoute(path: '/', builder: (_, __) => const DummyStatefulWidget()), | ||
| GoRoute( | ||
| path: '/error', | ||
| builder: (_, __) => widget, | ||
| ), | ||
| ], | ||
| ); | ||
| await tester.pumpWidget(appRouterBuilder(router)); | ||
| await tester.tap(buttonFinder); | ||
| await tester.pumpAndSettle(); | ||
| expect(find.byType(DummyStatefulWidget), findsOneWidget); | ||
| }; | ||
| } | ||
|
|
||
| Widget materialAppRouterBuilder(GoRouter router) { | ||
| return MaterialApp.router( | ||
| routeInformationParser: router.routeInformationParser, | ||
| routerDelegate: router.routerDelegate, | ||
| title: 'GoRouter Example', | ||
| ); | ||
| } | ||
|
|
||
| Widget cupertinoAppRouterBuilder(GoRouter router) { | ||
| return CupertinoApp.router( | ||
| routeInformationParser: router.routeInformationParser, | ||
| routerDelegate: router.routerDelegate, | ||
| title: 'GoRouter Example', | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // 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_test/flutter_test.dart'; | ||
| import 'package:go_router/go_router.dart'; | ||
|
|
||
| void main() { | ||
| test('throws when a builder is not set', () { | ||
Ascenio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expect(() => GoRoute(path: '/'), throwsException); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // 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/cupertino.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:go_router/src/go_router_cupertino.dart'; | ||
|
|
||
| import 'error_screen_helpers.dart'; | ||
|
|
||
| void main() { | ||
| group('isCupertinoApp', () { | ||
| testWidgets('returns [true] when CupertinoApp is present', | ||
| (WidgetTester tester) async { | ||
| final GlobalKey<_DummyStatefulWidgetState> key = | ||
| GlobalKey<_DummyStatefulWidgetState>(); | ||
| await tester.pumpWidget( | ||
| CupertinoApp( | ||
| home: DummyStatefulWidget(key: key), | ||
| ), | ||
| ); | ||
| final bool isCupertino = isCupertinoApp(key.currentContext! as Element); | ||
| expect(isCupertino, true); | ||
| }); | ||
|
|
||
| testWidgets('returns [false] when MaterialApp is present', | ||
| (WidgetTester tester) async { | ||
| final GlobalKey<_DummyStatefulWidgetState> key = | ||
| GlobalKey<_DummyStatefulWidgetState>(); | ||
| await tester.pumpWidget( | ||
| MaterialApp( | ||
| home: DummyStatefulWidget(key: key), | ||
| ), | ||
| ); | ||
| final bool isCupertino = isCupertinoApp(key.currentContext! as Element); | ||
| expect(isCupertino, false); | ||
| }); | ||
| }); | ||
|
|
||
| test('pageBuilderForCupertinoApp creates a [CupertinoPage] accordingly', () { | ||
| final UniqueKey key = UniqueKey(); | ||
| const String name = 'name'; | ||
| const String arguments = 'arguments'; | ||
| const String restorationId = 'restorationId'; | ||
| const DummyStatefulWidget child = DummyStatefulWidget(); | ||
| final CupertinoPage<void> page = pageBuilderForCupertinoApp( | ||
| key: key, | ||
| name: name, | ||
| arguments: arguments, | ||
| restorationId: restorationId, | ||
| child: child, | ||
| ); | ||
| expect(page.key, key); | ||
| expect(page.name, name); | ||
| expect(page.arguments, arguments); | ||
| expect(page.restorationId, restorationId); | ||
| expect(page.child, child); | ||
| }); | ||
|
|
||
| group('GoRouterCupertinoErrorScreen', () { | ||
| testWidgets( | ||
| 'shows "page not found" by default', | ||
| testPageNotFound( | ||
| widget: const CupertinoApp( | ||
| home: GoRouterCupertinoErrorScreen(null), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| final Exception exception = Exception('Something went wrong!'); | ||
| testWidgets( | ||
| 'shows the exception message when provided', | ||
| testPageShowsExceptionMessage( | ||
| exception: exception, | ||
| widget: CupertinoApp( | ||
| home: GoRouterCupertinoErrorScreen(exception), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| testWidgets( | ||
| 'clicking the CupertinoButton should redirect to /', | ||
| testClickingTheButtonRedirectsToRoot( | ||
| buttonFinder: find.byType(CupertinoButton), | ||
| appRouterBuilder: cupertinoAppRouterBuilder, | ||
| widget: const CupertinoApp( | ||
| home: GoRouterCupertinoErrorScreen(null), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| class DummyStatefulWidget extends StatefulWidget { | ||
| const DummyStatefulWidget({Key? key}) : super(key: key); | ||
|
|
||
| @override | ||
| State<DummyStatefulWidget> createState() => _DummyStatefulWidgetState(); | ||
| } | ||
|
|
||
| class _DummyStatefulWidgetState extends State<DummyStatefulWidget> { | ||
| @override | ||
| Widget build(BuildContext context) => Container(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.