diff --git a/Makefile b/Makefile index 3305e52..65c5aea 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,8 @@ component-test: unit-test: flutter test test/unit/*_test.dart -test: flutter test --coverage \ No newline at end of file +lint: + flutter analyze + +tests: + flutter test \ No newline at end of file diff --git a/test/component/country_picker_button_test.dart b/test/component/country_picker_button_test.dart index 6817d39..a9221bf 100644 --- a/test/component/country_picker_button_test.dart +++ b/test/component/country_picker_button_test.dart @@ -4,10 +4,15 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'mocks/country_picker_button.dart'; +import 'utils/caller_checker.dart'; void main() { CountryPickerButtonMock mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.bottomSheet, pickerHeight: CountryPickerHeigth.h25); + + setUp(() { + EphoneFieldCallerChecker.reset(); + }); testWidgets('should CountryPickerButton widget renders successfully', (widgetTester) async { await widgetTester.pumpWidget(mockWidget); expect(find.byType(CountryPickerButton), findsOneWidget); @@ -72,7 +77,67 @@ void main() { expect(find.byType(Dialog), findsOneWidget); }); + testWidgets('should CountryPickerButton widget onValuePicked triggers after selecting country on dialog', + (widgetTester) async { + mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.dialog, pickerHeight: CountryPickerHeigth.h25); + await widgetTester.pumpWidget(mockWidget); + + await widgetTester.tap(find.byType(CountryPickerButton)); + await widgetTester.pumpAndSettle(); + + expect(find.byType(CountryPickerMenu), findsOneWidget); + expect(find.byType(Dialog), findsOneWidget); + + await widgetTester.tap(find.byType(CountryCard).first); + await widgetTester.pumpAndSettle(); + + expect(find.byType(CountryPickerMenu), findsNothing); + expect(find.byType(Dialog), findsNothing); + + expect(EphoneFieldCallerChecker.isOnValuePickedCalled, true); + }); + + testWidgets('should CountryPickerButton widget onValuePicked triggers after selecting country on bottomsheet', + (widgetTester) async { + mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.bottomSheet, pickerHeight: CountryPickerHeigth.h25); + await widgetTester.pumpWidget(mockWidget); + + await widgetTester.tap(find.byType(CountryPickerButton)); + await widgetTester.pumpAndSettle(); + + expect(find.byType(CountryPickerMenu), findsOneWidget); + expect(find.byType(BottomSheet), findsOneWidget); + + await widgetTester.tap(find.byType(CountryCard).first); + await widgetTester.pumpAndSettle(); + + expect(find.byType(CountryPickerMenu), findsNothing); + expect(find.byType(BottomSheet), findsNothing); + + expect(EphoneFieldCallerChecker.isOnValuePickedCalled, true); + }); + + testWidgets('should CountryPickerButton widget onValuePicked triggers after selecting country on page', + (widgetTester) async { + mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.page, pickerHeight: CountryPickerHeigth.h25); + await widgetTester.pumpWidget(mockWidget); + + await widgetTester.tap(find.byType(CountryPickerButton)); + await widgetTester.pumpAndSettle(); + + expect(find.byType(CountryPickerMenu), findsOneWidget); + expect(find.byType(Scaffold), findsOneWidget); + + await widgetTester.tap(find.byType(CountryCard).first); + await widgetTester.pumpAndSettle(); + + expect(find.byType(CountryPickerMenu), findsNothing); + + expect(EphoneFieldCallerChecker.isOnValuePickedCalled, true); + }); + testWidgets('should country picker size as expected if pickerHeight CountryPickerHeigth.h25', (widgetTester) async { + mockWidget = CountryPickerButtonMock(menuType: PickerMenuType.bottomSheet, pickerHeight: CountryPickerHeigth.h25); await widgetTester.pumpWidget(mockWidget); await widgetTester.tap(find.byType(CountryPickerButton)); diff --git a/test/component/mocks/country_picker_button.dart b/test/component/mocks/country_picker_button.dart index 2dcb502..16b49a7 100644 --- a/test/component/mocks/country_picker_button.dart +++ b/test/component/mocks/country_picker_button.dart @@ -5,11 +5,11 @@ import 'package:ephone_field/src/enums/country_picker_menu.dart'; import 'package:ephone_field/src/enums/ephone_textfield_type.dart'; import 'package:flutter/material.dart'; +import '../utils/caller_checker.dart'; + // ignore: must_be_immutable class CountryPickerButtonMock extends StatelessWidget { - CountryPickerButtonMock( - {Key? key, required this.menuType, required this.pickerHeight, this.ctx}) - : super(key: key); + CountryPickerButtonMock({Key? key, required this.menuType, required this.pickerHeight, this.ctx}) : super(key: key); final List countries = Country.values; final String title = "Select Country"; final bool isSearchable = true; @@ -23,6 +23,7 @@ class CountryPickerButtonMock extends StatelessWidget { final EphoneFieldType initialType = EphoneFieldType.phone; final PickerMenuType menuType; final CountryPickerHeigth pickerHeight; + final void Function(Country) onValuePicked = EphoneFieldCallerChecker.mockOnValuePicked; BuildContext? ctx; @override @@ -39,12 +40,10 @@ class CountryPickerButtonMock extends StatelessWidget { title: title, isSearchable: isSearchable, titlePadding: titlePadding, - onValuePicked: (Country c) {}, + onValuePicked: onValuePicked, initialValue: initialValue, menuType: menuType, - pickerHeight: menuType == PickerMenuType.page - ? CountryPickerHeigth.h100 - : pickerHeight, + pickerHeight: menuType == PickerMenuType.page ? CountryPickerHeigth.h100 : pickerHeight, ), ), ),