Skip to content

Commit

Permalink
[Murat] Increase CountryPickerButton test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Murat Gun committed Nov 22, 2023
1 parent 98de16c commit 695e3a4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ component-test:
unit-test:
flutter test test/unit/*_test.dart

test: flutter test --coverage
lint:
flutter analyze

tests:
flutter test
65 changes: 65 additions & 0 deletions test/component/country_picker_button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
13 changes: 6 additions & 7 deletions test/component/mocks/country_picker_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Country> countries = Country.values;
final String title = "Select Country";
final bool isSearchable = true;
Expand All @@ -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
Expand All @@ -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,
),
),
),
Expand Down

0 comments on commit 695e3a4

Please sign in to comment.