Skip to content

Commit 04261ee

Browse files
committed
widget tests: Add checkErrorDialog
1 parent aa1bb15 commit 04261ee

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/widgets/dialog_checks.dart

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
/// In a widget test, check that showErrorDialog was called with the right text.
5+
///
6+
/// Checks for an error dialog matching an expected title
7+
/// and, optionally, matching an expected message. Fails if none is found.
8+
///
9+
/// On success, returns the widget's "OK" button.
10+
/// Dismiss the dialog by calling `tester.tap(find.byWidget(okButton))`.
11+
Widget checkErrorDialog(WidgetTester tester, {
12+
required String expectedTitle,
13+
String? expectedMessage,
14+
}) {
15+
final dialog = tester.widget<AlertDialog>(find.byType(AlertDialog));
16+
tester.widget(find.descendant(matchRoot: true,
17+
of: find.byWidget(dialog.title!), matching: find.text(expectedTitle)));
18+
if (expectedMessage != null) {
19+
tester.widget(find.descendant(matchRoot: true,
20+
of: find.byWidget(dialog.content!), matching: find.text(expectedMessage)));
21+
}
22+
23+
return tester.widget(
24+
find.descendant(of: find.byWidget(dialog),
25+
matching: find.widgetWithText(TextButton, 'OK')));
26+
}

0 commit comments

Comments
 (0)