feat(tests): test coverage 60%

This commit is contained in:
misha
2025-12-23 10:59:56 +03:00
parent 38118055a5
commit 5b705dfb72
67 changed files with 8391 additions and 42 deletions

View File

@@ -0,0 +1,199 @@
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/general_info_screen.dart';
import 'package:eatvio_app/features/settings/presentation/pages/other_info_screen.dart';
import 'package:eatvio_app/features/settings/presentation/pages/physical_activity_screen.dart';
import 'package:eatvio_app/features/settings/presentation/pages/physical_form_screen.dart';
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_dotenv/flutter_dotenv.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'settings_additional_info_screens_smoke_test.mocks.dart';
@GenerateMocks([SettingsCubit])
void main() {
group('Settings additional info screens smoke', () {
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'));
});
SettingsEntity entity(MockSettingsCubit cubit) {
return SettingsEntity(
profileSettingsModel: const ProfileSettingsModel(
newUpdatesNotifications: true,
diaryReminderNotifications: true,
isMoodFillable: true,
isBedtimeFillable: true,
isPublicProfile: true,
),
profileDetailsModel: const ProfileDetailsModel(
firstName: 'John',
lastName: 'Doe',
birthdate: '1990-01-01',
gender: 'male',
country: 'Russia',
city: 'Moscow',
lang: 'ru',
weight: 75.0,
height: 180,
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: false,
hasHyper: false,
aboutAlergy: 'None',
aboutTakingPils: 'None',
aboutSportHobbies: 'Running',
favoriteFoods: 'Pizza',
unfavoriteFoods: 'Broccoli',
additionalInfo: 'No additional info',
isMoodFillable: true,
isBedtimeFillable: true,
activityCoefficient: 1.5,
),
settingsCubit: cubit,
);
}
void stubCubit(MockSettingsCubit cubit) {
const profile = ProfileModel(
id: 1,
name: 'Test User',
email: 'test@example.com',
phoneConfirmationState: 'confirmed',
slug: 'test-user',
isPublicProfile: true,
isBlockedByMe: false,
isBlockedByAuthor: false,
isBlockedByUser: false,
isEntitySubscribedByMe: false,
profilePhotoUrl: '',
roles: [],
badges: [],
subscriptions: [],
subscribers: [],
);
const state = SettingsState.loaded(
profileSettings: ProfileSettingsModel(
newUpdatesNotifications: true,
diaryReminderNotifications: true,
isMoodFillable: true,
isBedtimeFillable: true,
isPublicProfile: true,
),
profileDetails: ProfileDetailsModel(
firstName: 'John',
lastName: 'Doe',
birthdate: '1990-01-01',
gender: 'male',
country: 'Russia',
city: 'Moscow',
lang: 'ru',
weight: 75.0,
height: 180,
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: false,
hasHyper: false,
aboutAlergy: 'None',
aboutTakingPils: 'None',
aboutSportHobbies: 'Running',
favoriteFoods: 'Pizza',
unfavoriteFoods: 'Broccoli',
additionalInfo: 'No additional info',
isMoodFillable: true,
isBedtimeFillable: true,
activityCoefficient: 1.5,
),
profile: profile,
hasActiveConsultation: false,
);
when(cubit.state).thenReturn(state);
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async {});
}
Widget wrap(Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: Scaffold(body: child),
);
}
testWidgets('GeneralInfoScreen renders', (tester) async {
final mockCubit = MockSettingsCubit();
stubCubit(mockCubit);
await tester.pumpWidget(wrap(GeneralInfoScreen(settingsEntity: entity(mockCubit))));
await tester.pump();
expect(find.byType(GeneralInfoScreen), findsOneWidget);
expect(find.text('Общая информация'), findsOneWidget);
});
testWidgets('PhysicalFormScreen renders', (tester) async {
final mockCubit = MockSettingsCubit();
stubCubit(mockCubit);
await tester.pumpWidget(wrap(PhysicalFormScreen(settingsEntity: entity(mockCubit))));
await tester.pump();
expect(find.byType(PhysicalFormScreen), findsOneWidget);
expect(find.text('Физическая форма'), findsOneWidget);
});
testWidgets('PressureScreen renders', (tester) async {
final mockCubit = MockSettingsCubit();
stubCubit(mockCubit);
await tester.pumpWidget(wrap(PressureScreen(settingsEntity: entity(mockCubit))));
await tester.pump();
expect(find.byType(PressureScreen), findsOneWidget);
expect(find.text('Давление'), findsOneWidget);
});
testWidgets('OtherInfoScreen renders', (tester) async {
final mockCubit = MockSettingsCubit();
stubCubit(mockCubit);
await tester.pumpWidget(wrap(OtherInfoScreen(settingsEntity: entity(mockCubit))));
await tester.pump();
expect(find.byType(OtherInfoScreen), findsOneWidget);
expect(find.text('Прочая информаиця'), findsOneWidget);
});
testWidgets('PhysicalActivityScreen renders', (tester) async {
final mockCubit = MockSettingsCubit();
stubCubit(mockCubit);
await tester.pumpWidget(wrap(PhysicalActivityScreen(settingsEntity: entity(mockCubit))));
await tester.pump();
expect(find.byType(PhysicalActivityScreen), findsOneWidget);
expect(find.text('Физическая нагрузка'), findsOneWidget);
});
});
}

View File

@@ -0,0 +1,149 @@
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/consultant/become_consultant/repositories/consultant_repository.dart';
import 'package:eatvio_app/features/consultations/repositories/consultations_repository.dart';
import 'package:eatvio_app/features/profile/models/profile_model.dart';
import 'package:eatvio_app/features/profile/repositories/profile_repository.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/pages/settings_screen.dart';
import 'package:eatvio_app/features/settings/repositories/service_repository.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'settings_screen_smoke_test.mocks.dart';
@GenerateNiceMocks([
MockSpec<ProfileRepository>(),
MockSpec<SettingsRepository>(),
MockSpec<ConsultantRepository>(),
MockSpec<ConsultationsRepository>(),
])
void main() {
group('SettingsScreen smoke', () {
late MockProfileRepository profileRepository;
late MockSettingsRepository settingsRepository;
late MockConsultantRepository consultantRepository;
late MockConsultationsRepository consultationsRepository;
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'));
profileRepository = MockProfileRepository();
settingsRepository = MockSettingsRepository();
consultantRepository = MockConsultantRepository();
consultationsRepository = MockConsultationsRepository();
sl.registerLazySingleton<ProfileRepository>(() => profileRepository);
sl.registerLazySingleton<SettingsRepository>(() => settingsRepository);
sl.registerLazySingleton<ConsultantRepository>(() => consultantRepository);
sl.registerLazySingleton<ConsultationsRepository>(() => consultationsRepository);
sl.registerFactory<SettingsCubit>(
() => SettingsCubit(
profileRepository: sl(),
settingsRepository: sl(),
consultantRepository: sl(),
consultationsRepository: sl(),
),
);
});
Widget wrap(Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: child,
);
}
testWidgets('renders loaded settings', (tester) async {
const profile = ProfileModel(
id: 1,
name: 'Test User',
email: 'test@example.com',
phoneConfirmationState: 'confirmed',
slug: 'test-user',
isPublicProfile: true,
isBlockedByMe: false,
isBlockedByAuthor: false,
isBlockedByUser: false,
isEntitySubscribedByMe: false,
profilePhotoUrl: '',
roles: [],
badges: [],
subscriptions: [],
subscribers: [],
);
const profileDetails = ProfileDetailsModel(
firstName: 'Test',
lastName: 'User',
birthdate: '1990-01-01',
gender: 'male',
country: 'Russia',
city: 'Moscow',
lang: 'ru',
weight: 70.0,
height: 175,
chestCircumference: 100,
waistCircumference: 80,
hipCircumference: 90,
bloodPressureUp: 120,
bloodPressureDown: 80,
hasHypo: false,
hasHyper: false,
aboutAlergy: '',
aboutTakingPils: '',
aboutSportHobbies: '',
favoriteFoods: '',
unfavoriteFoods: '',
additionalInfo: '',
isMoodFillable: true,
isBedtimeFillable: true,
activityCoefficient: 1.5,
);
const profileSettings = ProfileSettingsModel(
newUpdatesNotifications: true,
diaryReminderNotifications: true,
isMoodFillable: true,
isBedtimeFillable: true,
isPublicProfile: true,
);
when(profileRepository.getMyProfile()).thenAnswer((_) async => profile);
when(profileRepository.getMyProfileDetails()).thenAnswer((_) async => profileDetails);
when(settingsRepository.getProfileSettings()).thenAnswer((_) async => profileSettings);
when(consultantRepository.getMyRequestStatus(any)).thenThrow(Exception('skip consultant status in tests'));
when(settingsRepository.getOAuthAccounts()).thenThrow(Exception('skip oauth in tests'));
when(consultationsRepository.getUserPurchases(any, any)).thenThrow(Exception('skip purchases in tests'));
await tester.pumpWidget(wrap(const SettingsScreen()));
await tester.pumpAndSettle();
expect(find.text('Настройки'), findsWidgets);
expect(find.byType(SettingsScreen), findsOneWidget);
});
});
}