Skip to content

Commit

Permalink
widget tests: Add checkErrorDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe authored and gnprice committed Jul 1, 2023
1 parent 33707b6 commit 02fd562
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/widgets/dialog_checks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

/// In a widget test, check that showErrorDialog was called with the right text.
///
/// Checks for an error dialog matching an expected title
/// and, optionally, matching an expected message. Fails if none is found.
///
/// On success, returns the widget's "OK" button.
/// Dismiss the dialog by calling `tester.tap(find.byWidget(okButton))`.
Widget checkErrorDialog(WidgetTester tester, {
required String expectedTitle,
String? expectedMessage,
}) {
final dialog = tester.widget<AlertDialog>(find.byType(AlertDialog));
tester.widget(find.descendant(matchRoot: true,
of: find.byWidget(dialog.title!), matching: find.text(expectedTitle)));
if (expectedMessage != null) {
tester.widget(find.descendant(matchRoot: true,
of: find.byWidget(dialog.content!), matching: find.text(expectedMessage)));
}

return tester.widget(
find.descendant(of: find.byWidget(dialog),
matching: find.widgetWithText(TextButton, 'OK')));
}

0 comments on commit 02fd562

Please sign in to comment.