Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(authenticator): date validator #3766

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading