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,197 @@
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/comments/comments_cubit/comments_cubit.dart';
import 'package:eatvio_app/core/comments/comments_cubit/comments_state.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/recipes_model.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_cubit.dart';
import 'package:eatvio_app/features/recipe/presentation/cubit/about_recipe_cubit.dart';
import 'package:eatvio_app/features/recipe/presentation/cubit/about_recipe_state.dart';
import 'package:eatvio_app/features/recipe/presentation/pages/about_recipe_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: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 'about_recipe_screen_smoke_test.mocks.dart';
@GenerateNiceMocks([MockSpec<AboutRecipeCubit>(), MockSpec<FeedCubit>(), MockSpec<CommentsCubit>()])
void main() {
group('AboutRecipeScreen 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'));
FlutterError.onError = (FlutterErrorDetails details) {
if (details.exception is FlutterError && details.exception.toString().contains('overflowed')) {
return;
}
FlutterError.presentError(details);
};
});
void registerMocks(AboutRecipeCubit recipeCubit, CommentsCubit commentsCubit) {
sl.registerLazySingleton<AboutRecipeCubit>(() => recipeCubit);
sl.registerLazySingleton<CommentsCubit>(() => commentsCubit);
}
final testRecipe = Recipe(
id: 1,
slug: 'test-recipe',
title: 'Test Recipe',
image: 'https://example.com/image.jpg',
description: 'Test description',
state: 'active',
cookingTime: 30,
activeCookingTime: 20,
ingredients: const [],
steps: const [],
videoUrl: null,
createdAt: DateTime(2025, 1, 1),
updatedAt: DateTime(2025, 1, 1),
type: 'main',
commentsCount: 5,
wasLikedByMe: false,
isFavorite: false,
calories: 200.0,
servings: 4,
likesCount: 10,
viewsCount: 100,
tags: const [],
author: const Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
);
Widget wrap(AboutRecipeCubit recipeCubit, FeedCubit feedCubit, CommentsCubit commentsCubit, Widget child) {
final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => MediaQuery(
data: const MediaQueryData(size: Size(2000, 1200)),
child: MultiBlocProvider(
providers: [
BlocProvider<AboutRecipeCubit>.value(value: recipeCubit),
BlocProvider<FeedCubit>.value(value: feedCubit),
BlocProvider<CommentsCubit>.value(value: commentsCubit),
],
child: child,
),
),
),
GoRoute(path: '/feed-screen', builder: (_, _) => const SizedBox.shrink()),
],
);
return MaterialApp.router(
theme: ThemeHelper().themeData(),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [Locale('ru')],
routerConfig: router,
);
}
testWidgets('renders loading state', (tester) async {
final recipeCubit = MockAboutRecipeCubit();
final feedCubit = MockFeedCubit();
final commentsCubit = MockCommentsCubit();
const state = AboutRecipeState.loading();
when(recipeCubit.state).thenReturn(state);
when(recipeCubit.stream).thenAnswer((_) => const Stream<AboutRecipeState>.empty());
when(recipeCubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.viewRecipe(any)).thenAnswer((_) async => Future<void>.value());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.state).thenReturn(const CommentsState());
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
registerMocks(recipeCubit, commentsCubit);
await tester.pumpWidget(wrap(recipeCubit, feedCubit, commentsCubit, const AboutRecipeScreen(recipeSlug: 'test', index: null, navigateToComments: false)));
await tester.pump();
expect(find.byType(AboutRecipeScreen), findsOneWidget);
});
testWidgets('renders loaded state with recipe', (tester) async {
final recipeCubit = MockAboutRecipeCubit();
final feedCubit = MockFeedCubit();
final commentsCubit = MockCommentsCubit();
final state = AboutRecipeState.loaded(recipe: testRecipe);
when(recipeCubit.state).thenReturn(state);
when(recipeCubit.stream).thenAnswer((_) => const Stream<AboutRecipeState>.empty());
when(recipeCubit.close()).thenAnswer((_) async => Future<void>.value());
when(recipeCubit.getRecipe(any, index: anyNamed('index'))).thenAnswer((_) async => Future<void>.value());
when(feedCubit.viewRecipe(any)).thenAnswer((_) async => Future<void>.value());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.state).thenReturn(const CommentsState());
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.getComments(any, any, any)).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.setReplyId(any)).thenReturn(null);
registerMocks(recipeCubit, commentsCubit);
await mockNetworkImagesFor(() async {
await tester.pumpWidget(
wrap(recipeCubit, feedCubit, commentsCubit, const AboutRecipeScreen(recipeSlug: 'test', index: null, navigateToComments: false)),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 600));
});
expect(find.byType(AboutRecipeScreen), findsOneWidget);
});
testWidgets('renders error state', (tester) async {
final recipeCubit = MockAboutRecipeCubit();
final feedCubit = MockFeedCubit();
final commentsCubit = MockCommentsCubit();
const state = AboutRecipeState.error(message: 'Error loading recipe');
when(recipeCubit.state).thenReturn(state);
when(recipeCubit.stream).thenAnswer((_) => const Stream<AboutRecipeState>.empty());
when(recipeCubit.close()).thenAnswer((_) async => Future<void>.value());
when(feedCubit.viewRecipe(any)).thenAnswer((_) async => Future<void>.value());
when(feedCubit.stream).thenAnswer((_) => const Stream.empty());
when(feedCubit.close()).thenAnswer((_) async => Future<void>.value());
when(commentsCubit.state).thenReturn(const CommentsState());
when(commentsCubit.stream).thenAnswer((_) => const Stream<CommentsState>.empty());
when(commentsCubit.close()).thenAnswer((_) async => Future<void>.value());
registerMocks(recipeCubit, commentsCubit);
await tester.pumpWidget(wrap(recipeCubit, feedCubit, commentsCubit, const AboutRecipeScreen(recipeSlug: 'test', index: null, navigateToComments: false)));
await tester.pump();
expect(find.byType(AboutRecipeScreen), findsOneWidget);
});
});
}

View File

@@ -0,0 +1,112 @@
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/recipes_cubit/recipes_cubit.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/recipes_cubit/recipes_state.dart';
import 'package:eatvio_app/features/recipe/models/my_recipe_model.dart';
import 'package:eatvio_app/features/recipe/presentation/cubit/my_recipes_cubit/my_recieps_cubit.dart';
import 'package:eatvio_app/features/recipe/presentation/cubit/my_recipes_cubit/my_recipes_state.dart';
import 'package:eatvio_app/features/recipe/presentation/widgets/my_recipe_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 'my_recipe_widget_test.mocks.dart';
@GenerateNiceMocks([MockSpec<RecipesCubit>(), MockSpec<MyRecipesCubit>()])
void main() {
group('MyRecipeWidget 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'));
});
const mockRecipe = MyRecipeModel(
id: 1,
image: 'https://example.com/recipe.jpg',
state: 'published',
cookingTime: 60,
activeCookingTime: 30,
description: '# Test Recipe\nThis is a test recipe.',
steps: null,
videoUrl: null,
title: 'Test Recipe',
slug: 'test-recipe',
calories: 300.0,
likesCount: 10,
viewsCount: 100,
);
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: '/about-recipes-screen',
name: 'aboutRecipesScreen',
builder: (context, state) => const Scaffold(body: Text('About Recipe')),
),
],
),
);
}
testWidgets('renders my recipe widget', (tester) async {
tester.view.physicalSize = const Size(800, 2000);
tester.view.devicePixelRatio = 1.0;
addTearDown(() => tester.view.resetPhysicalSize());
mockNetworkImagesFor(() async {
final recipesCubit = MockRecipesCubit();
final myRecipesCubit = MockMyRecipesCubit();
when(recipesCubit.state).thenReturn(RecipesState.initial());
when(recipesCubit.stream).thenAnswer((_) => const Stream.empty());
when(recipesCubit.close()).thenAnswer((_) async => Future<void>.value());
when(myRecipesCubit.state).thenReturn(const MyRecipesState());
when(myRecipesCubit.stream).thenAnswer((_) => const Stream.empty());
when(myRecipesCubit.close()).thenAnswer((_) async => Future<void>.value());
await tester.pumpWidget(
MultiBlocProvider(
providers: [
BlocProvider<RecipesCubit>.value(value: recipesCubit),
BlocProvider<MyRecipesCubit>.value(value: myRecipesCubit),
],
child: wrap(
const Scaffold(
body: SingleChildScrollView(child: MyRecipeWidget(recipe: mockRecipe)),
),
),
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(find.byType(MyRecipeWidget), findsOneWidget);
});
});
});
}