From ea624af0f982e6adcd63f8549c4faf34a5a0cc83 Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Sat, 11 Dec 2021 07:39:07 +0100 Subject: [PATCH] testing_app code review and improvements (#960) --- testing_app/integration_test/app_test.dart | 20 +++++++++++++------- testing_app/lib/screens/favorites.dart | 4 ++-- testing_app/lib/screens/home.dart | 4 ++-- testing_app/test/favorites_test.dart | 8 +++++--- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/testing_app/integration_test/app_test.dart b/testing_app/integration_test/app_test.dart index e3c5bfa77..13c6cf997 100644 --- a/testing_app/integration_test/app_test.dart +++ b/testing_app/integration_test/app_test.dart @@ -35,6 +35,8 @@ void main() { // Tap on the icon. await tester.tap(iconFinder); + + // Wait 1 second for the SnackBar to be displayed await tester.pumpAndSettle(const Duration(seconds: 1)); // Verify if appropriate message appears. @@ -42,11 +44,12 @@ void main() { // Tap on the icon again. await tester.tap(iconFinder); + + // Wait 1 second for the SnackBar to be displayed await tester.pumpAndSettle(const Duration(seconds: 1)); // Verify if appropriate message appears. expect(find.text('Removed from favorites.'), findsOneWidget); - await tester.pumpAndSettle(const Duration(seconds: 1)); }); testWidgets('Verifying whether item gets added to favorites', @@ -55,7 +58,7 @@ void main() { // Add item to favorites. await tester.tap(find.byKey(const ValueKey('icon_5'))); - await tester.pumpAndSettle(const Duration(seconds: 1)); + await tester.pumpAndSettle(); // Tap on the favorites button on the AppBar. // The Favorites List should appear. @@ -64,10 +67,11 @@ void main() { // Check if the added item has appeared in the list. expect( - tester - .widget(find.byKey(const ValueKey('favorites_text_5'))) - .data, - equals('Item 5')); + tester + .widget(find.byKey(const ValueKey('favorites_text_5'))) + .data, + equals('Item 5'), + ); }); testWidgets('Testing remove button', (tester) async { @@ -75,7 +79,7 @@ void main() { // Add item to favorites. await tester.tap(find.byKey(const ValueKey('icon_5'))); - await tester.pumpAndSettle(const Duration(seconds: 1)); + await tester.pumpAndSettle(); // Navigate to Favorites screen. await tester.tap(find.text('Favorites')); @@ -83,6 +87,8 @@ void main() { // Tap on the remove icon. await tester.tap(find.byKey(const ValueKey('remove_icon_5'))); + + // Wait 1 second for the SnackBar to be displayed await tester.pumpAndSettle(const Duration(seconds: 1)); // Verify if it disappears. diff --git a/testing_app/lib/screens/favorites.dart b/testing_app/lib/screens/favorites.dart index 46e722b6c..03d6716e0 100644 --- a/testing_app/lib/screens/favorites.dart +++ b/testing_app/lib/screens/favorites.dart @@ -7,7 +7,7 @@ import 'package:provider/provider.dart'; import 'package:testing_app/models/favorites.dart'; class FavoritesPage extends StatelessWidget { - static String routeName = '/favorites_page'; + static const routeName = '/favorites_page'; const FavoritesPage({Key? key}) : super(key: key); @@ -54,7 +54,7 @@ class FavoriteItemTile extends StatelessWidget { key: Key('remove_icon_$itemNo'), icon: const Icon(Icons.close), onPressed: () { - Provider.of(context, listen: false).remove(itemNo); + context.read().remove(itemNo); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Removed from favorites.'), diff --git a/testing_app/lib/screens/home.dart b/testing_app/lib/screens/home.dart index e15a31d57..53c217a25 100644 --- a/testing_app/lib/screens/home.dart +++ b/testing_app/lib/screens/home.dart @@ -8,7 +8,7 @@ import 'package:testing_app/models/favorites.dart'; import 'package:testing_app/screens/favorites.dart'; class HomePage extends StatelessWidget { - static String routeName = '/'; + static const routeName = '/'; const HomePage({Key? key}) : super(key: key); @@ -46,7 +46,7 @@ class ItemTile extends StatelessWidget { @override Widget build(BuildContext context) { - var favoritesList = Provider.of(context); + final favoritesList = context.watch(); return Padding( padding: const EdgeInsets.all(8.0), diff --git a/testing_app/test/favorites_test.dart b/testing_app/test/favorites_test.dart index ea5d73e77..636a2a806 100644 --- a/testing_app/test/favorites_test.dart +++ b/testing_app/test/favorites_test.dart @@ -53,15 +53,17 @@ void main() { await tester.pumpAndSettle(); // Get the total number of items available. - var totalItems = tester.widgetList(find.byIcon(Icons.close)).length; + final totalItems = tester.widgetList(find.byIcon(Icons.close)).length; // Remove one item. await tester.tap(find.byIcon(Icons.close).first); await tester.pumpAndSettle(); // Check if removed properly. - expect(tester.widgetList(find.byIcon(Icons.close)).length, - lessThan(totalItems)); + expect( + tester.widgetList(find.byIcon(Icons.close)).length, + lessThan(totalItems), + ); // Verify if the appropriate message is shown. expect(find.text('Removed from favorites.'), findsOneWidget);