Skip to content

Commit

Permalink
test(#58): corrige testes
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCostaDeOliveira committed Jan 25, 2025
1 parent 480746a commit 95f3138
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 141 deletions.
72 changes: 40 additions & 32 deletions lib/ui/edit_profile/view/edit_profile_view.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:aranduapp/core/network/token_manager/model/user_model.dart';
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/shared/text_email.dart';
Expand Down Expand Up @@ -64,38 +65,45 @@ class EditProfileScreen extends StatelessWidget {
}

Widget _buildForm(BuildContext context, EditProfileViewModel viewModel) {
return Form(
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextName(
key: const Key("nameController"),
controller: nameController,
padding: const EdgeInsets.symmetric(vertical: 0),
initialText: viewModel.user?.name ?? "",
),
const SizedBox(height: 20),
TextName(
key: const Key("userNameController"),
label: "Nome de Usuário",
controller: userNameController,
padding: const EdgeInsets.symmetric(vertical: 0),
),
const SizedBox(height: 20),
TextEmail(
key: const Key("emailNameController"),
padding: const EdgeInsets.symmetric(vertical: 0),
controller: emailController,
initialText: viewModel.user?.email ?? "",
),
const SizedBox(height: 100),
_saveButton(context, viewModel),
const SizedBox(height: 20),
// _deleteButton(context),
],
),
);
viewModel.getUserCommand.execute();

return ListenableBuilder(
listenable: viewModel.getUserCommand,
builder: (context, child) {
UserModel? user = viewModel.getUserCommand.result?.asValue?.value;

return Form(
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextName(
key: const Key("nameController"),
controller: nameController,
padding: const EdgeInsets.symmetric(vertical: 0),
initialText: user?.name ?? "",
),
const SizedBox(height: 20),
TextName(
key: const Key("userNameController"),
label: "Nome de Usuário",
controller: userNameController,
padding: const EdgeInsets.symmetric(vertical: 0),
),
const SizedBox(height: 20),
TextEmail(
key: const Key("emailNameController"),
padding: const EdgeInsets.symmetric(vertical: 0),
controller: emailController,
initialText: user?.email ?? "",
),
const SizedBox(height: 100),
_saveButton(context, viewModel),
const SizedBox(height: 20),
],
),
);
});
}

Widget _saveButton(BuildContext context, EditProfileViewModel viewModel) {
Expand Down
12 changes: 2 additions & 10 deletions lib/ui/edit_profile/viewmodel/edit_profile_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import 'package:get_it/get_it.dart';

class EditProfileViewModel extends ChangeNotifier {
late Command1<void, EditProfileRequest> editCommand;
late Command0<void> getUserCommand;

UserModel? user;
late Command0<UserModel> getUserCommand;

EditProfileViewModel() {
editCommand = Command1<void, EditProfileRequest>(editProfile);
getUserCommand = Command0<void>(getUser);
getUserCommand = Command0<UserModel>(getUser);

getUserCommand.execute();
}
Expand All @@ -29,12 +27,6 @@ class EditProfileViewModel extends ChangeNotifier {

Future<Result<UserModel>> getUser() async {
UserModel user = await GetIt.instance<AuthRepository>().getUser();
_setUser(user);
return Result.value(user);
}

void _setUser(UserModel user) {
this.user = user;
notifyListeners();
}
}
125 changes: 66 additions & 59 deletions lib/ui/profile/view/profile_view.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:aranduapp/core/network/token_manager/model/user_model.dart';
import 'package:aranduapp/ui/edit_delete_user/view/edit_delete_user_view.dart';
import 'package:aranduapp/ui/edit_password/view/edit_password_view.dart';
import 'package:aranduapp/ui/login/view/login_view.dart';
Expand Down Expand Up @@ -74,10 +75,16 @@ class Profile extends StatelessWidget {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ProfileHeader(
name: viewModel.user == null ?"..." : viewModel.user!.name ,
role: viewModel.user == null ?"..." : viewModel.user!.role ,
),
ListenableBuilder(
listenable: viewModel.getUserCommand,
builder: (context, child) {
UserModel? user = viewModel.getUserCommand.result?.asValue?.value;

return ProfileHeader(
name: user?.name == null ? "..." : user!.name,
role: user?.role == null ? "..." : user!.role,
);
}),
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: ElevatedButton(
Expand Down Expand Up @@ -122,75 +129,75 @@ class Profile extends StatelessWidget {

ListTile _editPassword(BuildContext context) {
return ListTile(
leading: Icon(
Icons.lock_reset,
color: Theme.of(context).colorScheme.primary,
size: 32,
),
title: const Text('Trocar senha'),
trailing: Icon(
Icons.chevron_right,
color: Theme.of(context).colorScheme.primary,
size: 32,
leading: Icon(
Icons.lock_reset,
color: Theme.of(context).colorScheme.primary,
size: 32,
),
title: const Text('Trocar senha'),
trailing: Icon(
Icons.chevron_right,
color: Theme.of(context).colorScheme.primary,
size: 32,
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const EditPassword(),
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const EditPassword(),
),
);
},
);
},
);
}

ListTile _deleteAccount(BuildContext context) {
return ListTile(
leading: Icon(
Icons.delete_forever,
color: Theme.of(context).colorScheme.error,
size: 32,
),
title: const Text('Apagar conta'),
trailing: Icon(
Icons.chevron_right,
color: Theme.of(context).colorScheme.error,
size: 32,
leading: Icon(
Icons.delete_forever,
color: Theme.of(context).colorScheme.error,
size: 32,
),
title: const Text('Apagar conta'),
trailing: Icon(
Icons.chevron_right,
color: Theme.of(context).colorScheme.error,
size: 32,
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const EditDeleteUser(),
),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const EditDeleteUser(),
),
);
},
);
},
);
}

ListenableBuilder _logout(ProfileViewModel viewModel) {
return ListenableBuilder(
listenable: viewModel.logoutCommand,
builder: (context, child) {
if (viewModel.logoutCommand.isOk) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const Login(),
),
);
});
}

return ListTile(
key: const Key('logout_button'),
leading: Icon(
Icons.logout_sharp,
color: Theme.of(context).colorScheme.error,
size: 32,
listenable: viewModel.logoutCommand,
builder: (context, child) {
if (viewModel.logoutCommand.isOk) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => const Login(),
),
title: const Text('Sair'),
onTap: viewModel.logoutCommand.execute,
);
},
});
}

return ListTile(
key: const Key('logout_button'),
leading: Icon(
Icons.logout_sharp,
color: Theme.of(context).colorScheme.error,
size: 32,
),
title: const Text('Sair'),
onTap: viewModel.logoutCommand.execute,
);
},
);
}
}
11 changes: 2 additions & 9 deletions lib/ui/profile/viewmodel/profile_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import 'package:get_it/get_it.dart';

class ProfileViewModel extends ChangeNotifier {
late Command0<void> logoutCommand;
late Command0<void> getUserCommand;
late Command0<UserModel> getUserCommand;

UserModel? user;

ProfileViewModel() {
logoutCommand = Command0<void>(logout);
getUserCommand = Command0<void>(getUser);
getUserCommand = Command0<UserModel>(getUser);

if (user == null) getUserCommand.execute();
}

Future<Result<void>> logout() async {
Expand All @@ -29,12 +27,7 @@ class ProfileViewModel extends ChangeNotifier {

Future<Result<UserModel>> getUser() async {
UserModel user = await GetIt.instance<AuthRepository>().getUser();
_setUser(user);
return Result.value(user);
}

void _setUser(UserModel user) {
this.user = user;
notifyListeners();
}
}
14 changes: 11 additions & 3 deletions test/ui/edit_profile/view/edit_profile_view_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:aranduapp/core/network/token_manager/model/user_model.dart';
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';
Expand All @@ -11,25 +12,32 @@ import 'package:mockito/mockito.dart';

import 'edit_profile_view_test.mocks.dart';

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

void main() {

late MockEditProfileViewModel mockEditProfileViewModel;
late MockCommand1<void, EditProfileRequest> mockEditProfileCommand1;
late MockCommand0<UserModel> mockGetUserCommand1;

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);

mockGetUserCommand1 = MockCommand0();
when(mockEditProfileViewModel.getUserCommand)
.thenReturn(mockGetUserCommand1);
when(mockGetUserCommand1.running).thenReturn(false);
when(mockGetUserCommand1.isError).thenReturn(false);
when(mockGetUserCommand1.isOk).thenReturn(false);


await GetIt.instance.reset();
GetIt.I.registerLazySingleton<EditProfileViewModel>(
() => mockEditProfileViewModel);
Expand Down
Loading

0 comments on commit 95f3138

Please sign in to comment.