From 66d7028d0f8ae17b50a9bd738900b3bfb885f130 Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Mon, 16 Nov 2026 17:55:57 +0300 Subject: [PATCH] fix(settings): return to settings with success toast after saving related to: #989 --- .../pages/general_info_screen.dart | 2 + .../presentation/pages/other_info_screen.dart | 11 +- .../pages/physical_activity_screen.dart | 12 +- .../pages/physical_form_screen.dart | 11 +- .../presentation/pages/pressure_screen.dart | 11 +- .../pages/general_info_screen_test.dart | 39 ++++-- .../pages/other_info_screen_test.dart | 109 +++++++++++++++- .../pages/physical_activity_screen_test.dart | 118 +++++++++++++++++- .../pages/physical_form_screen_test.dart | 109 +++++++++++++++- .../pages/pressure_screen_test.dart | 109 +++++++++++++++- 10 files changed, 498 insertions(+), 33 deletions(-) diff --git a/lib/features/settings/presentation/pages/general_info_screen.dart b/lib/features/settings/presentation/pages/general_info_screen.dart index f4af2f9d..91d206e3 100644 --- a/lib/features/settings/presentation/pages/general_info_screen.dart +++ b/lib/features/settings/presentation/pages/general_info_screen.dart @@ -11,6 +11,7 @@ import 'package:eatvio_app/core/theme/theme_helper.dart'; import 'package:eatvio_app/core/widgets/custom_text_form_field.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; class GeneralInfoScreen extends StatefulWidget { final SettingsEntity settingsEntity; @@ -256,6 +257,7 @@ class _GeneralInfoScreenState extends State { }); if (detailsSaved && settingsSaved) { showSuccessToast(S.of(context).data_saved_successfully); + context.pop(); } }, borderRadius: BorderRadius.circular(20), diff --git a/lib/features/settings/presentation/pages/other_info_screen.dart b/lib/features/settings/presentation/pages/other_info_screen.dart index 839c440d..35a4cc0e 100644 --- a/lib/features/settings/presentation/pages/other_info_screen.dart +++ b/lib/features/settings/presentation/pages/other_info_screen.dart @@ -2,6 +2,7 @@ import 'package:eatvio_app/features/settings/entities/settings_entity.dart'; import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/settings_state.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; import 'package:eatvio_app/core/widgets/custom_text_form_field.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; @@ -137,8 +138,8 @@ class _OtherInfoScreenState extends State { InkWell _saveButton() { return InkWell( - onTap: () { - widget.settingsEntity.settingsCubit.updateProfileDetails( + onTap: () async { + final saved = await widget.settingsEntity.settingsCubit.updateProfileDetails( profileDetailsModel: (widget.settingsEntity.settingsCubit.state as SettingsStateLoaded).profileDetails.copyWith( aboutAlergy: aboutAllergyController.text, aboutTakingPils: aboutTakingPillsController.text, @@ -148,7 +149,11 @@ class _OtherInfoScreenState extends State { additionalInfo: additionalInfoController.text, ), ); - GoRouter.of(context).pop(); + if (!mounted) return; + if (saved) { + showSuccessToast(S.of(context).data_saved_successfully); + GoRouter.of(context).pop(); + } }, borderRadius: BorderRadius.circular(20), child: Ink( diff --git a/lib/features/settings/presentation/pages/physical_activity_screen.dart b/lib/features/settings/presentation/pages/physical_activity_screen.dart index 74b6f971..26747cd8 100644 --- a/lib/features/settings/presentation/pages/physical_activity_screen.dart +++ b/lib/features/settings/presentation/pages/physical_activity_screen.dart @@ -2,6 +2,8 @@ import 'package:eatvio_app/features/settings/entities/settings_entity.dart'; import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/settings_cubit.dart'; import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/settings_state.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; +import 'package:eatvio_app/generated/l10n.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; @@ -200,7 +202,7 @@ class _PhysicalActivityScreenState extends State { InkWell _saveButton() { return InkWell( - onTap: () { + onTap: () async { late final double activityCoefficient; switch (_physicalActivity) { case PhysicalActivity.minimum: @@ -221,12 +223,16 @@ class _PhysicalActivityScreenState extends State { default: break; } - widget.settingsEntity.settingsCubit.updateProfileDetails( + final saved = await widget.settingsEntity.settingsCubit.updateProfileDetails( profileDetailsModel: (widget.settingsEntity.settingsCubit.state as SettingsStateLoaded).profileDetails.copyWith( activityCoefficient: activityCoefficient, ), ); - GoRouter.of(context).pop(); + if (!mounted) return; + if (saved) { + showSuccessToast(S.of(context).data_saved_successfully); + GoRouter.of(context).pop(); + } }, borderRadius: BorderRadius.circular(20), child: Ink( diff --git a/lib/features/settings/presentation/pages/physical_form_screen.dart b/lib/features/settings/presentation/pages/physical_form_screen.dart index f806b086..487af658 100644 --- a/lib/features/settings/presentation/pages/physical_form_screen.dart +++ b/lib/features/settings/presentation/pages/physical_form_screen.dart @@ -2,6 +2,7 @@ import 'package:eatvio_app/features/settings/entities/settings_entity.dart'; import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/settings_state.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; import 'package:eatvio_app/core/widgets/custom_text_form_field.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -108,8 +109,8 @@ class _PhysicalFormScreenState extends State { InkWell _saveButton() { return InkWell( - onTap: () { - widget.settingsEntity.settingsCubit.updateProfileDetails( + onTap: () async { + final saved = await widget.settingsEntity.settingsCubit.updateProfileDetails( profileDetailsModel: (widget.settingsEntity.settingsCubit.state as SettingsStateLoaded).profileDetails.copyWith( chestCircumference: int.parse(chestCircumferenceController.text), waistCircumference: int.parse(waistCircumferenceController.text), @@ -118,7 +119,11 @@ class _PhysicalFormScreenState extends State { weight: double.parse(weightController.text), ), ); - GoRouter.of(context).pop(); + if (!mounted) return; + if (saved) { + showSuccessToast(S.of(context).data_saved_successfully); + GoRouter.of(context).pop(); + } }, borderRadius: BorderRadius.circular(20), child: Ink( diff --git a/lib/features/settings/presentation/pages/pressure_screen.dart b/lib/features/settings/presentation/pages/pressure_screen.dart index b50d9a42..e8855a54 100644 --- a/lib/features/settings/presentation/pages/pressure_screen.dart +++ b/lib/features/settings/presentation/pages/pressure_screen.dart @@ -4,6 +4,7 @@ import 'package:eatvio_app/features/settings/entities/settings_entity.dart'; import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/settings_state.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; import 'package:eatvio_app/core/widgets/custom_text_form_field.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -114,8 +115,8 @@ class _PressureScreenState extends State { InkWell _saveButton() { return InkWell( - onTap: () { - widget.settingsEntity.settingsCubit.updateProfileDetails( + onTap: () async { + final saved = await widget.settingsEntity.settingsCubit.updateProfileDetails( profileDetailsModel: (widget.settingsEntity.settingsCubit.state as SettingsStateLoaded).profileDetails.copyWith( bloodPressureUp: int.parse(bloodPressureUpController.text), bloodPressureDown: int.parse(bloodPressureDownController.text), @@ -123,7 +124,11 @@ class _PressureScreenState extends State { hasHypo: hypo.value, ), ); - GoRouter.of(context).pop(); + if (!mounted) return; + if (saved) { + showSuccessToast(S.of(context).data_saved_successfully); + GoRouter.of(context).pop(); + } }, borderRadius: BorderRadius.circular(20), child: Ink( diff --git a/test/features/settings/presentation/pages/general_info_screen_test.dart b/test/features/settings/presentation/pages/general_info_screen_test.dart index 2ae6f0ad..1223718e 100644 --- a/test/features/settings/presentation/pages/general_info_screen_test.dart +++ b/test/features/settings/presentation/pages/general_info_screen_test.dart @@ -119,7 +119,20 @@ void main() { supportedLocales: const [Locale('ru')], routerConfig: GoRouter( initialLocation: '/', - routes: [GoRoute(path: '/', builder: (context, state) => child)], + routes: [ + GoRoute( + path: '/', + builder: (context, state) => Scaffold( + body: Center( + child: TextButton( + onPressed: () => context.push('/screen'), + child: const Text('open screen'), + ), + ), + ), + ), + GoRoute(path: '/screen', builder: (context, state) => child), + ], ), ); } @@ -146,8 +159,8 @@ void main() { child: wrap(GeneralInfoScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 200)); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); expect(find.byType(GeneralInfoScreen), findsOneWidget); }); @@ -176,7 +189,8 @@ void main() { child: wrap(GeneralInfoScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); expect(find.text('Сохранить'), findsOneWidget); expect(find.byType(Loader), findsNothing); @@ -193,10 +207,10 @@ void main() { saveCompleter.complete(true); await tester.pump(); - await tester.pump(const Duration(milliseconds: 50)); + await tester.pumpAndSettle(); - expect(find.text('Сохранить'), findsOneWidget); - expect(find.byType(Loader), findsNothing); + expect(find.byType(GeneralInfoScreen), findsNothing); + expect(find.text('open screen'), findsOneWidget); await tester.pump(const Duration(seconds: 2)); }); @@ -224,7 +238,8 @@ void main() { child: wrap(GeneralInfoScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); await tester.tap(find.text('Сохранить')); await tester.pump(); @@ -233,6 +248,10 @@ void main() { final toastMessages = toastCalls.where((call) => call.method == 'showToast').map((call) => call.arguments['msg']); expect(toastMessages, contains('Данные успешно сохранены')); + await tester.pumpAndSettle(); + expect(find.byType(GeneralInfoScreen), findsNothing); + expect(find.text('open screen'), findsOneWidget); + await tester.pump(const Duration(seconds: 2)); }); @@ -259,13 +278,15 @@ void main() { child: wrap(GeneralInfoScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); await tester.tap(find.text('Сохранить')); await tester.pump(); await tester.pump(const Duration(milliseconds: 50)); expect(toastCalls.where((call) => call.method == 'showToast'), isEmpty); + expect(find.byType(GeneralInfoScreen), findsOneWidget); }); }); } diff --git a/test/features/settings/presentation/pages/other_info_screen_test.dart b/test/features/settings/presentation/pages/other_info_screen_test.dart index ff915a99..3d8d5f86 100644 --- a/test/features/settings/presentation/pages/other_info_screen_test.dart +++ b/test/features/settings/presentation/pages/other_info_screen_test.dart @@ -10,6 +10,7 @@ import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/se import 'package:eatvio_app/features/settings/presentation/pages/other_info_screen.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; @@ -23,6 +24,8 @@ import 'other_info_screen_test.mocks.dart'; @GenerateNiceMocks([MockSpec()]) void main() { group('OtherInfoScreen smoke', () { + final toastCalls = []; + setUpAll(() async { dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost'); }); @@ -30,6 +33,21 @@ void main() { setUp(() async { await sl.reset(); sl.registerLazySingleton(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost')); + toastCalls.clear(); + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + (MethodCall methodCall) async { + toastCalls.add(methodCall); + return null; + }, + ); + }); + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + null, + ); }); const mockProfile = ProfileModel( @@ -98,7 +116,20 @@ void main() { supportedLocales: const [Locale('ru')], routerConfig: GoRouter( initialLocation: '/', - routes: [GoRoute(path: '/', builder: (context, state) => child)], + routes: [ + GoRoute( + path: '/', + builder: (context, state) => Scaffold( + body: Center( + child: TextButton( + onPressed: () => context.push('/screen'), + child: const Text('open screen'), + ), + ), + ), + ), + GoRoute(path: '/screen', builder: (context, state) => child), + ], ), ); } @@ -125,10 +156,82 @@ void main() { child: wrap(OtherInfoScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); expect(find.byType(OtherInfoScreen), findsOneWidget); }); + + testWidgets('показывает уведомление и возвращается к настройкам после успешного сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => true); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(OtherInfoScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + final toastMessages = toastCalls.where((call) => call.method == 'showToast').map((call) => call.arguments['msg']); + expect(toastMessages, contains('Данные успешно сохранены')); + + await tester.pumpAndSettle(); + expect(find.byType(OtherInfoScreen), findsNothing); + expect(find.text('open screen'), findsOneWidget); + + await tester.pump(const Duration(seconds: 2)); + }); + + testWidgets('не показывает уведомление и остаётся на экране при ошибке сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => false); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(OtherInfoScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect(toastCalls.where((call) => call.method == 'showToast'), isEmpty); + expect(find.byType(OtherInfoScreen), findsOneWidget); + }); }); } diff --git a/test/features/settings/presentation/pages/physical_activity_screen_test.dart b/test/features/settings/presentation/pages/physical_activity_screen_test.dart index 74ca2e13..b83d6a35 100644 --- a/test/features/settings/presentation/pages/physical_activity_screen_test.dart +++ b/test/features/settings/presentation/pages/physical_activity_screen_test.dart @@ -8,9 +8,12 @@ import 'package:eatvio_app/features/settings/models/profile_settings_model.dart' import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/settings_cubit.dart'; import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/settings_state.dart'; import 'package:eatvio_app/features/settings/presentation/pages/physical_activity_screen.dart'; +import 'package:eatvio_app/generated/l10n.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:go_router/go_router.dart'; import 'package:mockito/annotations.dart'; @@ -21,6 +24,8 @@ import 'physical_activity_screen_test.mocks.dart'; @GenerateNiceMocks([MockSpec()]) void main() { group('PhysicalActivityScreen smoke', () { + final toastCalls = []; + setUpAll(() async { dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost'); }); @@ -28,6 +33,21 @@ void main() { setUp(() async { await sl.reset(); sl.registerLazySingleton(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost')); + toastCalls.clear(); + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + (MethodCall methodCall) async { + toastCalls.add(methodCall); + return null; + }, + ); + }); + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + null, + ); }); const mockProfile = ProfileModel( @@ -87,9 +107,29 @@ void main() { Widget wrap(Widget child) { return MaterialApp.router( theme: ThemeHelper().themeData(), + localizationsDelegates: const [ + S.delegate, + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: const [Locale('ru')], routerConfig: GoRouter( initialLocation: '/', - routes: [GoRoute(path: '/', builder: (context, state) => child)], + routes: [ + GoRoute( + path: '/', + builder: (context, state) => Scaffold( + body: Center( + child: TextButton( + onPressed: () => context.push('/screen'), + child: const Text('open screen'), + ), + ), + ), + ), + GoRoute(path: '/screen', builder: (context, state) => child), + ], ), ); } @@ -116,10 +156,82 @@ void main() { child: wrap(PhysicalActivityScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 200)); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); expect(find.byType(PhysicalActivityScreen), findsOneWidget); }); + + testWidgets('показывает уведомление и возвращается к настройкам после успешного сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => true); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(PhysicalActivityScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + final toastMessages = toastCalls.where((call) => call.method == 'showToast').map((call) => call.arguments['msg']); + expect(toastMessages, contains('Данные успешно сохранены')); + + await tester.pumpAndSettle(); + expect(find.byType(PhysicalActivityScreen), findsNothing); + expect(find.text('open screen'), findsOneWidget); + + await tester.pump(const Duration(seconds: 2)); + }); + + testWidgets('не показывает уведомление и остаётся на экране при ошибке сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => false); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(PhysicalActivityScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect(toastCalls.where((call) => call.method == 'showToast'), isEmpty); + expect(find.byType(PhysicalActivityScreen), findsOneWidget); + }); }); } diff --git a/test/features/settings/presentation/pages/physical_form_screen_test.dart b/test/features/settings/presentation/pages/physical_form_screen_test.dart index 903a3390..b170d657 100644 --- a/test/features/settings/presentation/pages/physical_form_screen_test.dart +++ b/test/features/settings/presentation/pages/physical_form_screen_test.dart @@ -10,6 +10,7 @@ import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/se import 'package:eatvio_app/features/settings/presentation/pages/physical_form_screen.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; @@ -23,6 +24,8 @@ import 'physical_form_screen_test.mocks.dart'; @GenerateNiceMocks([MockSpec()]) void main() { group('PhysicalFormScreen smoke', () { + final toastCalls = []; + setUpAll(() async { dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost'); }); @@ -30,6 +33,21 @@ void main() { setUp(() async { await sl.reset(); sl.registerLazySingleton(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost')); + toastCalls.clear(); + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + (MethodCall methodCall) async { + toastCalls.add(methodCall); + return null; + }, + ); + }); + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + null, + ); }); const mockProfile = ProfileModel( @@ -98,7 +116,20 @@ void main() { supportedLocales: const [Locale('ru')], routerConfig: GoRouter( initialLocation: '/', - routes: [GoRoute(path: '/', builder: (context, state) => child)], + routes: [ + GoRoute( + path: '/', + builder: (context, state) => Scaffold( + body: Center( + child: TextButton( + onPressed: () => context.push('/screen'), + child: const Text('open screen'), + ), + ), + ), + ), + GoRoute(path: '/screen', builder: (context, state) => child), + ], ), ); } @@ -125,10 +156,82 @@ void main() { child: wrap(PhysicalFormScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); expect(find.byType(PhysicalFormScreen), findsOneWidget); }); + + testWidgets('показывает уведомление и возвращается к настройкам после успешного сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => true); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(PhysicalFormScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + final toastMessages = toastCalls.where((call) => call.method == 'showToast').map((call) => call.arguments['msg']); + expect(toastMessages, contains('Данные успешно сохранены')); + + await tester.pumpAndSettle(); + expect(find.byType(PhysicalFormScreen), findsNothing); + expect(find.text('open screen'), findsOneWidget); + + await tester.pump(const Duration(seconds: 2)); + }); + + testWidgets('не показывает уведомление и остаётся на экране при ошибке сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => false); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(PhysicalFormScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect(toastCalls.where((call) => call.method == 'showToast'), isEmpty); + expect(find.byType(PhysicalFormScreen), findsOneWidget); + }); }); } diff --git a/test/features/settings/presentation/pages/pressure_screen_test.dart b/test/features/settings/presentation/pages/pressure_screen_test.dart index 6e399b5c..69b44bb5 100644 --- a/test/features/settings/presentation/pages/pressure_screen_test.dart +++ b/test/features/settings/presentation/pages/pressure_screen_test.dart @@ -10,6 +10,7 @@ import 'package:eatvio_app/features/settings/presentation/bloc/settings_cubit/se import 'package:eatvio_app/features/settings/presentation/pages/pressure_screen.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; @@ -23,6 +24,8 @@ import 'pressure_screen_test.mocks.dart'; @GenerateNiceMocks([MockSpec()]) void main() { group('PressureScreen smoke', () { + final toastCalls = []; + setUpAll(() async { dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost'); }); @@ -30,6 +33,21 @@ void main() { setUp(() async { await sl.reset(); sl.registerLazySingleton(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost')); + toastCalls.clear(); + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + (MethodCall methodCall) async { + toastCalls.add(methodCall); + return null; + }, + ); + }); + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('PonnamKarthik/fluttertoast'), + null, + ); }); const mockProfile = ProfileModel( @@ -98,7 +116,20 @@ void main() { supportedLocales: const [Locale('ru')], routerConfig: GoRouter( initialLocation: '/', - routes: [GoRoute(path: '/', builder: (context, state) => child)], + routes: [ + GoRoute( + path: '/', + builder: (context, state) => Scaffold( + body: Center( + child: TextButton( + onPressed: () => context.push('/screen'), + child: const Text('open screen'), + ), + ), + ), + ), + GoRoute(path: '/screen', builder: (context, state) => child), + ], ), ); } @@ -125,10 +156,82 @@ void main() { child: wrap(PressureScreen(settingsEntity: settingsEntity)), ), ); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); expect(find.byType(PressureScreen), findsOneWidget); }); + + testWidgets('показывает уведомление и возвращается к настройкам после успешного сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => true); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(PressureScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + final toastMessages = toastCalls.where((call) => call.method == 'showToast').map((call) => call.arguments['msg']); + expect(toastMessages, contains('Данные успешно сохранены')); + + await tester.pumpAndSettle(); + expect(find.byType(PressureScreen), findsNothing); + expect(find.text('open screen'), findsOneWidget); + + await tester.pump(const Duration(seconds: 2)); + }); + + testWidgets('не показывает уведомление и остаётся на экране при ошибке сохранения', (tester) async { + final cubit = MockSettingsCubit(); + const state = SettingsState.loaded( + profileSettings: mockProfileSettings, + profileDetails: mockProfileDetails, + profile: mockProfile, + hasActiveConsultation: false, + ); + + when(cubit.state).thenReturn(state); + when(cubit.stream).thenAnswer((_) => Stream.value(state)); + when(cubit.close()).thenAnswer((_) async => Future.value()); + when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => false); + + final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit); + + await tester.pumpWidget( + BlocProvider.value( + value: cubit, + child: wrap(PressureScreen(settingsEntity: settingsEntity)), + ), + ); + await tester.tap(find.text('open screen')); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Сохранить')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 50)); + + expect(toastCalls.where((call) => call.method == 'showToast'), isEmpty); + expect(find.byType(PressureScreen), findsOneWidget); + }); }); }