5

I am working on a flutter web project and I have a widget I would like to write tests about. This widget is using a MouseRegion which does some actions when it is hovered or not by the user.

As an example:


class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  bool isHovered = false;
  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      onExit: (_) {
        setState(() {
          isHovered = false;
        });
      },
      onEnter: (_) {
        setState(() {
          isHovered = true;
        });
      },
      child: Container(
        color: isHovered ? Colors.blue : Colors.red,
        height: 50,
        width: 50,
      )
    );
  }
}

I can write a widget test to test my container is red:

testWidgets('MyWidget should be red by default', (WidgetTester tester) async {
  await tester.pumpWidget(
    MyWidget(),
  );
  expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.red), findsOneWidget);
  expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.blue), findsNothing);
});

But how can I simulate the hovering in a test widget (to check that the container is blue)?

3
  • 1
    I believe this comment can give you a direction. stackoverflow.com/a/64450668/10136855 Commented Feb 18, 2021 at 4:19
  • Thanks for the suggestion, I tried to add final offset = tester.getCenter(find.byType(MyWidget)); final testPointer = TestPointer(1, PointerDeviceKind.mouse)..hover(offset); await tester.pumpAndSettle(); with and without await tester.sendEventToBinding(testPointer.scroll(const Offset(0, 0))); and it unfortunately didn't work Commented Feb 18, 2021 at 6:06
  • await tester.sendEventToBinding(testPointer.scroll(const Offset(0, 0))); is for scrolling. I thought the hover function is answer but not. Please try to look around this flutter test to find an answer github.com/flutter/flutter/blob/master/packages/flutter/test/… Commented Feb 18, 2021 at 6:18

1 Answer 1

13

Here is how I managed to do it

testWidgets('MyWidget should be red by default', (WidgetTester tester) async {
  await tester.pumpWidget(
    MyWidget(),
  );
  expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.red), findsOneWidget);
  expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.blue), findsNothing);

  final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
  await gesture.addPointer(location: Offset.zero);
  addTearDown(gesture.removePointer);
  await tester.pump();
  await gesture.moveTo(tester.getCenter(find.byType(MyWidget)));
  await tester.pumpAndSettle();

  expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.red), findsNothing);
  expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.blue), findsOneWidget);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.