Skip to content

Commit

Permalink
test(#58): corrigindo tela e criando casos de teste
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Jan 20, 2025
1 parent 5d8b046 commit 51ab7ac
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 33 deletions.
35 changes: 25 additions & 10 deletions lib/ui/edit_profile/view/edit_profile_view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:aranduapp/ui/edit_profile/model/edit_profile_request.dart';
import 'package:aranduapp/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart';
import 'package:aranduapp/ui/login/viewmodel/login_viewmodel.dart';
import 'package:aranduapp/ui/shared/text_email.dart';
import 'package:aranduapp/ui/shared/text_name.dart';
import 'package:aranduapp/ui/shared/command_button.dart';
Expand All @@ -12,15 +12,20 @@ class EditProfile extends StatelessWidget {

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<LoginViewModel>.value(
value: GetIt.instance<LoginViewModel>(),
child: const EditProfileScreen(),
return ChangeNotifierProvider<EditProfileViewModel>.value(
value: GetIt.instance<EditProfileViewModel>(),
child: EditProfileScreen(),
);
}
}

class EditProfileScreen extends StatelessWidget {
const EditProfileScreen({super.key});
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController nameController = TextEditingController();
final TextEditingController userNameController = TextEditingController();
final TextEditingController emailController = TextEditingController();

EditProfileScreen({super.key});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -60,24 +65,27 @@ class EditProfileScreen extends StatelessWidget {

Widget _buildForm(BuildContext context, EditProfileViewModel viewModel) {
return Form(
key: viewModel.formKey,
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextName(
controller: viewModel.nameController,
key: const Key("nameController"),
controller: nameController,
padding: const EdgeInsets.symmetric(vertical: 0),
),
const SizedBox(height: 20),
TextName(
key: const Key("userNameController"),
label: "Nome de Usuário",
controller: viewModel.userNameController,
controller: userNameController,
padding: const EdgeInsets.symmetric(vertical: 0),
),
const SizedBox(height: 20),
TextEmail(
key: const Key("emailNameController"),
padding: const EdgeInsets.symmetric(vertical: 0),
controller: viewModel.emailController,
controller: emailController,
),
const SizedBox(height: 100),
_saveButton(context, viewModel),
Expand All @@ -90,7 +98,14 @@ class EditProfileScreen extends StatelessWidget {

Widget _saveButton(BuildContext context, EditProfileViewModel viewModel) {
return CommandButton(
tap: viewModel.editCommand.execute ,
tap: () {
if (formKey.currentState!.validate()) {
viewModel.editCommand.execute(EditProfileRequest(
name: nameController.text,
email: emailController.text,
userName: userNameController.text));
}
},
command: viewModel.editCommand,
nameButton: "Salvar",
onErrorCallback: (e) {
Expand Down
27 changes: 4 additions & 23 deletions lib/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,14 @@ import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

class EditProfileViewModel extends ChangeNotifier {
final GlobalKey<FormState> formKey;
final TextEditingController nameController;
final TextEditingController userNameController;
final TextEditingController emailController;
late Command1<void, EditProfileRequest> editCommand;

late Command0<void> editCommand;

EditProfileViewModel()
: formKey = GlobalKey<FormState>(),
nameController = TextEditingController(),
userNameController = TextEditingController(),
emailController = TextEditingController() {
editCommand = Command0<void>(editProfile);
EditProfileViewModel() {
editCommand = Command1<void, EditProfileRequest>(editProfile);
}

Future<Result<void>> editProfile() async {
if (!formKey.currentState!.validate()) {
return Result.error(Exception('Valores inválidos'));
}

EditProfileRequest request = EditProfileRequest(
name: nameController.text,
userName: userNameController.text,
email: emailController.text);

Future<Result<void>> editProfile(EditProfileRequest request) async {
await GetIt.instance<EditProfileService>().edit(request);

return Result.value(null);
}
}
97 changes: 97 additions & 0 deletions test/ui/edit_profile/view/edit_profile_view_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:aranduapp/ui/edit_profile/model/edit_profile_request.dart';
import 'package:aranduapp/ui/edit_profile/view/edit_profile_view.dart';
import 'package:aranduapp/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart';
import 'package:aranduapp/ui/shared/command_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import 'edit_profile_view_test.mocks.dart';

@GenerateNiceMocks([MockSpec<EditProfileViewModel>(), MockSpec<Command1>()])

void main() {

late MockEditProfileViewModel mockEditProfileViewModel;
late MockCommand1<void, EditProfileRequest> mockEditProfileCommand1;

setUp(() async {
mockEditProfileViewModel =MockEditProfileViewModel();

mockEditProfileCommand1 = MockCommand1();

when(mockEditProfileViewModel.editCommand)
.thenReturn(mockEditProfileCommand1);

when(mockEditProfileCommand1.running).thenReturn(false);
when(mockEditProfileCommand1.isError).thenReturn(false);
when(mockEditProfileCommand1.isOk).thenReturn(false);

await GetIt.instance.reset();
GetIt.I.registerLazySingleton<EditProfileViewModel>(
() => mockEditProfileViewModel);
});

Widget createScreen() {
return const MaterialApp(
home: EditProfile(),
);
}

testWidgets('edit profile screen displays', (WidgetTester tester) async {
await tester.pumpWidget(createScreen());
await tester.pumpAndSettle();

expect(find.byType(AppBar), findsOneWidget);
expect(find.byKey(const ValueKey('nameController')), findsOneWidget);
expect(find.byKey(const ValueKey('userNameController')), findsOneWidget);
expect(find.byKey(const ValueKey('emailNameController')), findsOneWidget);
expect(find.byType(CommandButton), findsOneWidget);
});



testWidgets('edit password right values', (WidgetTester tester) async {
await tester.pumpWidget(createScreen());

const name = 'gabriel';
const email = '[email protected]';
const userName = 'dark';

await tester.enterText(find.byKey(const ValueKey('nameController')), name);
await tester.enterText(find.byKey(const ValueKey('emailNameController')), email);
await tester.enterText(find.byKey(const ValueKey('userNameController')), userName);

await tester.tap(find.byKey(const Key('elevated_button_key')));
await tester.pumpAndSettle();

verify(mockEditProfileCommand1.execute(argThat(
predicate<EditProfileRequest>((req) =>
req.name == name &&
req.email == email &&
req.userName == userName
)))).called(1);
});

testWidgets('edit password wrong values', (WidgetTester tester) async {
await tester.pumpWidget(createScreen());

const name = 'gabriel';
const email = 'gab';
const userName = 'dark';

await tester.enterText(find.byKey(const ValueKey('nameController')), name);
await tester.enterText(find.byKey(const ValueKey('emailNameController')), email);
await tester.enterText(find.byKey(const ValueKey('userNameController')), userName);

await tester.tap(find.byKey(const Key('elevated_button_key')));
await tester.pumpAndSettle();

verifyNever(mockEditProfileCommand1.execute(argThat(isNotNull)));
});

}

Loading

0 comments on commit 51ab7ac

Please sign in to comment.