Files
eatvio_app/test/features/settings/presentation/pages/pressure_screen_test.dart
alex-chevski 23f24c0be0 Merge remote-tracking branch 'origin/main' into fix/989-profile-save-alert
# Conflicts:
#	lib/features/settings/presentation/pages/general_info_screen.dart
#	lib/features/settings/presentation/pages/physical_activity_screen.dart
#	lib/generated/intl/messages_en.dart
#	lib/generated/intl/messages_ru.dart
#	test/features/settings/presentation/pages/general_info_screen_test.dart
#	test/features/settings/presentation/pages/other_info_screen_test.dart
#	test/features/settings/presentation/pages/physical_activity_screen_test.dart
#	test/features/settings/presentation/pages/physical_form_screen_test.dart
#	test/features/settings/presentation/pages/pressure_screen_test.dart
2026-11-19 14:19:40 +03:00

232 lines
8.4 KiB
Dart

import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/profile/models/profile_model.dart';
import 'package:eatvio_app/features/settings/entities/settings_entity.dart';
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/pressure_screen.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'pressure_screen_test.mocks.dart';
@GenerateNiceMocks([MockSpec<SettingsCubit>()])
void main() {
group('PressureScreen smoke', () {
final toastCalls = <MethodCall>[];
setUpAll(() async {
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
});
setUp(() async {
await sl.reset();
sl.registerLazySingleton<AppConfig>(() => 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(
id: 1,
name: 'Test User',
email: 'test@example.com',
phoneConfirmationState: 'confirmed',
isPublicProfile: true,
isBlockedByMe: false,
isBlockedByAuthor: false,
isBlockedByUser: false,
isEntitySubscribedByMe: false,
slug: 'test-user',
profilePhotoUrl: '/test.jpg',
roles: ['user'],
badges: [],
subscriptions: [],
subscribers: [],
);
const mockProfileDetails = ProfileDetailsModel(
firstName: 'Test',
lastName: 'User',
birthdate: '1990-01-01',
gender: 'male',
country: 'Russia',
city: 'Moscow',
lang: 'ru',
weight: 70.0,
height: 175,
chestCircumference: 90,
waistCircumference: 80,
hipCircumference: 95,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: false,
hasHyper: false,
aboutAlergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
isMoodFillable: true,
isBedtimeFillable: true,
activityCoefficient: 1.2,
);
const mockProfileSettings = ProfileSettingsModel(
newUpdatesNotifications: 1,
diaryReminderNotifications: 1,
isMoodFillable: true,
isBedtimeFillable: true,
isPublicProfile: true,
);
Widget wrap(Widget child) {
return MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [S.delegate, ...GlobalMaterialLocalizations.delegates],
supportedLocales: const [Locale('ru')],
routerConfig: GoRouter(
initialLocation: '/',
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),
],
),
);
}
testWidgets('renders pressure screen', (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<void>.value());
when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => true);
final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit);
await tester.pumpWidget(
BlocProvider<SettingsCubit>.value(
value: cubit,
child: wrap(PressureScreen(settingsEntity: settingsEntity)),
),
);
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<void>.value());
when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => true);
final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit);
await tester.pumpWidget(
BlocProvider<SettingsCubit>.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<void>.value());
when(cubit.updateProfileDetails(profileDetailsModel: anyNamed('profileDetailsModel'))).thenAnswer((_) async => false);
final settingsEntity = SettingsEntity(profileSettingsModel: mockProfileSettings, profileDetailsModel: mockProfileDetails, settingsCubit: cubit);
await tester.pumpWidget(
BlocProvider<SettingsCubit>.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);
});
});
}