-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(#58): molhora casos de teste da LoginView
- Loading branch information
1 parent
c99ebbb
commit a203335
Showing
1 changed file
with
67 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import 'package:aranduapp/ui/login/view/login_view.dart'; | ||
import 'package:aranduapp/ui/login/viewModel/login_view_model.dart'; | ||
import 'package:aranduapp/ui/shared/TextEmail.dart'; | ||
import 'package:aranduapp/ui/shared/TextPassword.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/annotations.dart'; | ||
|
@@ -16,21 +18,24 @@ void main() { | |
mockViewModel = MockLoginViewModel(); | ||
when(mockViewModel.formKey).thenReturn(GlobalKey<FormState>()); | ||
when(mockViewModel.emailController).thenReturn(TextEditingController()); | ||
when(mockViewModel.passwordController).thenReturn(TextEditingController()); | ||
}); | ||
|
||
Widget createLoginScreen(LoginViewModel mockViewModel) { | ||
return ChangeNotifierProvider<LoginViewModel>.value( | ||
value: mockViewModel, | ||
child: const MaterialApp( | ||
home: LoginScreen(), | ||
), | ||
); | ||
} | ||
|
||
testWidgets('Displays loading screen while waiting for Future', | ||
(WidgetTester tester) async { | ||
when(mockViewModel.validateToken()).thenAnswer( | ||
(_) async => await Future.delayed(const Duration(seconds: 1))); | ||
|
||
await tester.pumpWidget( | ||
ChangeNotifierProvider<LoginViewModel>.value( | ||
value: mockViewModel, | ||
child: const MaterialApp( | ||
home: LoginScreen(), | ||
), | ||
), | ||
); | ||
await tester.pumpWidget(createLoginScreen(mockViewModel)); | ||
|
||
await tester.pump(const Duration(milliseconds: 500)); | ||
|
||
|
@@ -41,19 +46,10 @@ void main() { | |
expect(find.byType(CircularProgressIndicator), findsNothing); | ||
}); | ||
|
||
|
||
testWidgets('login with auth device', (WidgetTester tester) async { | ||
when(mockViewModel.loginWithDeviceAuth()).thenAnswer((_) async => false); | ||
|
||
await tester.pumpWidget( | ||
ChangeNotifierProvider<LoginViewModel>.value( | ||
value: mockViewModel, | ||
child: const MaterialApp( | ||
home: LoginScreen(), | ||
), | ||
), | ||
); | ||
|
||
await tester.pumpWidget(createLoginScreen(mockViewModel)); | ||
await tester.pumpAndSettle(); | ||
|
||
when(mockViewModel.loginWithDeviceAuth()).thenAnswer((_) async => true); | ||
|
@@ -66,4 +62,57 @@ void main() { | |
verify(mockViewModel.goToHome()).called(1); | ||
verify(mockViewModel.loginWithDeviceAuth()).called(2); | ||
}); | ||
|
||
testWidgets( | ||
'Login screen displays email and password fields and login button', | ||
(WidgetTester tester) async { | ||
when(mockViewModel.validateToken()) | ||
.thenAnswer((_) async => throw Exception('Token validation failed')); | ||
|
||
await tester.pumpWidget(createLoginScreen(mockViewModel)); | ||
await tester.pumpAndSettle(); | ||
|
||
expect(find.byType(TextEmail), findsOneWidget); | ||
expect(find.byType(TextPassWord), findsOneWidget); | ||
expect(find.text('Entrar'), findsOneWidget); | ||
}); | ||
|
||
testWidgets('Test User Input for Email and Password', | ||
(WidgetTester tester) async { | ||
when(mockViewModel.validateToken()) | ||
.thenAnswer((_) async => throw Exception('Token validation failed')); | ||
|
||
await tester.pumpWidget(createLoginScreen(mockViewModel)); | ||
await tester.pumpAndSettle(); | ||
|
||
const email = '[email protected]'; | ||
const password = 'password123'; | ||
|
||
await tester.enterText(find.byType(TextEmail), email); | ||
await tester.enterText(find.byType(TextPassWord), password); | ||
|
||
expect(mockViewModel.emailController.text, email); | ||
expect(mockViewModel.passwordController.text, password); | ||
}); | ||
|
||
testWidgets('Login is successful', | ||
(WidgetTester tester) async { | ||
when(mockViewModel.validateToken()) | ||
.thenAnswer((_) async => throw Exception('Token validation failed')); | ||
|
||
await tester.pumpWidget(createLoginScreen(mockViewModel)); | ||
await tester.pumpAndSettle(); | ||
|
||
await tester.tap(find.text('Entrar')); | ||
|
||
when(mockViewModel.loginWithEmailAndPassword()).thenAnswer( | ||
(_) async => await Future.delayed(const Duration(seconds: 1))); | ||
|
||
await tester.pumpAndSettle(); | ||
verify(mockViewModel.loginWithEmailAndPassword()).called(1); | ||
verify(mockViewModel.goToHome()).called(1); | ||
}); | ||
|
||
testWidgets('Displays error when login fails', (WidgetTester tester) async { | ||
}); | ||
} |