Skip to content

Commit

Permalink
test(#59): adiciona teste da view
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Dec 27, 2024
1 parent 1b6eb75 commit 2c2a76d
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 16 deletions.
25 changes: 11 additions & 14 deletions lib/ui/register_account/view/register_account_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RegisterAccount extends StatelessWidget {
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => RegisterAccountViewModel(),
child: const RegisterAccountScreen(),
child: const RegisterAccountScreen(),
);
}
}
Expand Down Expand Up @@ -64,10 +64,12 @@ class RegisterAccountScreen extends StatelessWidget {
key: viewModel.formKey,
child: Column(children: [
TextName(
key: const Key('nameField'),
label: 'Nome',
controller: viewModel.nameController,
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20)),
TextName(
key: const Key('userNameField'),
label: 'Nome de Usuário',
controller: viewModel.userNameController,
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20)),
Expand Down Expand Up @@ -111,27 +113,24 @@ class RegisterAccountScreen extends StatelessWidget {
final viewModel = Provider.of<RegisterAccountViewModel>(context);

return ListenableBuilder(

listenable: viewModel.registercomand,
listenable: viewModel.registerCommand,
builder: (context, child) {
if (viewModel.registercomand.isError) {
if (viewModel.registerCommand.isError) {
WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog<Object>(
context: context,
builder: (BuildContext context) => ErrorPopUp(
content: Text(
viewModel.registercomand.result!.asError!.error.toString()),
content: Text(viewModel.registerCommand.result!.asError!.error
.toString()),
),
);
});
}

if (viewModel.registercomand.isOk) {
if (viewModel.registerCommand.isOk) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'conta criada com sucesso!!!')),
const SnackBar(content: Text('conta criada com sucesso!!!')),
);
});
}
Expand All @@ -141,9 +140,9 @@ class RegisterAccountScreen extends StatelessWidget {
height: 64,
child: ElevatedButton(
onPressed: () async {
viewModel.registercomand.execute();
viewModel.registerCommand.execute();
},
child: viewModel.registercomand.running
child: viewModel.registerCommand.running
? const CircularProgressIndicator(value: null)
: const Text('Registrar'),
),
Expand Down Expand Up @@ -171,6 +170,4 @@ class RegisterAccountScreen extends StatelessWidget {
),
);
}

}

4 changes: 2 additions & 2 deletions lib/ui/register_account/viewModel/register_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class RegisterAccountViewModel extends ChangeNotifier {

bool isTermsAccepted = false;

late Command0<void> registercomand;
late Command0<void> registerCommand;

RegisterAccountViewModel() {
registercomand = Command0<void>(_register);
registerCommand = Command0<void>(_register);
}

Future<Result<void>> _register() async {
Expand Down
134 changes: 134 additions & 0 deletions test/ui/register_account/view/register_account_view_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:aranduapp/ui/register_account/view/register_account_view.dart';
import 'package:aranduapp/ui/shared/ErrorPopUp.dart';
import 'package:aranduapp/ui/shared/TextAndLink.dart';
import 'package:aranduapp/ui/shared/TextEmail.dart';
import 'package:aranduapp/ui/shared/TextName.dart';
import 'package:aranduapp/ui/shared/TextPassword.dart';
import 'package:aranduapp/ui/shared/TitleSlogan.dart';
import 'package:async/async.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<RegisterAccountViewModel>(), MockSpec<Command0>()])
import 'package:aranduapp/ui/register_account/viewModel/register_view_model.dart';

import 'register_account_view_test.mocks.dart';

void main() {
late MockRegisterAccountViewModel mockViewModel;
late MockCommand0 mockCommand0;

setUp(() {
mockViewModel = MockRegisterAccountViewModel();
mockCommand0 = MockCommand0();

when(mockCommand0.execute()).thenAnswer((_) async => Result.value(null));

when(mockViewModel.registerCommand).thenReturn(mockCommand0);
when(mockViewModel.formKey).thenReturn(GlobalKey<FormState>());
when(mockViewModel.nameController).thenReturn(TextEditingController());
when(mockViewModel.emailController).thenReturn(TextEditingController());
when(mockViewModel.userNameController).thenReturn(TextEditingController());
when(mockViewModel.passwordController).thenReturn(TextEditingController());
when(mockViewModel.isTermsAccepted).thenReturn(false);
});

Widget createLoginScreen(MockRegisterAccountViewModel mockViewModel) {
return ChangeNotifierProvider<RegisterAccountViewModel>.value(
value: mockViewModel,
builder: (context, child) {
return const MaterialApp(
home: RegisterAccountScreen(),
);
},
);
}

testWidgets('Register Account screen displays fields and send button',
(WidgetTester tester) async {
when(mockViewModel.registerCommand).thenReturn(
Command0<void>(() => Future.delayed(const Duration(seconds: 1))));

await tester.pumpWidget(createLoginScreen(mockViewModel));
await tester.pump();

expect(find.byType(TitleSlogan), findsOneWidget);
expect(find.byKey(const Key('nameField')), findsOneWidget);
expect(find.byKey(const Key('userNameField')), findsOneWidget);
expect(find.byType(TextEmail), findsOneWidget);
expect(find.byType(TextPassWord), findsOneWidget);
expect(find.byType(Checkbox), findsOneWidget);

expect(find.text('Registrar'), findsOneWidget);
expect(find.byType(TextAndLink), findsOneWidget);
});

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);
await tester.pump();

verify(mockCommand0.execute()).called(1);
});

testWidgets('Register Account Test User Input', (WidgetTester tester) async {
when(mockViewModel.registerCommand).thenReturn(mockCommand0);

await tester.pumpWidget(createLoginScreen(mockViewModel));

const name = 'test';
const email = '[email protected]';
const userName = 'test123';
const password = 'password123';

await tester.pumpAndSettle();

await tester.enterText(find.byKey(const Key('nameField')), name);
await tester.enterText(find.byKey(const Key('userNameField')), userName);
await tester.enterText(find.byType(TextEmail), email);
await tester.enterText(find.byType(TextPassWord), password);
await tester.pumpAndSettle();

expect(mockViewModel.nameController.text, name);
expect(mockViewModel.userNameController.text, userName);
expect(mockViewModel.passwordController.text, password);
expect(mockViewModel.emailController.text, email);
});

testWidgets('Register Account user notification snackbar',
(WidgetTester tester) async {
when(mockViewModel.registerCommand).thenReturn(mockCommand0);
when(mockCommand0.isOk).thenReturn(true);

await tester.pumpWidget(createLoginScreen(mockViewModel));
await tester.pumpAndSettle();

expect(find.byType(SnackBar), findsOneWidget);
});

testWidgets('Register Account user notification popup error',
(WidgetTester tester) async {
when(mockViewModel.registerCommand).thenReturn(mockCommand0);
when(mockCommand0.isError).thenReturn(true);

const error = 'error message.';

when(mockCommand0.result).thenReturn(Result.error(error));

await tester.pumpWidget(createLoginScreen(mockViewModel));
await tester.pumpAndSettle();

expect(find.byType(ErrorPopUp), findsOneWidget);
expect(find.text(error), findsOneWidget);
});

}
Loading

0 comments on commit 2c2a76d

Please sign in to comment.