Skip to content

Commit

Permalink
Automatically pump after act.tap() (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy authored Mar 21, 2024
1 parent 4e88ca1 commit 83f3b34
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/src/act/act.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Act {
selector.snapshot().existsOnce();

return TestAsyncUtils.guard<void>(() async {
final binding = WidgetsBinding.instance as TestWidgetsFlutterBinding;
final binding = TestWidgetsFlutterBinding.instance;

final editableText = spot<EditableText>().withParent(selector);
final element = editableText.snapshot().discoveredElement;
Expand Down Expand Up @@ -106,14 +106,16 @@ 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);
binding.handlePointerEvent(downEvent);

final upEvent = PointerUpEvent(position: centerPosition);
binding.handlePointerEvent(upEvent);

await binding.pump();
});
});
}
Expand Down
46 changes: 46 additions & 0 deletions test/act/act_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MaterialApp>();
app.existsOnce().hasWidgetProp(
prop: widgetProp('color', (w) => w.color),
match: (it) => it.equals(Colors.blue),
);
final button = spot<ElevatedButton>();

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(
Expand Down Expand Up @@ -292,6 +310,34 @@ void actTests() {
});
}

class ColorToggleApp extends StatefulWidget {
const ColorToggleApp({super.key});

@override
State<ColorToggleApp> createState() => _ColorToggleAppState();
}

class _ColorToggleAppState extends State<ColorToggleApp> {
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();
Expand Down

0 comments on commit 83f3b34

Please sign in to comment.