Skip to content

Commit

Permalink
#32: added restore deleted note test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
omartoutounji committed Nov 4, 2023
1 parent edf38a0 commit d04068c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 50 deletions.
10 changes: 3 additions & 7 deletions lib/screens/journal_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class _JournalPageState extends State<JournalPage> {
});
}),
));
_save();
}

TextStyle getStyle(double size) {
Expand Down Expand Up @@ -257,7 +258,7 @@ class _JournalPageState extends State<JournalPage> {
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)),
),
Expand Down Expand Up @@ -367,12 +368,7 @@ class _JournalPageState extends State<JournalPage> {
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
Expand Down
81 changes: 38 additions & 43 deletions test/hwyd_sanity_test.dart
Original file line number Diff line number Diff line change
@@ -1,97 +1,92 @@
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
await tester.ensureVisible(find.byIcon(Icons.menu));
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));
});
}

0 comments on commit d04068c

Please sign in to comment.