Skip to content

Commit

Permalink
Merge pull request #78 from fga-eps-mds/feat#75/di_register
Browse files Browse the repository at this point in the history
ref(#58) adiciona injeção de dependência no registro
  • Loading branch information
GabrielCostaDeOliveira authored Jan 15, 2025
2 parents 5d27acb + 1ada268 commit 1ebff0a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/core/di/locator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:aranduapp/ui/login/di/di.dart';
import 'package:aranduapp/ui/register_account/di/di.dart';
import 'package:get_it/get_it.dart';

final GetIt locator = GetIt.instance;

void setupLocator() {
setupLoginDI();
setupRegisterDI();
}
10 changes: 10 additions & 0 deletions lib/ui/register_account/di/di.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:aranduapp/ui/register_account/service/register_service.dart';
import 'package:aranduapp/ui/register_account/viewmodel/register_viewmodel.dart';
import 'package:get_it/get_it.dart';

void setupRegisterDI() {

GetIt.I.registerLazySingleton(() => RegisterService());

GetIt.I.registerFactory(() => RegisterAccountViewModel());
}
2 changes: 1 addition & 1 deletion lib/ui/register_account/service/register_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:aranduapp/core/network/base_api.dart';
import '../model/register_request.dart';

class RegisterService {
static Future<void> register(RegisterRequest registerRequest) async {
Future<void> register(RegisterRequest registerRequest) async {
await BaseApi.getInstance(auth: false)
.post(path: '/users', data: <String, dynamic>{
'name': registerRequest.name,
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/register_account/view/register_account_view.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:aranduapp/ui/login/view/login_view.dart';
import 'package:aranduapp/ui/shared/or_divider.dart';
import 'package:aranduapp/ui/shared/text_and_link.dart';
import 'package:aranduapp/ui/shared/text_name.dart';
import 'package:aranduapp/ui/shared/request_button.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:provider/provider.dart';
import 'package:aranduapp/core/log/log.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
Expand All @@ -20,8 +20,8 @@ class RegisterAccount extends StatelessWidget {

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => RegisterAccountViewModel(),
return ChangeNotifierProvider<RegisterAccountViewModel>.value(
value: GetIt.instance<RegisterAccountViewModel>(),
child: const RegisterAccountScreen(),
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/register_account/viewmodel/register_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:aranduapp/core/state/command.dart';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import '../model/register_request.dart';
import '../service/register_service.dart';

Expand Down Expand Up @@ -30,7 +31,7 @@ class RegisterAccountViewModel extends ChangeNotifier {
return Result.error('Por favor, preencha todos os campos corretamente');
}

await RegisterService.register(RegisterRequest(
await GetIt.instance<RegisterService>().register(RegisterRequest(
email: emailController.text,
name: nameController.text,
userName: userNameController.text,
Expand Down
35 changes: 18 additions & 17 deletions test/ui/register_account/view/register_account_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import 'package:aranduapp/ui/shared/title_slogan.dart';
import 'package:async/async.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 'package:provider/provider.dart';

@GenerateNiceMocks([MockSpec<RegisterAccountViewModel>(), MockSpec<Command0>()])
import 'package:aranduapp/ui/register_account/viewmodel/register_viewmodel.dart';
Expand All @@ -21,7 +21,7 @@ void main() {
late MockRegisterAccountViewModel mockViewModel;
late MockCommand0 mockCommand0;

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

Expand All @@ -34,16 +34,17 @@ void main() {
when(mockViewModel.userNameController).thenReturn(TextEditingController());
when(mockViewModel.passwordController).thenReturn(TextEditingController());
when(mockViewModel.isTermsAccepted).thenReturn(false);



await GetIt.instance.reset();
GetIt.I.registerLazySingleton<RegisterAccountViewModel>(() => mockViewModel);
});

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

Widget createLoginScreen() {
return const MaterialApp(
home: RegisterAccount(),
);
}

Expand All @@ -52,7 +53,7 @@ void main() {
when(mockViewModel.registerCommand).thenReturn(
Command0<void>(() => Future.delayed(const Duration(seconds: 1))));

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

expect(find.byType(TitleSlogan), findsOneWidget);
Expand All @@ -67,7 +68,7 @@ void main() {
});

testWidgets('Test sending the request', (WidgetTester tester) async {
await tester.pumpWidget(createLoginScreen(mockViewModel));
await tester.pumpWidget(createLoginScreen());

final sendButton = find.text('Registrar');

Expand All @@ -78,7 +79,7 @@ void main() {
});

testWidgets('Register Account Test User Input', (WidgetTester tester) async {
await tester.pumpWidget(createLoginScreen(mockViewModel));
await tester.pumpWidget(createLoginScreen());

const name = 'test';
const email = 'test@example.com';
Expand All @@ -103,7 +104,7 @@ void main() {
(WidgetTester tester) async {
when(mockViewModel.isTermsAccepted).thenReturn(false);

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

Checkbox checkbox = tester.widget(find.byType(Checkbox));
Expand All @@ -118,7 +119,7 @@ void main() {
(WidgetTester tester) async {
when(mockViewModel.isTermsAccepted).thenReturn(true);

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

Checkbox checkbox = tester.widget(find.byType(Checkbox));
Expand All @@ -133,7 +134,7 @@ void main() {
(WidgetTester tester) async {
when(mockCommand0.isOk).thenReturn(true);

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

expect(find.byType(SnackBar), findsOneWidget);
Expand All @@ -148,7 +149,7 @@ void main() {

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

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

expect(find.byType(ErrorPopUp), findsOneWidget);
Expand Down

0 comments on commit 1ebff0a

Please sign in to comment.