Skip to content

Commit f662187

Browse files
committed
Add additional tests for EmailPasswordSignInContents, additional tester.pump() in signInWithEmailAndPassword helper
1 parent 2e10b05 commit f662187

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

ecommerce_app/test/src/features/authentication/auth_robot.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:ecommerce_app/src/common_widgets/alert_dialogs.dart';
2+
import 'package:ecommerce_app/src/common_widgets/custom_text_button.dart';
23
import 'package:ecommerce_app/src/common_widgets/primary_button.dart';
34
import 'package:ecommerce_app/src/features/authentication/data/fake_auth_repository.dart';
45
import 'package:ecommerce_app/src/features/authentication/presentation/account/account_screen.dart';
@@ -49,6 +50,13 @@ class AuthRobot {
4950
await tester.pumpAndSettle();
5051
}
5152

53+
Future<void> tapFormToggleButton() async {
54+
final toggleButton = find.byType(CustomTextButton);
55+
expect(toggleButton, findsOneWidget);
56+
await tester.tap(toggleButton);
57+
await tester.pumpAndSettle();
58+
}
59+
5260
Future<void> enterEmail(String email) async {
5361
final emailField = find.byKey(EmailPasswordSignInScreen.emailKey);
5462
expect(emailField, findsOneWidget);
@@ -61,8 +69,19 @@ class AuthRobot {
6169
await tester.enterText(passwordField, password);
6270
}
6371

72+
void expectCreateAccountButtonFound() {
73+
final dialogTitle = find.text('Create an account');
74+
expect(dialogTitle, findsOneWidget);
75+
}
76+
77+
void expectCreateAccountButtonNotFound() {
78+
final dialogTitle = find.text('Create an account');
79+
expect(dialogTitle, findsNothing);
80+
}
81+
6482
Future<void> signInWithEmailAndPassword() async {
6583
await enterEmail('[email protected]');
84+
await tester.pump();
6685
await enterPassword('test1234');
6786
await tapEmailAndPasswordSubmitButton();
6887
}

ecommerce_app/test/src/features/authentication/presentation/sign_in/email_password_sign_in_screen_test.dart

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,109 @@ void main() {
5959
expect(didSignIn, true);
6060
});
6161
});
62+
63+
group('register', () {
64+
testWidgets('''
65+
Given formType is register
66+
When tap on the sign-in button
67+
Then createUserWithEmailAndPassword is not called
68+
''', (tester) async {
69+
final r = AuthRobot(tester);
70+
await r.pumpEmailPasswordSignInContents(
71+
authRepository: authRepository,
72+
formType: EmailPasswordSignInFormType.register,
73+
);
74+
await r.tapEmailAndPasswordSubmitButton();
75+
verifyNever(() => authRepository.createUserWithEmailAndPassword(
76+
any(),
77+
any(),
78+
));
79+
});
80+
testWidgets('''
81+
Given formType is register
82+
When enter valid email
83+
And enter password that is too short
84+
And tap on the sign-in button
85+
Then createUserWithEmailAndPassword is called
86+
And onSignedIn callback is called
87+
And error alert is not shown
88+
''', (tester) async {
89+
final r = AuthRobot(tester);
90+
when(() => authRepository.createUserWithEmailAndPassword(
91+
testEmail,
92+
testPassword,
93+
)).thenAnswer((_) => Future.value());
94+
await r.pumpEmailPasswordSignInContents(
95+
authRepository: authRepository,
96+
formType: EmailPasswordSignInFormType.register,
97+
);
98+
await r.enterEmail(testEmail);
99+
await r.enterPassword(testPassword);
100+
await r.tapEmailAndPasswordSubmitButton();
101+
verifyNever(() => authRepository.createUserWithEmailAndPassword(
102+
any(),
103+
any(),
104+
));
105+
});
106+
testWidgets('''
107+
Given formType is register
108+
When enter valid email
109+
And enter password that is long enough
110+
And tap on the sign-in button
111+
Then createUserWithEmailAndPassword is called
112+
And onSignedIn callback is called
113+
And error alert is not shown
114+
''', (tester) async {
115+
var didSignIn = false;
116+
final r = AuthRobot(tester);
117+
const password = 'test1234'; // at least 8 characters to pass validation
118+
when(() => authRepository.createUserWithEmailAndPassword(
119+
testEmail,
120+
password,
121+
)).thenAnswer((_) => Future.value());
122+
await r.pumpEmailPasswordSignInContents(
123+
authRepository: authRepository,
124+
formType: EmailPasswordSignInFormType.register,
125+
onSignedIn: () => didSignIn = true,
126+
);
127+
await r.enterEmail(testEmail);
128+
await r.enterPassword(password);
129+
await r.tapEmailAndPasswordSubmitButton();
130+
verify(() => authRepository.createUserWithEmailAndPassword(
131+
testEmail,
132+
password,
133+
)).called(1);
134+
r.expectErrorAlertNotFound();
135+
expect(didSignIn, true);
136+
});
137+
});
138+
139+
group('updateFormType', () {
140+
testWidgets('''
141+
Given formType is sign in
142+
When tap on the form toggle button
143+
Then create account button is found
144+
''', (tester) async {
145+
final r = AuthRobot(tester);
146+
await r.pumpEmailPasswordSignInContents(
147+
authRepository: authRepository,
148+
formType: EmailPasswordSignInFormType.signIn,
149+
);
150+
await r.tapFormToggleButton();
151+
r.expectCreateAccountButtonFound();
152+
});
153+
testWidgets('''
154+
Given formType is sign in
155+
When tap on the form toggle button
156+
Then create account button is found
157+
''', (tester) async {
158+
final r = AuthRobot(tester);
159+
await r.pumpEmailPasswordSignInContents(
160+
authRepository: authRepository,
161+
formType: EmailPasswordSignInFormType.register,
162+
);
163+
await r.tapFormToggleButton();
164+
r.expectCreateAccountButtonNotFound();
165+
});
166+
});
62167
}

0 commit comments

Comments
 (0)