mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
testing_app code review and improvements (#960)
This commit is contained in:
@@ -35,6 +35,8 @@ void main() {
|
|||||||
|
|
||||||
// Tap on the icon.
|
// Tap on the icon.
|
||||||
await tester.tap(iconFinder);
|
await tester.tap(iconFinder);
|
||||||
|
|
||||||
|
// Wait 1 second for the SnackBar to be displayed
|
||||||
await tester.pumpAndSettle(const Duration(seconds: 1));
|
await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||||
|
|
||||||
// Verify if appropriate message appears.
|
// Verify if appropriate message appears.
|
||||||
@@ -42,11 +44,12 @@ void main() {
|
|||||||
|
|
||||||
// Tap on the icon again.
|
// Tap on the icon again.
|
||||||
await tester.tap(iconFinder);
|
await tester.tap(iconFinder);
|
||||||
|
|
||||||
|
// Wait 1 second for the SnackBar to be displayed
|
||||||
await tester.pumpAndSettle(const Duration(seconds: 1));
|
await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||||
|
|
||||||
// Verify if appropriate message appears.
|
// Verify if appropriate message appears.
|
||||||
expect(find.text('Removed from favorites.'), findsOneWidget);
|
expect(find.text('Removed from favorites.'), findsOneWidget);
|
||||||
await tester.pumpAndSettle(const Duration(seconds: 1));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Verifying whether item gets added to favorites',
|
testWidgets('Verifying whether item gets added to favorites',
|
||||||
@@ -55,7 +58,7 @@ void main() {
|
|||||||
|
|
||||||
// Add item to favorites.
|
// Add item to favorites.
|
||||||
await tester.tap(find.byKey(const ValueKey('icon_5')));
|
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.
|
// Tap on the favorites button on the AppBar.
|
||||||
// The Favorites List should appear.
|
// The Favorites List should appear.
|
||||||
@@ -64,10 +67,11 @@ void main() {
|
|||||||
|
|
||||||
// Check if the added item has appeared in the list.
|
// Check if the added item has appeared in the list.
|
||||||
expect(
|
expect(
|
||||||
tester
|
tester
|
||||||
.widget<Text>(find.byKey(const ValueKey('favorites_text_5')))
|
.widget<Text>(find.byKey(const ValueKey('favorites_text_5')))
|
||||||
.data,
|
.data,
|
||||||
equals('Item 5'));
|
equals('Item 5'),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Testing remove button', (tester) async {
|
testWidgets('Testing remove button', (tester) async {
|
||||||
@@ -75,7 +79,7 @@ void main() {
|
|||||||
|
|
||||||
// Add item to favorites.
|
// Add item to favorites.
|
||||||
await tester.tap(find.byKey(const ValueKey('icon_5')));
|
await tester.tap(find.byKey(const ValueKey('icon_5')));
|
||||||
await tester.pumpAndSettle(const Duration(seconds: 1));
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
// Navigate to Favorites screen.
|
// Navigate to Favorites screen.
|
||||||
await tester.tap(find.text('Favorites'));
|
await tester.tap(find.text('Favorites'));
|
||||||
@@ -83,6 +87,8 @@ void main() {
|
|||||||
|
|
||||||
// Tap on the remove icon.
|
// Tap on the remove icon.
|
||||||
await tester.tap(find.byKey(const ValueKey('remove_icon_5')));
|
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));
|
await tester.pumpAndSettle(const Duration(seconds: 1));
|
||||||
|
|
||||||
// Verify if it disappears.
|
// Verify if it disappears.
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import 'package:provider/provider.dart';
|
|||||||
import 'package:testing_app/models/favorites.dart';
|
import 'package:testing_app/models/favorites.dart';
|
||||||
|
|
||||||
class FavoritesPage extends StatelessWidget {
|
class FavoritesPage extends StatelessWidget {
|
||||||
static String routeName = '/favorites_page';
|
static const routeName = '/favorites_page';
|
||||||
|
|
||||||
const FavoritesPage({Key? key}) : super(key: key);
|
const FavoritesPage({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ class FavoriteItemTile extends StatelessWidget {
|
|||||||
key: Key('remove_icon_$itemNo'),
|
key: Key('remove_icon_$itemNo'),
|
||||||
icon: const Icon(Icons.close),
|
icon: const Icon(Icons.close),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Provider.of<Favorites>(context, listen: false).remove(itemNo);
|
context.read<Favorites>().remove(itemNo);
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(
|
const SnackBar(
|
||||||
content: Text('Removed from favorites.'),
|
content: Text('Removed from favorites.'),
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import 'package:testing_app/models/favorites.dart';
|
|||||||
import 'package:testing_app/screens/favorites.dart';
|
import 'package:testing_app/screens/favorites.dart';
|
||||||
|
|
||||||
class HomePage extends StatelessWidget {
|
class HomePage extends StatelessWidget {
|
||||||
static String routeName = '/';
|
static const routeName = '/';
|
||||||
|
|
||||||
const HomePage({Key? key}) : super(key: key);
|
const HomePage({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ class ItemTile extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var favoritesList = Provider.of<Favorites>(context);
|
final favoritesList = context.watch<Favorites>();
|
||||||
|
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
|||||||
@@ -53,15 +53,17 @@ void main() {
|
|||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
// Get the total number of items available.
|
// 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.
|
// Remove one item.
|
||||||
await tester.tap(find.byIcon(Icons.close).first);
|
await tester.tap(find.byIcon(Icons.close).first);
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
// Check if removed properly.
|
// Check if removed properly.
|
||||||
expect(tester.widgetList(find.byIcon(Icons.close)).length,
|
expect(
|
||||||
lessThan(totalItems));
|
tester.widgetList(find.byIcon(Icons.close)).length,
|
||||||
|
lessThan(totalItems),
|
||||||
|
);
|
||||||
|
|
||||||
// Verify if the appropriate message is shown.
|
// Verify if the appropriate message is shown.
|
||||||
expect(find.text('Removed from favorites.'), findsOneWidget);
|
expect(find.text('Removed from favorites.'), findsOneWidget);
|
||||||
|
|||||||
Reference in New Issue
Block a user