Testing a DropdownButton
It is quite difficult to write UI tests for a DropdownButton
. Thanks to this StackOverflow post, I was able to extract a minimal test. Here's the snippet:
imports...
void main() {
testWidgets(
'Selects a civilization',
(WidgetTester tester) async {
final picker = CivilizationPicker(...);
await tester.pumpWidget(MaterialApp(home: Scaffold(body: picker)));
// Opens the dropdown.
await tester.tap(find.byType(CivilizationPicker));
await tester.pump();
// It is open, even though the list of options doesn't appear in the screenshot.
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('goldens/create_build_2_select_civ.png'),
);
// Only when the list of options is open do we find 2 widgets for each option.
expect(find.textContaining('Abbasid'), findsNWidgets(2));
// Gotta tap the last of the two items to select the option.
await tester.tap(find.textContaining('Abbasid', skipOffstage: false).last);
await tester.pump();
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('goldens/create_build_3_selected_civ.png'),
);
});
}