diff --git a/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart b/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart index d010547155..8c9d00679c 100644 --- a/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart +++ b/packages/authenticator/amplify_authenticator/lib/src/widgets/form_fields/sign_up_form_field.dart @@ -780,6 +780,9 @@ class _SignUpDateFieldState extends _SignUpFormFieldState @override FormFieldValidator get validator { + if (widget.validatorOverride != null) { + return widget.validatorOverride!; + } return simpleValidator( stringResolver.inputs.resolve( context, 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 72ee1b142e..8a83833686 100644 --- a/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart +++ b/packages/authenticator/amplify_authenticator/test/sign_up_form_test.dart @@ -73,6 +73,53 @@ void main() { }, ); + testWidgets( + 'displays message when submitted with invalid birth date', + (tester) async { + await tester.pumpWidget( + MockAuthenticatorApp( + initialStep: AuthenticatorStep.signUp, + signUpForm: SignUpForm.custom( + fields: [ + SignUpFormField.username(), + SignUpFormField.birthdate( + validator: (value) { + if (value == null || value.isEmpty) return null; + final age = DateTime.now().difference( + DateTime.parse(value), + ); + if (age < const Duration(days: 365 * 18)) { + return 'You must be 18 years or older.'; + } + return null; + }, + ), + SignUpFormField.password(), + SignUpFormField.passwordConfirmation(), + ], + ), + ), + ); + await tester.pumpAndSettle(); + + final signUpPage = SignUpPage(tester: tester); + + await signUpPage.enterUsername('user123'); + await signUpPage.enterBirthDate('01/01/2020'); + await signUpPage.enterPassword('Password123'); + await signUpPage.enterPasswordConfirmation('Password123'); + + await signUpPage.submitSignUp(); + + final usernameFieldError = find.descendant( + of: signUpPage.birthdateField, + matching: find.text('You must be 18 years or older.'), + ); + + expect(usernameFieldError, findsOneWidget); + }, + ); + testWidgets( 'displays message when password does not meet requirements', (tester) async { diff --git a/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart b/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart index 2d69b60a10..651d3452e7 100644 --- a/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart +++ b/packages/authenticator/amplify_authenticator_test/lib/src/mock_authenticator_app.dart @@ -22,6 +22,8 @@ class MockAuthenticatorApp extends StatefulWidget { this.darkTheme, this.initialStep = AuthenticatorStep.signIn, this.authPlugin, + this.signInForm, + this.signUpForm, this.child, }); @@ -30,6 +32,8 @@ class MockAuthenticatorApp extends StatefulWidget { final ThemeData? darkTheme; final AuthenticatorStep initialStep; final AuthPluginInterface? authPlugin; + final SignInForm? signInForm; + final SignUpForm? signUpForm; final Widget? child; @override @@ -102,6 +106,8 @@ class _MockAuthenticatorAppState extends State { return Authenticator( key: authenticatorKey, authBlocOverride: _authBloc, + signInForm: widget.signInForm, + signUpForm: widget.signUpForm, child: widget.child ?? MaterialApp( debugShowCheckedModeBanner: false, diff --git a/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart b/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart index a8ca710810..83ccdd3121 100644 --- a/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart +++ b/packages/authenticator/amplify_authenticator_test/lib/src/pages/sign_up_page.dart @@ -21,6 +21,15 @@ class SignUpPage extends AuthenticatorPage { Finder get phoneField => find.byKey(keyPhoneNumberSignUpFormField); Finder get preferredUsernameField => find.byKey(keyPreferredUsernameSignUpFormField); + Finder get birthdateField => find.byKey(keyBirthdateSignUpFormField); + Finder get datePickerTextField => find.descendant( + of: find.byType(InputDatePickerFormField), + matching: find.byType(TextFormField), + ); + Finder get datePickerOkayButton => find.descendant( + of: find.byType(DatePickerDialog), + matching: find.byType(TextButton).at(1), + ); Finder get signUpButton => find.byKey(keySignUpButton); Finder get selectEmailButton => find.byKey(keyEmailUsernameToggleButton); @@ -67,6 +76,16 @@ class SignUpPage extends AuthenticatorPage { await tester.enterText(preferredUsernameField, username); } + /// When I type a new "birth date" + Future enterBirthDate(String value) async { + await tester.ensureVisible(birthdateField); + await tester.tap(birthdateField); + await tester.pumpAndSettle(); + await tester.enterText(datePickerTextField, value); + await tester.tap(datePickerOkayButton); + await tester.pumpAndSettle(); + } + /// When I click the "Create Account" button Future submitSignUp() async { await tester.ensureVisible(signUpButton);