Skip to content

Commit

Permalink
Merge pull request #85 from fga-eps-mds/feat#58/teste_edit_password
Browse files Browse the repository at this point in the history
test(#58): criando teste para edit profile view
  • Loading branch information
GabrielCostaDeOliveira authored Jan 18, 2025
2 parents 0fc15f2 + 047454c commit cb0f9dd
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 30 deletions.
2 changes: 2 additions & 0 deletions lib/core/di/locator.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:aranduapp/ui/edit_password/di/di.dart';
import 'package:aranduapp/ui/edit_profile/di/di.dart';
import 'package:aranduapp/ui/login/di/di.dart';
import 'package:aranduapp/ui/register_account/di/di.dart';
Expand All @@ -9,4 +10,5 @@ void setupLocator() {
setupLoginDI();
setupRegisterDI();
setupEditProfileDI();
setupPasswordDI();
}
9 changes: 9 additions & 0 deletions lib/ui/edit_password/di/di.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:aranduapp/ui/edit_password/service/edit_password_service.dart';
import 'package:aranduapp/ui/edit_password/viewmodel/edit_password_viewmodel.dart';
import 'package:get_it/get_it.dart';

void setupPasswordDI(){

GetIt.instance.registerFactory(() => EditPasswordViewModel());
GetIt.instance.registerSingleton(() => EditPasswordService());
}
2 changes: 1 addition & 1 deletion lib/ui/edit_password/service/edit_password_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:aranduapp/ui/edit_password/model/edit_password_request.dart';

class EditPasswordService {

static Future<void> edit(EditPasswordRequest editPasswordRequest) async {
Future<void> edit(EditPasswordRequest editPasswordRequest) async {
await BaseApi.getInstance(auth: true)
.put(path: '/auth/change-password', data: editPasswordRequest.toJson());
}
Expand Down
30 changes: 22 additions & 8 deletions lib/ui/edit_password/view/edit_password_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:aranduapp/ui/edit_password/model/edit_password_request.dart';
import 'package:aranduapp/ui/edit_password/viewmodel/edit_password_viewmodel.dart';
import 'package:aranduapp/ui/shared/command_button.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:provider/provider.dart';

import 'package:aranduapp/ui/shared/text_password.dart';
Expand All @@ -10,15 +12,19 @@ class EditPassword extends StatelessWidget {

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => EditPasswordViewModel(),
child: const EditPasswordScreen(),
return ChangeNotifierProvider<EditPasswordViewModel>.value(
value: GetIt.instance<EditPasswordViewModel>(),
child: EditPasswordScreen(),
);
}
}

class EditPasswordScreen extends StatelessWidget {
const EditPasswordScreen({super.key});
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController newPasswordController = TextEditingController();
final TextEditingController oldPasswordController = TextEditingController();

EditPasswordScreen({super.key});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -63,16 +69,18 @@ class EditPasswordScreen extends StatelessWidget {
Provider.of<EditPasswordViewModel>(context);

return Form(
key: viewModel.formKey,
key: formKey,
child: Column(children: [
TextPassWord(
key: const Key('old_password') ,
label: "Senha Antiga",
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
controller: viewModel.oldPasswordController),
controller: oldPasswordController),
TextPassWord(
key: const Key('new_password') ,
label: "Senha Nova",
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
controller: viewModel.newPasswordController),
controller: newPasswordController),
const SizedBox(height: 80),
_button(context, viewModel)
]),
Expand All @@ -81,7 +89,13 @@ class EditPasswordScreen extends StatelessWidget {

Widget _button(BuildContext context, EditPasswordViewModel viewModel) {
return CommandButton(
tap: viewModel.editCommand.execute ,
tap: () {
if (formKey.currentState!.validate()) {
viewModel.editCommand.execute(EditPasswordRequest(
oldPassword: oldPasswordController.text,
newPassword: newPasswordController.text));
}
},
command: viewModel.editCommand,
nameButton: "Enviar",
onErrorCallback: (e) {
Expand Down
27 changes: 6 additions & 21 deletions lib/ui/edit_password/viewmodel/edit_password_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,17 @@ import 'package:aranduapp/ui/edit_password/model/edit_password_request.dart';
import 'package:aranduapp/ui/edit_password/service/edit_password_service.dart';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

class EditPasswordViewModel extends ChangeNotifier {
final GlobalKey<FormState> formKey;
final TextEditingController newPasswordController;
final TextEditingController oldPasswordController;
late Command1<void, EditPasswordRequest> editCommand;

late Command0<void> editCommand;

EditPasswordViewModel()
: formKey = GlobalKey<FormState>(),
newPasswordController = TextEditingController(),
oldPasswordController = TextEditingController() {
editCommand = Command0<void>(editPassword);
EditPasswordViewModel() {
editCommand = Command1<void, EditPasswordRequest>(editPassword);
}

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

EditPasswordRequest request = EditPasswordRequest(
oldPassword: oldPasswordController.text,
newPassword: newPasswordController.text);

await EditPasswordService.edit(request);

Future<Result<void>> editPassword(EditPasswordRequest request) async {
await GetIt.instance<EditPasswordService>().edit(request);
return Result.value(null);
}
}
84 changes: 84 additions & 0 deletions test/ui/edit_password/view/edit_password_view_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:aranduapp/ui/edit_password/model/edit_password_request.dart';
import 'package:aranduapp/ui/edit_password/view/edit_password_view.dart';
import 'package:aranduapp/ui/edit_password/viewmodel/edit_password_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_password_view_test.mocks.dart';

@GenerateNiceMocks([MockSpec<EditPasswordViewModel>(), MockSpec<Command1>()])
void main() {
late MockEditPasswordViewModel mockEditPasswordViewModel;
late MockCommand1<void, EditPasswordRequest> mockEditPasswordCommand1;

setUp(() async {
mockEditPasswordViewModel = MockEditPasswordViewModel();

mockEditPasswordCommand1 = MockCommand1();

when(mockEditPasswordViewModel.editCommand)
.thenReturn(mockEditPasswordCommand1);

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

await GetIt.instance.reset();
GetIt.I.registerLazySingleton<EditPasswordViewModel>(
() => mockEditPasswordViewModel);
});

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

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

expect(find.byType(AppBar), findsOneWidget);
expect(find.byKey(const ValueKey('old_password')), findsOneWidget);
expect(find.byKey(const ValueKey('new_password')), findsOneWidget);
expect(find.byType(CommandButton), findsOneWidget);
});

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

const oldPassword = 'test@example.com';
const newPassword = 'password123';

await tester.enterText(find.byKey(const Key('old_password')), oldPassword);
await tester.enterText(find.byKey(const Key('new_password')), newPassword);

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

verify(mockEditPasswordCommand1.execute(argThat(
predicate<EditPasswordRequest>((req) =>
req.oldPassword == oldPassword &&
req.newPassword == newPassword)))).called(1);
});

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

const oldPassword = 'test';
const newPassword = 'password123';

await tester.enterText(find.byKey(const Key('old_password')), oldPassword);
await tester.enterText(find.byKey(const Key('new_password')), newPassword);

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

verifyNever(mockEditPasswordCommand1.execute(argThat(isNotNull)));
});
}
Loading

0 comments on commit cb0f9dd

Please sign in to comment.