From d04068cf092e6e5aad5a77d12a32465c53f04d4d Mon Sep 17 00:00:00 2001 From: Omar Toutounji Date: Sat, 4 Nov 2023 15:11:53 -0400 Subject: [PATCH] #32: added restore deleted note test coverage --- lib/screens/journal_page.dart | 10 ++--- test/hwyd_sanity_test.dart | 81 ++++++++++++++++------------------- 2 files changed, 41 insertions(+), 50 deletions(-) diff --git a/lib/screens/journal_page.dart b/lib/screens/journal_page.dart index d587042..c52cd8e 100644 --- a/lib/screens/journal_page.dart +++ b/lib/screens/journal_page.dart @@ -204,6 +204,7 @@ class _JournalPageState extends State { }); }), )); + _save(); } TextStyle getStyle(double size) { @@ -257,7 +258,7 @@ class _JournalPageState extends State { padding: const EdgeInsets.only(right: 15.0), child: IconButton( onPressed: notes[currentNoteIndex].text.isEmpty - ? null + ? () => Share.share(greeting) : () => Share.share(notes[currentNoteIndex].text), icon: const Icon(Icons.ios_share)), ), @@ -367,12 +368,7 @@ class _JournalPageState extends State { color: Colors.red, child: const Icon(Icons.delete_forever_outlined)), - onDismissed: (direction) { - // Remove the item from the data source. - setState(() { - _deleteNote(index); - }); - }, + onDismissed: (direction) => _deleteNote(index), child: ListTile( tileColor: currentNoteIndex == index ? Colors.grey diff --git a/test/hwyd_sanity_test.dart b/test/hwyd_sanity_test.dart index f5b764d..b25d95e 100644 --- a/test/hwyd_sanity_test.dart +++ b/test/hwyd_sanity_test.dart @@ -1,17 +1,15 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:hwyd/screens/journal_page.dart'; +import 'package:share_plus/share_plus.dart'; void main() { testWidgets('Hwyd sanity test', (WidgetTester tester) async { - // Isolate the JournalPage widget out of our app safely so we can test it as testWidget + // Isolate the JournalPage widget out of our app safely so we can test + // it as testWidget and set things up Widget testWidget = const MediaQuery( data: MediaQueryData(), child: MaterialApp(home: JournalPage())); - - // Rebuild the widget tree for our test widget await tester.pumpWidget(testWidget); - - // Ensure testWidget is visible await tester.ensureVisible(find.byWidget(testWidget)); // Ensure user can see the menu, share and add icons @@ -19,79 +17,76 @@ void main() { await tester.ensureVisible(find.byIcon(Icons.ios_share)); await tester.ensureVisible(find.byIcon(Icons.add)); - // Search for our hint text and verify there is one widget + // Enter journal: "my day was great!" expect(find.byType(TextField), findsOneWidget); - - // Enter a mock journal await tester.enterText(find.byType(TextField), 'my day was great!'); + await tester.pumpAndSettle(); - // Expect to find the new mock journal on screen + // Ensure text is actually there expect(find.text('my day was great!'), findsOneWidget); - // Press on menu icon to open drawer + // Open drawer await tester.tap(find.byIcon(Icons.menu)); - - // Rebuild widget tree or wait for animation to complete await tester.pumpAndSettle(); - - // Expect the Drawer to be there in widget tree expect(find.byType(Drawer), findsOneWidget); - - // Ensure the Drawer is visible await tester.ensureVisible(find.byType(Drawer)); // Ensure there is one journal in the drawer only expect(find.byType(ListTile), findsOneWidget); - // Tap on rename name button to rename journal + // Rename journal to "journal1" await tester.tap(find.byIcon(Icons.drive_file_rename_outline)); - - // Rename journal to 'journal1'. We're using the first text field we find. await tester.enterText(find.byType(TextField).first, 'journal1'); - - // Expect to find renamed journal in Drawer expect(find.text('journal1'), findsOneWidget); - // Press on menu icon to close drawer. Disable warning since we intend to tap off-screen share button + // Close drawer await tester.tap(find.byIcon(Icons.ios_share), warnIfMissed: false); - - // Rebuild widget tree or wait for animation to complete await tester.pumpAndSettle(); - - // Expect the Drawer to be gone in widget tree expect(find.byType(Drawer), findsNothing); - // Press on add button to create new jounrnal + // Create new journal await tester.tap(find.byIcon(Icons.add)); - - // Rebuild widget tree or wait for animation to complete await tester.pumpAndSettle(); - // Press on menu icon to open drawer + // Open drawer await tester.tap(find.byIcon(Icons.menu)); - - // Rebuild widget tree or wait for animation to complete await tester.pumpAndSettle(); - - // Expect the Drawer to be there in widget tree expect(find.byType(Drawer), findsOneWidget); - - // Ensure the Drawer is visible await tester.ensureVisible(find.byType(Drawer)); - // Ensure there are 2 journals journal in the drawer only + // Ensure there are now 2 journals journal in the drawer only expect(find.byType(ListTile), findsNWidgets(2)); - - // Rebuild widget tree or wait for animation to complete await tester.pumpAndSettle(); - // Dismiss 1 journal + // Ensure snackbar is not visible + expect(find.byType(SnackBarAction), findsNothing); + + // Delete first journal await tester.drag(find.byType(Dismissible).first, const Offset(500, 0)); + await tester.pumpAndSettle(); + expect(find.byType(ListTile), findsOneWidget); + + // Make sure snackbar now appears + expect(find.byType(SnackBarAction), findsOneWidget); + await tester.ensureVisible(find.byType(SnackBarAction)); - // Build the widget until the dismiss animation ends. + // Close drawer + await tester.tap(find.byIcon(Icons.ios_share), warnIfMissed: false); await tester.pumpAndSettle(); + expect(find.byType(Drawer), findsNothing); - // Ensure that one journal is now deleted and there is only 1 left - expect(find.byType(ListTile), findsOneWidget); + // Undo delete journal + await tester.tap(find.widgetWithText(SnackBarAction, "Undo"), + warnIfMissed: false); + await tester.pumpAndSettle(); + + // Open Drawer + await tester.tap(find.byIcon(Icons.menu)); + await tester.pumpAndSettle(); + expect(find.byType(Drawer), findsOneWidget); + await tester.ensureVisible(find.byType(Drawer)); + + // Check deleted journal is now restored + expect(find.byType(ListTile), findsNWidgets(2)); }); }