Skip to content

Commit

Permalink
Add additional tests for EmailPasswordSignInContents, additional test…
Browse files Browse the repository at this point in the history
…er.pump() in signInWithEmailAndPassword helper
  • Loading branch information
bizz84 committed Aug 25, 2023
1 parent 2e10b05 commit f662187
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ecommerce_app/test/src/features/authentication/auth_robot.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:ecommerce_app/src/common_widgets/alert_dialogs.dart';
import 'package:ecommerce_app/src/common_widgets/custom_text_button.dart';
import 'package:ecommerce_app/src/common_widgets/primary_button.dart';
import 'package:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart';
import 'package:ecommerce_app/src/features/authentication/presentation/account/account_screen.dart';
Expand Down Expand Up @@ -49,6 +50,13 @@ class AuthRobot {
await tester.pumpAndSettle();
}

Future<void> tapFormToggleButton() async {
final toggleButton = find.byType(CustomTextButton);
expect(toggleButton, findsOneWidget);
await tester.tap(toggleButton);
await tester.pumpAndSettle();
}

Future<void> enterEmail(String email) async {
final emailField = find.byKey(EmailPasswordSignInScreen.emailKey);
expect(emailField, findsOneWidget);
Expand All @@ -61,8 +69,19 @@ class AuthRobot {
await tester.enterText(passwordField, password);
}

void expectCreateAccountButtonFound() {
final dialogTitle = find.text('Create an account');
expect(dialogTitle, findsOneWidget);
}

void expectCreateAccountButtonNotFound() {
final dialogTitle = find.text('Create an account');
expect(dialogTitle, findsNothing);
}

Future<void> signInWithEmailAndPassword() async {
await enterEmail('[email protected]');
await tester.pump();
await enterPassword('test1234');
await tapEmailAndPasswordSubmitButton();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,109 @@ void main() {
expect(didSignIn, true);
});
});

group('register', () {
testWidgets('''
Given formType is register
When tap on the sign-in button
Then createUserWithEmailAndPassword is not called
''', (tester) async {
final r = AuthRobot(tester);
await r.pumpEmailPasswordSignInContents(
authRepository: authRepository,
formType: EmailPasswordSignInFormType.register,
);
await r.tapEmailAndPasswordSubmitButton();
verifyNever(() => authRepository.createUserWithEmailAndPassword(
any(),
any(),
));
});
testWidgets('''
Given formType is register
When enter valid email
And enter password that is too short
And tap on the sign-in button
Then createUserWithEmailAndPassword is called
And onSignedIn callback is called
And error alert is not shown
''', (tester) async {
final r = AuthRobot(tester);
when(() => authRepository.createUserWithEmailAndPassword(
testEmail,
testPassword,
)).thenAnswer((_) => Future.value());
await r.pumpEmailPasswordSignInContents(
authRepository: authRepository,
formType: EmailPasswordSignInFormType.register,
);
await r.enterEmail(testEmail);
await r.enterPassword(testPassword);
await r.tapEmailAndPasswordSubmitButton();
verifyNever(() => authRepository.createUserWithEmailAndPassword(
any(),
any(),
));
});
testWidgets('''
Given formType is register
When enter valid email
And enter password that is long enough
And tap on the sign-in button
Then createUserWithEmailAndPassword is called
And onSignedIn callback is called
And error alert is not shown
''', (tester) async {
var didSignIn = false;
final r = AuthRobot(tester);
const password = 'test1234'; // at least 8 characters to pass validation
when(() => authRepository.createUserWithEmailAndPassword(
testEmail,
password,
)).thenAnswer((_) => Future.value());
await r.pumpEmailPasswordSignInContents(
authRepository: authRepository,
formType: EmailPasswordSignInFormType.register,
onSignedIn: () => didSignIn = true,
);
await r.enterEmail(testEmail);
await r.enterPassword(password);
await r.tapEmailAndPasswordSubmitButton();
verify(() => authRepository.createUserWithEmailAndPassword(
testEmail,
password,
)).called(1);
r.expectErrorAlertNotFound();
expect(didSignIn, true);
});
});

group('updateFormType', () {
testWidgets('''
Given formType is sign in
When tap on the form toggle button
Then create account button is found
''', (tester) async {
final r = AuthRobot(tester);
await r.pumpEmailPasswordSignInContents(
authRepository: authRepository,
formType: EmailPasswordSignInFormType.signIn,
);
await r.tapFormToggleButton();
r.expectCreateAccountButtonFound();
});
testWidgets('''
Given formType is sign in
When tap on the form toggle button
Then create account button is found
''', (tester) async {
final r = AuthRobot(tester);
await r.pumpEmailPasswordSignInContents(
authRepository: authRepository,
formType: EmailPasswordSignInFormType.register,
);
await r.tapFormToggleButton();
r.expectCreateAccountButtonNotFound();
});
});
}

0 comments on commit f662187

Please sign in to comment.