From 4f4f1cdd87ba05ec6b647842cfedf609c749f021 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Fri, 6 May 2022 19:08:52 -0300 Subject: [PATCH] adding tests for pinball dpad button --- .../src/widgets/pinball_dpad_button_test.dart | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 packages/pinball_ui/test/src/widgets/pinball_dpad_button_test.dart diff --git a/packages/pinball_ui/test/src/widgets/pinball_dpad_button_test.dart b/packages/pinball_ui/test/src/widgets/pinball_dpad_button_test.dart new file mode 100644 index 00000000..56e72203 --- /dev/null +++ b/packages/pinball_ui/test/src/widgets/pinball_dpad_button_test.dart @@ -0,0 +1,120 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mocktail/mocktail.dart'; +import 'package:pinball_ui/gen/gen.dart'; +import 'package:pinball_ui/pinball_ui.dart'; + +extension _WidgetTesterX on WidgetTester { + Future pumpButton({ + required PinballDpadDirection direction, + required VoidCallback onTap, + }) async { + await pumpWidget( + MaterialApp( + home: Scaffold( + body: PinballDpadButton( + direction: direction, + onTap: onTap, + ), + ), + ), + ); + } +} + +extension _CommonFindersX on CommonFinders { + Finder byImagePath(String path) { + return find.byWidgetPredicate( + (widget) { + if (widget is Image) { + final image = widget.image; + + if (image is AssetImage) { + return image.keyName == path; + } + return false; + } + + return false; + }, + ); + } +} + +abstract class _VoidCallbackStubBase { + void onCall(); +} + +class _VoidCallbackStub extends Mock implements _VoidCallbackStubBase {} + +void main() { + group('PinballDpadButton', () { + testWidgets('can be tapped', (tester) async { + final stub = _VoidCallbackStub(); + await tester.pumpButton( + direction: PinballDpadDirection.up, + onTap: stub.onCall, + ); + + await tester.tap(find.byType(Image)); + + verify(stub.onCall).called(1); + }); + + group('when it is up', () { + testWidgets('renders the correct image', (tester) async { + await tester.pumpButton( + direction: PinballDpadDirection.up, + onTap: () {}, + ); + + expect( + find.byImagePath(Assets.images.button.dpadUp.keyName), + findsOneWidget, + ); + }); + }); + + group('when it is down', () { + testWidgets('renders the correct image', (tester) async { + await tester.pumpButton( + direction: PinballDpadDirection.down, + onTap: () {}, + ); + + expect( + find.byImagePath(Assets.images.button.dpadDown.keyName), + findsOneWidget, + ); + }); + }); + + group('when it is left', () { + testWidgets('renders the correct image', (tester) async { + await tester.pumpButton( + direction: PinballDpadDirection.left, + onTap: () {}, + ); + + expect( + find.byImagePath(Assets.images.button.dpadLeft.keyName), + findsOneWidget, + ); + }); + }); + + group('when it is right', () { + testWidgets('renders the correct image', (tester) async { + await tester.pumpButton( + direction: PinballDpadDirection.right, + onTap: () {}, + ); + + expect( + find.byImagePath(Assets.images.button.dpadRight.keyName), + findsOneWidget, + ); + }); + }); + }); +}