From c616675aa676f2e9c1eddf7391d7cc9b6e5753ad Mon Sep 17 00:00:00 2001 From: Ekjot <43255916+ekjotmultani@users.noreply.github.com> Date: Sat, 4 Jan 2025 10:17:17 -0800 Subject: [PATCH] rewrote functions for better code standards, and rewrote duplicated test --- .../src/mixins/authenticator_phone_field.dart | 13 +++++---- .../mixins/authenticator_username_field.dart | 13 +++++---- .../test/sign_up_form_test.dart | 28 +++++++++---------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_phone_field.dart b/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_phone_field.dart index bc761806da..44f0f66800 100644 --- a/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_phone_field.dart +++ b/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_phone_field.dart @@ -56,13 +56,14 @@ mixin AuthenticatorPhoneFieldMixin 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 { diff --git a/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart b/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart index 04a0de6ff2..4cb636526a 100644 --- a/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart +++ b/packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_username_field.dart @@ -173,13 +173,14 @@ mixin AuthenticatorUsernameField 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 diff --git a/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart b/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart index bdff62ec3f..8647bb1b08 100644 --- a/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart +++ b/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart @@ -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'; @@ -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( @@ -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(