feat(tests): increased tests coverage

This commit is contained in:
misha
2025-12-25 12:56:38 +03:00
parent 5b705dfb72
commit 99659f7ea1
132 changed files with 10304 additions and 6226 deletions

View File

@@ -0,0 +1,325 @@
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/consultations/models/consultation_model.dart';
import 'package:eatvio_app/features/plans/enums/chosen_plan_state_enum.dart';
import 'package:eatvio_app/features/personal_diet_suggestion/cubit/personal_diet_suggestion_cubit.dart';
import 'package:eatvio_app/features/personal_diet_suggestion/cubit/personal_diet_suggestion_state.dart';
import 'package:eatvio_app/features/personal_diet_suggestion/presentation/consultation_widget.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.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';
import 'package:mockito/mockito.dart';
import 'package:network_image_mock/network_image_mock.dart';
import 'consultation_widget_test.mocks.dart';
@GenerateNiceMocks([MockSpec<PersonalDietSuggestionCubit>()])
void main() {
group('ConsultationWidget 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'));
});
final mockConsultation = ConsultationModel(
id: 1,
chosenPlanState: ChosenPlanStateEnum.stateInProgress,
planTitle: 'Test Plan',
userName: 'Test User',
userSlug: 'test-user',
consultantPayment: 1000.0,
duration: 7,
currentDiaryDay: 3,
userProfilePhotoUrl: '/avatar.jpg',
consultationOptionPrice: 500,
startDate: DateTime(2025, 1, 1),
endDate: DateTime(2025, 1, 8),
consultantId: 1,
diaries: [],
personalDietId: null,
);
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),
GoRoute(
path: '/personal-diet-suggestion',
builder: (context, state) => const Scaffold(body: Text('Personal Diet')),
),
],
),
);
}
testWidgets('renders consultation widget without personal diet', (tester) async {
mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: mockConsultation, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
expect(find.byType(ConsultationWidget), findsOneWidget);
});
});
testWidgets('renders consultation widget with personal diet', (tester) async {
mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
final consultationWithDiet = ConsultationModel(
id: 1,
chosenPlanState: ChosenPlanStateEnum.stateInProgress,
planTitle: 'Test Plan',
userName: 'Test User',
userSlug: 'test-user',
consultantPayment: 1000.0,
duration: 7,
currentDiaryDay: 3,
userProfilePhotoUrl: '/avatar.jpg',
consultationOptionPrice: 500,
startDate: DateTime(2025, 1, 1),
endDate: DateTime(2025, 1, 8),
consultantId: 1,
diaries: [],
personalDietId: 1,
);
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: consultationWithDiet, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
expect(find.byType(ConsultationWidget), findsOneWidget);
});
});
testWidgets('taps suggest diet button and shows bottom sheet', (tester) async {
await mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: mockConsultation, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
final suggestButton = find.text(S.of(tester.element(find.byType(ConsultationWidget))).suggestDiet);
expect(suggestButton, findsOneWidget);
await tester.tap(suggestButton);
await tester.pumpAndSettle();
expect(find.text('Предложение диеты'), findsOneWidget);
expect(find.text('Предложить пользователю Test User «Test Diet»'), findsOneWidget);
});
});
testWidgets('taps change diet button and shows bottom sheet', (tester) async {
await mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
final consultationWithDiet = ConsultationModel(
id: 1,
chosenPlanState: ChosenPlanStateEnum.stateInProgress,
planTitle: 'Test Plan',
userName: 'Test User',
userSlug: 'test-user',
consultantPayment: 1000.0,
duration: 7,
currentDiaryDay: 3,
userProfilePhotoUrl: '/avatar.jpg',
consultationOptionPrice: 500,
startDate: DateTime(2025, 1, 1),
endDate: DateTime(2025, 1, 8),
consultantId: 1,
diaries: [],
personalDietId: 1,
);
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: consultationWithDiet, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
final changeButton = find.text(S.of(tester.element(find.byType(ConsultationWidget))).changeDiet);
expect(changeButton, findsOneWidget);
await tester.tap(changeButton);
await tester.pumpAndSettle();
expect(find.text('Предложение диеты'), findsOneWidget);
expect(find.text('Предложить пользователю Test User «Test Diet»'), findsOneWidget);
});
});
testWidgets('shows success bottom sheet after successful diet attachment', (tester) async {
await mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(cubit.attachDietToConsultation(consultationId: 1)).thenAnswer((_) async => true);
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: mockConsultation, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
final suggestButton = find.text('Предложить диету');
await tester.tap(suggestButton);
await tester.pumpAndSettle();
final offerButton = find.text(S.of(tester.element(find.byType(ConsultationWidget))).toOffer);
await tester.tap(offerButton);
await tester.pumpAndSettle();
expect(find.text('«Test Diet» предложена пользователю Test User'), findsOneWidget);
verify(cubit.attachDietToConsultation(consultationId: 1)).called(1);
});
});
testWidgets('shows failure bottom sheet after failed diet attachment', (tester) async {
await mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(cubit.attachDietToConsultation(consultationId: 1)).thenAnswer((_) async => false);
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: mockConsultation, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
final suggestButton = find.text('Предложить диету');
await tester.tap(suggestButton);
await tester.pumpAndSettle();
final offerButton = find.text(S.of(tester.element(find.byType(ConsultationWidget))).toOffer);
await tester.tap(offerButton);
await tester.pumpAndSettle();
expect(find.text('Не удалось предложить диету Test Diet пользователю Test User'), findsOneWidget);
verify(cubit.attachDietToConsultation(consultationId: 1)).called(1);
});
});
testWidgets('displays user name and plan title correctly', (tester) async {
await mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: mockConsultation, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
expect(find.text('Test User'), findsOneWidget);
expect(find.text('Test Plan'), findsOneWidget);
expect(find.text('Старт: Test User'), findsOneWidget);
});
});
testWidgets('displays user name and plan title without start text when personal diet exists', (tester) async {
await mockNetworkImagesFor(() async {
final cubit = MockPersonalDietSuggestionCubit();
final consultationWithDiet = ConsultationModel(
id: 1,
chosenPlanState: ChosenPlanStateEnum.stateInProgress,
planTitle: 'Test Plan',
userName: 'Test User',
userSlug: 'test-user',
consultantPayment: 1000.0,
duration: 7,
currentDiaryDay: 3,
userProfilePhotoUrl: '/avatar.jpg',
consultationOptionPrice: 500,
startDate: DateTime(2025, 1, 1),
endDate: DateTime(2025, 1, 8),
consultantId: 1,
diaries: [],
personalDietId: 1,
);
when(cubit.state).thenReturn(PersonalDietSuggestionState());
when(cubit.stream).thenAnswer((_) => const Stream.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
await tester.pumpWidget(
BlocProvider<PersonalDietSuggestionCubit>.value(
value: cubit,
child: wrap(ConsultationWidget(consultationModel: consultationWithDiet, dietName: 'Test Diet')),
),
);
await tester.pumpAndSettle();
expect(find.text('Test User'), findsOneWidget);
expect(find.text('Test Plan'), findsOneWidget);
expect(find.text('Старт: Test User'), findsNothing);
});
});
});
}