Skip to content

Commit

Permalink
fix(authenticator): date validator (#3766)
Browse files Browse the repository at this point in the history
* fix: authenticator date validator

* chore: formatting

* chore: fix comment
  • Loading branch information
Jordan-Nelson authored Sep 19, 2023
1 parent 0c8214a commit 247962a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,9 @@ class _SignUpDateFieldState extends _SignUpFormFieldState<String>

@override
FormFieldValidator<String> get validator {
if (widget.validatorOverride != null) {
return widget.validatorOverride!;
}
return simpleValidator(
stringResolver.inputs.resolve(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class MockAuthenticatorApp extends StatefulWidget {
this.darkTheme,
this.initialStep = AuthenticatorStep.signIn,
this.authPlugin,
this.signInForm,
this.signUpForm,
this.child,
});

Expand All @@ -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
Expand Down Expand Up @@ -102,6 +106,8 @@ class _MockAuthenticatorAppState extends State<MockAuthenticatorApp> {
return Authenticator(
key: authenticatorKey,
authBlocOverride: _authBloc,
signInForm: widget.signInForm,
signUpForm: widget.signUpForm,
child: widget.child ??
MaterialApp(
debugShowCheckedModeBanner: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -67,6 +76,16 @@ class SignUpPage extends AuthenticatorPage {
await tester.enterText(preferredUsernameField, username);
}

/// When I type a new "birth date"
Future<void> 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<void> submitSignUp() async {
await tester.ensureVisible(signUpButton);
Expand Down

0 comments on commit 247962a

Please sign in to comment.