From f6621874da63b0682cff80162a3e21226a0b0f8e Mon Sep 17 00:00:00 2001 From: Andrea Bizzotto Date: Tue, 7 Jun 2022 09:29:34 +0100 Subject: [PATCH] Add additional tests for EmailPasswordSignInContents, additional tester.pump() in signInWithEmailAndPassword helper --- .../features/authentication/auth_robot.dart | 19 ++++ .../email_password_sign_in_screen_test.dart | 105 ++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/ecommerce_app/test/src/features/authentication/auth_robot.dart b/ecommerce_app/test/src/features/authentication/auth_robot.dart index 91ed772a..beee8b6d 100644 --- a/ecommerce_app/test/src/features/authentication/auth_robot.dart +++ b/ecommerce_app/test/src/features/authentication/auth_robot.dart @@ -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'; @@ -49,6 +50,13 @@ class AuthRobot { await tester.pumpAndSettle(); } + Future tapFormToggleButton() async { + final toggleButton = find.byType(CustomTextButton); + expect(toggleButton, findsOneWidget); + await tester.tap(toggleButton); + await tester.pumpAndSettle(); + } + Future enterEmail(String email) async { final emailField = find.byKey(EmailPasswordSignInScreen.emailKey); expect(emailField, findsOneWidget); @@ -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 signInWithEmailAndPassword() async { await enterEmail('test@test.com'); + await tester.pump(); await enterPassword('test1234'); await tapEmailAndPasswordSubmitButton(); } diff --git a/ecommerce_app/test/src/features/authentication/presentation/sign_in/email_password_sign_in_screen_test.dart b/ecommerce_app/test/src/features/authentication/presentation/sign_in/email_password_sign_in_screen_test.dart index 098cd593..0dd27a1d 100644 --- a/ecommerce_app/test/src/features/authentication/presentation/sign_in/email_password_sign_in_screen_test.dart +++ b/ecommerce_app/test/src/features/authentication/presentation/sign_in/email_password_sign_in_screen_test.dart @@ -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(); + }); + }); }