From 83f3b34f38f93ec525f20250e579117eb9c23370 Mon Sep 17 00:00:00 2001 From: Pascal Welsch Date: Thu, 21 Mar 2024 18:40:27 +0100 Subject: [PATCH] Automatically pump after act.tap() (#52) --- lib/src/act/act.dart | 6 ++++-- test/act/act_test.dart | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/src/act/act.dart b/lib/src/act/act.dart index ff48c408..0b657b18 100644 --- a/lib/src/act/act.dart +++ b/lib/src/act/act.dart @@ -34,7 +34,7 @@ class Act { selector.snapshot().existsOnce(); return TestAsyncUtils.guard(() async { - final binding = WidgetsBinding.instance as TestWidgetsFlutterBinding; + final binding = TestWidgetsFlutterBinding.instance; final editableText = spot().withParent(selector); final element = editableText.snapshot().discoveredElement; @@ -106,7 +106,7 @@ class Act { snapshot: snapshot, ); - final binding = WidgetsBinding.instance; + final binding = TestWidgetsFlutterBinding.instance; // Finally, tap the widget by sending a down and up event. final downEvent = PointerDownEvent(position: centerPosition); @@ -114,6 +114,8 @@ class Act { final upEvent = PointerUpEvent(position: centerPosition); binding.handlePointerEvent(upEvent); + + await binding.pump(); }); }); } diff --git a/test/act/act_test.dart b/test/act/act_test.dart index 3bb5b3cf..039829b1 100644 --- a/test/act/act_test.dart +++ b/test/act/act_test.dart @@ -38,6 +38,24 @@ void actTests() { expect(i, 2); }); + testWidgets('tap pumps a new frame', (tester) async { + await tester.pumpWidget(const ColorToggleApp()); + + final app = spot(); + app.existsOnce().hasWidgetProp( + prop: widgetProp('color', (w) => w.color), + match: (it) => it.equals(Colors.blue), + ); + final button = spot(); + + await act.tap(button); + // without the automatic pump() inside tap(), the color would not have change + app.existsOnce().hasWidgetProp( + prop: widgetProp('color', (w) => w.color), + match: (it) => it.equals(Colors.red), + ); + }); + testWidgets('tap must be awaited', (tester) async { await tester.pumpWidget( MaterialApp( @@ -292,6 +310,34 @@ void actTests() { }); } +class ColorToggleApp extends StatefulWidget { + const ColorToggleApp({super.key}); + + @override + State createState() => _ColorToggleAppState(); +} + +class _ColorToggleAppState extends State { + bool _red = false; + + @override + Widget build(BuildContext context) { + return MaterialApp( + color: _red ? Colors.red : Colors.blue, + home: Center( + child: ElevatedButton( + onPressed: () { + setState(() { + _red = !_red; + }); + }, + child: null, + ), + ), + ); + } +} + class _NonCartesianWidget extends StatelessWidget { @override Widget build(BuildContext context) => const SizedBox();