Skip to content

Commit

Permalink
rewrote functions for better code standards, and rewrote duplicated test
Browse files Browse the repository at this point in the history
  • Loading branch information
ekjotmultani committed Jan 4, 2025
1 parent 9d9498a commit c616675
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ mixin AuthenticatorPhoneFieldMixin<FieldType extends Enum,
}
// this is to handle the case where the user may errantly input their dial code again in their phone number
// we make sure the user's phone number doesn't naturally just start with their dial code by checking if the number exceeds the maximum phone length of the country's phone number scheme before truncating it
if (phoneNumber.startsWith(prefix.substring(1))) {
if (countryPhoneNumberLengths.containsKey(prefix) &&
phoneNumber.length > countryPhoneNumberLengths[prefix]!) {
phoneNumber = phoneNumber.substring(prefix.length - 1);
}
if (!phoneNumber.startsWith(prefix.substring(1))) {
return phoneNumber;
}
return phoneNumber;
final prefixLength = countryPhoneNumberLengths[prefix];
if (prefixLength == null || phoneNumber.length <= prefixLength) {
return phoneNumber;
}
return phoneNumber.substring(prefix.length - 1);
}

String get dialCode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,14 @@ mixin AuthenticatorUsernameField<FieldType extends Enum,
}
// this is to handle the case where the user may errantly input their dial code again in their phone number
// we make sure the user's phone number doesn't naturally just start with their dial code by checking if the number exceeds the maximum phone length of the country's phone number scheme before truncating it
if (phoneNumber.startsWith(prefix.substring(1))) {
if (countryPhoneNumberLengths.containsKey(prefix) &&
phoneNumber.length > countryPhoneNumberLengths[prefix]!) {
phoneNumber = phoneNumber.substring(prefix.length - 1);
}
if (!phoneNumber.startsWith(prefix.substring(1))) {
return phoneNumber;
}
return phoneNumber;
final prefixLength = countryPhoneNumberLengths[prefix];
if (prefixLength == null || phoneNumber.length <= prefixLength) {
return phoneNumber;
}
return phoneNumber.substring(prefix.length - 1);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:amplify_authenticator/amplify_authenticator.dart';
import 'package:amplify_authenticator/src/services/amplify_auth_service.dart';
import 'package:amplify_authenticator_test/amplify_authenticator_test.dart';
import 'package:amplify_integration_test/amplify_integration_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

Expand Down Expand Up @@ -176,7 +177,7 @@ void main() {
},
);
testWidgets(
'displays message when submitted with empty phone number if the field is required',
'Truncates the prefix with a phone number that accidentally resubmits the dial code already selected in the dropdown',
(tester) async {
await tester.pumpWidget(
MockAuthenticatorApp(
Expand All @@ -194,22 +195,21 @@ void main() {

final signUpPage = SignUpPage(tester: tester);

await signUpPage.submitSignUp();
await signUpPage.enterPhoneNumber('12235556789');

await signUpPage.submitSignUp();
await tester.pumpAndSettle();

Finder findPhoneFieldError() => find.descendant(
final phoneNumber = find
.descendant(
of: signUpPage.phoneField,
matching: find.text('Phone Number field must not be blank.'),
);

expect(findPhoneFieldError(), findsOneWidget);

await signUpPage.enterPhoneNumber('1235556789');

await signUpPage.submitSignUp();

expect(findPhoneFieldError(), findsNothing);
matching: find.byType(Text),
)
.evaluate()
.map((e) => (e.widget as Text).data)
.where((text) => text != null)
.first;

expect(phoneNumber, equals('2235556789'));
},
);
testWidgets(
Expand Down

0 comments on commit c616675

Please sign in to comment.