Skip to content

Commit

Permalink
test(#58): teste da view
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Dec 23, 2024
1 parent af065a5 commit 2f00182
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/ui/login/view/login_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class _LoginScreenState extends State<LoginScreen> {
}

Widget _authDevice(LoginViewModel viewModel) {

Log.d("Mostrando tela de autorização do dispositivo");

return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expand All @@ -92,7 +95,7 @@ class _LoginScreenState extends State<LoginScreen> {
child: ElevatedButton(
onPressed: () async {
viewModel.loginWithDeviceAuth().then((ok) {
viewModel.goNextPage();
viewModel.goToHome();
});
},
child: const Text('Usar senha do celular'),
Expand Down Expand Up @@ -185,7 +188,7 @@ class _LoginScreenState extends State<LoginScreen> {
onPressed: () {
viewModel.loginWithEmailAndPassword().then((_) {

viewModel.goNextPage();
viewModel.goToHome();

}).catchError((e) => showDialog<Object>(
context: context,
Expand Down
19 changes: 13 additions & 6 deletions lib/ui/login/viewModel/login_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ class LoginViewModel extends ChangeNotifier {
.authenticate(localizedReason: 'Toque com o dedo no sensor para logar');
}

void goNextPage() {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const NavbarView(),
),
);
void goToHome() {
try {
if (context.mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const NavbarView(),
),
);
}
} catch (e) {
Log.e(e);
rethrow;
}
}
}
64 changes: 64 additions & 0 deletions test/ui/login/view/login_view_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:aranduapp/ui/login/view/login_view.dart';
import 'package:aranduapp/ui/login/viewModel/login_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';

@GenerateNiceMocks([MockSpec<LoginViewModel>()])
import 'login_view_test.mocks.dart';

void main() {
late MockLoginViewModel mockViewModel;

setUp(() {
mockViewModel = MockLoginViewModel();
when(mockViewModel.formKey).thenReturn(GlobalKey<FormState>());
when(mockViewModel.emailController).thenReturn(TextEditingController());
when(mockViewModel.isLoading).thenReturn(false);
});

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.pump(const Duration(milliseconds: 500));

expect(find.byType(CircularProgressIndicator), findsOneWidget);

await tester.pumpAndSettle();

expect(find.byType(CircularProgressIndicator), findsNothing);
});

testWidgets('login with auth device', (WidgetTester tester) async {
when(mockViewModel.loginWithDeviceAuth()).thenAnswer((_) async => true);

await tester.pumpWidget(
ChangeNotifierProvider<LoginViewModel>.value(
value: mockViewModel,
child: const MaterialApp(
home: LoginScreen(),
),
),
);

await tester.pumpAndSettle();

verify(mockViewModel.loginWithDeviceAuth()).called(1);

await tester.pumpAndSettle();
verify(mockViewModel.goToHome()).called(1);
});
}
210 changes: 210 additions & 0 deletions test/ui/login/view/login_view_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Mocks generated by Mockito 5.4.4 from annotations
// in aranduapp/test/ui/login/view/login_view_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4;

import 'package:aranduapp/ui/login/viewModel/login_view_model.dart' as _i3;
import 'package:flutter/material.dart' as _i2;
import 'package:mockito/mockito.dart' as _i1;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: deprecated_member_use
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

class _FakeBuildContext_0 extends _i1.SmartFake implements _i2.BuildContext {
_FakeBuildContext_0(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

class _FakeGlobalKey_1<T extends _i2.State<_i2.StatefulWidget>>
extends _i1.SmartFake implements _i2.GlobalKey<T> {
_FakeGlobalKey_1(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

class _FakeTextEditingController_2 extends _i1.SmartFake
implements _i2.TextEditingController {
_FakeTextEditingController_2(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

/// A class which mocks [LoginViewModel].
///
/// See the documentation for Mockito's code generation for more information.
class MockLoginViewModel extends _i1.Mock implements _i3.LoginViewModel {
@override
_i2.BuildContext get context => (super.noSuchMethod(
Invocation.getter(#context),
returnValue: _FakeBuildContext_0(
this,
Invocation.getter(#context),
),
returnValueForMissingStub: _FakeBuildContext_0(
this,
Invocation.getter(#context),
),
) as _i2.BuildContext);

@override
bool get isLoading => (super.noSuchMethod(
Invocation.getter(#isLoading),
returnValue: false,
returnValueForMissingStub: false,
) as bool);

@override
set isLoading(bool? _isLoading) => super.noSuchMethod(
Invocation.setter(
#isLoading,
_isLoading,
),
returnValueForMissingStub: null,
);

@override
_i2.GlobalKey<_i2.FormState> get formKey => (super.noSuchMethod(
Invocation.getter(#formKey),
returnValue: _FakeGlobalKey_1<_i2.FormState>(
this,
Invocation.getter(#formKey),
),
returnValueForMissingStub: _FakeGlobalKey_1<_i2.FormState>(
this,
Invocation.getter(#formKey),
),
) as _i2.GlobalKey<_i2.FormState>);

@override
_i2.TextEditingController get emailController => (super.noSuchMethod(
Invocation.getter(#emailController),
returnValue: _FakeTextEditingController_2(
this,
Invocation.getter(#emailController),
),
returnValueForMissingStub: _FakeTextEditingController_2(
this,
Invocation.getter(#emailController),
),
) as _i2.TextEditingController);

@override
_i2.TextEditingController get passwordController => (super.noSuchMethod(
Invocation.getter(#passwordController),
returnValue: _FakeTextEditingController_2(
this,
Invocation.getter(#passwordController),
),
returnValueForMissingStub: _FakeTextEditingController_2(
this,
Invocation.getter(#passwordController),
),
) as _i2.TextEditingController);

@override
bool get hasListeners => (super.noSuchMethod(
Invocation.getter(#hasListeners),
returnValue: false,
returnValueForMissingStub: false,
) as bool);

@override
_i4.Future<void> loginWithEmailAndPassword() => (super.noSuchMethod(
Invocation.method(
#loginWithEmailAndPassword,
[],
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);

@override
_i4.Future<void> validateToken() => (super.noSuchMethod(
Invocation.method(
#validateToken,
[],
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);

@override
_i4.Future<bool> loginWithDeviceAuth() => (super.noSuchMethod(
Invocation.method(
#loginWithDeviceAuth,
[],
),
returnValue: _i4.Future<bool>.value(false),
returnValueForMissingStub: _i4.Future<bool>.value(false),
) as _i4.Future<bool>);

@override
void goToHome() => super.noSuchMethod(
Invocation.method(
#goToHome,
[],
),
returnValueForMissingStub: null,
);

@override
void addListener(dynamic listener) => super.noSuchMethod(
Invocation.method(
#addListener,
[listener],
),
returnValueForMissingStub: null,
);

@override
void removeListener(dynamic listener) => super.noSuchMethod(
Invocation.method(
#removeListener,
[listener],
),
returnValueForMissingStub: null,
);

@override
void dispose() => super.noSuchMethod(
Invocation.method(
#dispose,
[],
),
returnValueForMissingStub: null,
);

@override
void notifyListeners() => super.noSuchMethod(
Invocation.method(
#notifyListeners,
[],
),
returnValueForMissingStub: null,
);
}

0 comments on commit 2f00182

Please sign in to comment.