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,139 @@
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/feed/presentation/bloc/consultants_cubit/consultants_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/diets_cubit/diets_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_state.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/consultants_cubit/consultants_state.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/diets_cubit/diets_state.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_state.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/profile_cubit/profile_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/profile_cubit/profile_state.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/recipes_cubit/recipes_cubit.dart';
import 'package:eatvio_app/core/cubit/marketing/marketing_cubit.dart';
import 'package:eatvio_app/core/views/cubit/views_cubit.dart';
import 'package:eatvio_app/core/views/repository/views_repository.dart';
import 'package:eatvio_app/core/views/service/views_service.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/recipes_cubit/recipes_state.dart';
import 'package:eatvio_app/features/feed/presentation/pages/feed_screen.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:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'feed_screen_smoke_test.mocks.dart';
@GenerateNiceMocks([
MockSpec<RecipesCubit>(),
MockSpec<FeedProfileCubit>(),
MockSpec<DietsCubit>(),
MockSpec<PlansCubit>(),
MockSpec<ConsultantsCubit>(),
MockSpec<FeedCubit>(),
MockSpec<ViewsService>(),
MockSpec<MarketingCubit>(),
])
void main() {
group('FeedScreen 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'));
});
Widget wrap(FeedCubit feedCubit, MarketingCubit marketingCubit, Widget child) {
final viewsService = MockViewsService();
final viewsRepository = ViewsRepository(viewsService: viewsService);
final viewsCubit = ViewsCubit(viewsRepository: viewsRepository);
return MultiBlocProvider(
providers: [
BlocProvider<ViewsCubit>.value(value: viewsCubit),
BlocProvider<MarketingCubit>.value(value: marketingCubit),
BlocProvider<FeedCubit>.value(value: feedCubit),
],
child: MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: child,
),
);
}
testWidgets('renders FeedScreen with all tabs', (tester) async {
final recipesCubit = MockRecipesCubit();
final profileCubit = MockFeedProfileCubit();
final dietsCubit = MockDietsCubit();
final plansCubit = MockPlansCubit();
final consultantsCubit = MockConsultantsCubit();
final feedCubit = MockFeedCubit();
final viewsService = MockViewsService();
final viewsRepository = ViewsRepository(viewsService: viewsService);
final viewsCubit = ViewsCubit(viewsRepository: viewsRepository);
final marketingCubit = MockMarketingCubit();
when(recipesCubit.state).thenReturn(RecipesState.initial());
when(recipesCubit.stream).thenAnswer((_) => const Stream.empty());
when(recipesCubit.close()).thenAnswer((_) async => Future<void>.value());
when(profileCubit.state).thenReturn(ProfileState(profile: null));
when(profileCubit.stream).thenAnswer((_) => const Stream.empty());
when(profileCubit.close()).thenAnswer((_) async => Future<void>.value());
when(dietsCubit.state).thenReturn(DietsState.initial());
when(dietsCubit.stream).thenAnswer((_) => const Stream.empty());
when(dietsCubit.close()).thenAnswer((_) async => Future<void>.value());
when(plansCubit.state).thenReturn(PlansState.initial());
when(plansCubit.stream).thenAnswer((_) => const Stream.empty());
when(plansCubit.close()).thenAnswer((_) async => Future<void>.value());
when(consultantsCubit.state).thenReturn(const ConsultantsState());
when(consultantsCubit.stream).thenAnswer((_) => const Stream.empty());
when(consultantsCubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.state).thenReturn(const FeedState.loading());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.loadFeedBlogPostList()).thenAnswer((_) async => Future<void>.value());
when(marketingCubit.checkRegistrationDate()).thenAnswer((_) async => false);
when(marketingCubit.stream).thenAnswer((_) => const Stream.empty());
when(marketingCubit.close()).thenAnswer((_) async => Future<void>.value());
sl.registerLazySingleton<RecipesCubit>(() => recipesCubit);
sl.registerLazySingleton<FeedProfileCubit>(() => profileCubit);
sl.registerLazySingleton<DietsCubit>(() => dietsCubit);
sl.registerLazySingleton<PlansCubit>(() => plansCubit);
sl.registerLazySingleton<ConsultantsCubit>(() => consultantsCubit);
sl.registerLazySingleton<FeedCubit>(() => feedCubit);
if (!sl.isRegistered<ViewsRepository>()) {
sl.registerLazySingleton<ViewsRepository>(() => viewsRepository);
} else {
sl.unregister<ViewsRepository>();
sl.registerLazySingleton<ViewsRepository>(() => viewsRepository);
}
if (!sl.isRegistered<ViewsCubit>()) {
sl.registerLazySingleton<ViewsCubit>(() => viewsCubit);
} else {
sl.unregister<ViewsCubit>();
sl.registerLazySingleton<ViewsCubit>(() => viewsCubit);
}
await tester.pumpWidget(wrap(feedCubit, marketingCubit, const FeedScreen()));
await tester.pump();
expect(find.byType(FeedScreen), findsOneWidget);
});
});
}

View File

@@ -0,0 +1,57 @@
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/plans_cubit/plans_state.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/price_choice_filter_widget.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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 'price_choice_filter_widget_test.mocks.dart';
@GenerateNiceMocks([MockSpec<PlansCubit>()])
void main() {
group('PriceChoiceFilterWidget', () {
Widget wrap(PlansCubit cubit, Widget child) {
return MaterialApp(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
home: BlocProvider<PlansCubit>.value(
value: cubit,
child: Scaffold(body: child),
),
);
}
testWidgets('renders with PlansCubit', (tester) async {
final cubit = MockPlansCubit();
const state = PlansState(
isLoading: false,
plans: [],
errorMessage: null,
filterState: PlansFilterState(priceFilter: (null, null), durationIndex: 0, typeIndex: 0),
minPlanPrice: 100,
maxPlanPrice: 1000,
);
when(cubit.state).thenReturn(state);
when(cubit.stream).thenAnswer((_) => const Stream<PlansState>.empty());
when(cubit.close()).thenAnswer((_) async => Future<void>.value());
when(cubit.changePriceFilter(any, any, disableDebounce: anyNamed('disableDebounce'))).thenReturn(null);
await tester.pumpWidget(wrap(cubit, PriceChoiceFilterWidget(plansCubit: cubit)));
await tester.pump();
expect(find.byType(PriceChoiceFilterWidget), findsOneWidget);
});
});
}