From 8801575ac9f3f6325e47e175b86a6e1a2d05b1be Mon Sep 17 00:00:00 2001 From: Gabriel Costa de Oliveira Date: Fri, 10 Jan 2025 00:08:37 -0300 Subject: [PATCH 1/2] ref(#59): adiciona request button --- .../view/register_account_view.dart | 55 ++++++------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/lib/ui/register_account/view/register_account_view.dart b/lib/ui/register_account/view/register_account_view.dart index ed81652..9f80479 100644 --- a/lib/ui/register_account/view/register_account_view.dart +++ b/lib/ui/register_account/view/register_account_view.dart @@ -2,6 +2,7 @@ import 'package:aranduapp/ui/login/view/login_view.dart'; import 'package:aranduapp/ui/shared/OrDivider.dart'; import 'package:aranduapp/ui/shared/TextAndLink.dart'; import 'package:aranduapp/ui/shared/TextName.dart'; +import 'package:aranduapp/ui/shared/requestbutton.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:aranduapp/core/log/Log.dart'; @@ -112,43 +113,23 @@ class RegisterAccountScreen extends StatelessWidget { Widget _buildRegisterButton(BuildContext context) { final viewModel = Provider.of(context); - return ListenableBuilder( - listenable: viewModel.registerCommand, - builder: (context, child) { - if (viewModel.registerCommand.isError) { - WidgetsBinding.instance.addPostFrameCallback((_) { - showDialog( - context: context, - builder: (BuildContext context) => ErrorPopUp( - content: Text(viewModel.registerCommand.result!.asError!.error - .toString()), - ), - ); - }); - } - - if (viewModel.registerCommand.isOk) { - WidgetsBinding.instance.addPostFrameCallback((_) { - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar(content: Text('conta criada com sucesso!!!')), - ); - }); - } - - return SizedBox( - width: 291, - height: 64, - child: ElevatedButton( - onPressed: () async { - viewModel.registerCommand.execute(); - }, - child: viewModel.registerCommand.running - ? const CircularProgressIndicator(value: null) - : const Text('Registrar'), - ), - ); - }, - ); + return Requestbutton( + command: viewModel.registerCommand, + nameButton: 'Registrar', + onSuccessCallback: () { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('conta criada com sucesso!!!')), + ); + }, + onErrorCallback: (e) { + showDialog( + context: context, + builder: (BuildContext context) => ErrorPopUp( + content: Text( + viewModel.registerCommand.result!.asError!.error.toString()), + ), + ); + }); } Widget _buildGoogleLoginButton(BuildContext context) { From c3105bc57073dea3a1773ae5f4ead4bef3c686f3 Mon Sep 17 00:00:00 2001 From: Gabriel Costa de Oliveira Date: Fri, 10 Jan 2025 00:45:49 -0300 Subject: [PATCH 2/2] test(#59): adiciona teste no Checkbox --- .../view/register_account_view.dart | 22 +++--------- .../viewModel/register_view_model.dart | 2 +- .../view/register_account_view_test.dart | 35 +++++++++++++++---- .../register_account_view_test.mocks.dart | 4 +-- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/lib/ui/register_account/view/register_account_view.dart b/lib/ui/register_account/view/register_account_view.dart index 9f80479..1cb3e87 100644 --- a/lib/ui/register_account/view/register_account_view.dart +++ b/lib/ui/register_account/view/register_account_view.dart @@ -89,25 +89,13 @@ class RegisterAccountScreen extends StatelessWidget { Widget _buildTermsCheckbox(BuildContext context) { final viewModel = Provider.of(context); - return Row( - children: [ - Checkbox( + + return Checkbox( value: viewModel.isTermsAccepted, onChanged: (value) { - // Ação ao clicar no checkbox - if (value != null) { - viewModel.toggleTermsAccepted(value); - } - }, - ), - Expanded( - child: Text( - 'Aceite os termos de privacidade', - style: Theme.of(context).textTheme.bodySmall, - ), - ), - ], - ); + viewModel.setToggleTermsAccepted(value??false); + }); + } Widget _buildRegisterButton(BuildContext context) { diff --git a/lib/ui/register_account/viewModel/register_view_model.dart b/lib/ui/register_account/viewModel/register_view_model.dart index 164d91b..19f7be8 100644 --- a/lib/ui/register_account/viewModel/register_view_model.dart +++ b/lib/ui/register_account/viewModel/register_view_model.dart @@ -40,7 +40,7 @@ class RegisterAccountViewModel extends ChangeNotifier { return Result.value(null); } - void toggleTermsAccepted(bool value) { + void setToggleTermsAccepted(bool value) { isTermsAccepted = value; notifyListeners(); } diff --git a/test/ui/register_account/view/register_account_view_test.dart b/test/ui/register_account/view/register_account_view_test.dart index b770d47..8bc36cf 100644 --- a/test/ui/register_account/view/register_account_view_test.dart +++ b/test/ui/register_account/view/register_account_view_test.dart @@ -69,8 +69,6 @@ void main() { testWidgets('Test sending the request', (WidgetTester tester) async { await tester.pumpWidget(createLoginScreen(mockViewModel)); - when(mockViewModel.registerCommand).thenReturn(mockCommand0); - final sendButton = find.text('Registrar'); await tester.tap(sendButton); @@ -80,8 +78,6 @@ void main() { }); testWidgets('Register Account Test User Input', (WidgetTester tester) async { - when(mockViewModel.registerCommand).thenReturn(mockCommand0); - await tester.pumpWidget(createLoginScreen(mockViewModel)); const name = 'test'; @@ -103,9 +99,37 @@ void main() { expect(mockViewModel.emailController.text, email); }); + testWidgets('Checkbox shows correct value when initially unchecked', (WidgetTester tester) async { + when(mockViewModel.isTermsAccepted).thenReturn(false); + + await tester.pumpWidget(createLoginScreen(mockViewModel)); + await tester.pumpAndSettle(); + + Checkbox checkbox = tester.widget(find.byType(Checkbox)); + expect(checkbox.value, false); + + await tester.tap(find.byType(Checkbox)); + + verify(mockViewModel.setToggleTermsAccepted(true)).called(1); + }); + + + testWidgets('Checkbox shows correct value when initially checked', (WidgetTester tester) async { + when(mockViewModel.isTermsAccepted).thenReturn(true); + + await tester.pumpWidget(createLoginScreen(mockViewModel)); + await tester.pumpAndSettle(); + + Checkbox checkbox = tester.widget(find.byType(Checkbox)); + expect(checkbox.value, true); + + await tester.tap(find.byType(Checkbox)); + + verify(mockViewModel.setToggleTermsAccepted(false)).called(1); + }); + testWidgets('Register Account user notification snackbar', (WidgetTester tester) async { - when(mockViewModel.registerCommand).thenReturn(mockCommand0); when(mockCommand0.isOk).thenReturn(true); await tester.pumpWidget(createLoginScreen(mockViewModel)); @@ -129,5 +153,4 @@ void main() { expect(find.byType(ErrorPopUp), findsOneWidget); expect(find.text(error), findsOneWidget); }); - } diff --git a/test/ui/register_account/view/register_account_view_test.mocks.dart b/test/ui/register_account/view/register_account_view_test.mocks.dart index a2b45f3..f785872 100644 --- a/test/ui/register_account/view/register_account_view_test.mocks.dart +++ b/test/ui/register_account/view/register_account_view_test.mocks.dart @@ -184,9 +184,9 @@ class MockRegisterAccountViewModel extends _i2.Mock ) as bool); @override - void toggleTermsAccepted(bool? value) => super.noSuchMethod( + void setToggleTermsAccepted(bool? value) => super.noSuchMethod( Invocation.method( - #toggleTermsAccepted, + #setToggleTermsAccepted, [value], ), returnValueForMissingStub: null,