Skip to content

Commit

Permalink
test(#58): molhora casos de teste da LoginView
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Dec 24, 2024
1 parent c99ebbb commit a203335
Showing 1 changed file with 67 additions and 18 deletions.
85 changes: 67 additions & 18 deletions test/ui/login/view/login_view_test.dart
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';
Expand All @@ -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));

Expand All @@ -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);
Expand All @@ -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 {
});
}

0 comments on commit a203335

Please sign in to comment.