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,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);
});
});
});
}