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

@@ -10,10 +10,7 @@ import 'package:mockito/mockito.dart';
import 'about_recipe_cubit_test.mocks.dart';
@GenerateNiceMocks([
MockSpec<AboutRecipeRepository>(),
MockSpec<ProfileRepository>(),
])
@GenerateNiceMocks([MockSpec<AboutRecipeRepository>(), MockSpec<ProfileRepository>()])
void main() {
group('AboutRecipeCubit', () {
AboutRecipeCubit? aboutRecipeCubit;
@@ -33,19 +30,13 @@ void main() {
});
test('начальное состояние - initial', () {
aboutRecipeCubit = AboutRecipeCubit(
recipeRepository: mockRecipeRepository,
profileRepository: mockProfileRepository,
);
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
expect(aboutRecipeCubit!.state, isA<AboutRecipeState>());
});
test('getRecipe - успешная загрузка рецепта', () async {
aboutRecipeCubit = AboutRecipeCubit(
recipeRepository: mockRecipeRepository,
profileRepository: mockProfileRepository,
);
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
const testRecipe = Recipe(
id: 1,
@@ -80,8 +71,7 @@ void main() {
),
);
when(mockRecipeRepository.getRecipe('1'))
.thenAnswer((_) async => testRecipe);
when(mockRecipeRepository.getRecipe('1')).thenAnswer((_) async => testRecipe);
await aboutRecipeCubit!.getRecipe('1');
@@ -91,13 +81,9 @@ void main() {
});
test('getRecipe - обработка ошибки', () async {
aboutRecipeCubit = AboutRecipeCubit(
recipeRepository: mockRecipeRepository,
profileRepository: mockProfileRepository,
);
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
when(mockRecipeRepository.getRecipe(any))
.thenThrow(Exception('Ошибка загрузки'));
when(mockRecipeRepository.getRecipe(any)).thenThrow(Exception('Ошибка загрузки'));
await aboutRecipeCubit!.getRecipe('1');
@@ -107,10 +93,7 @@ void main() {
});
test('likeRecipe - лайк рецепта', () async {
aboutRecipeCubit = AboutRecipeCubit(
recipeRepository: mockRecipeRepository,
profileRepository: mockProfileRepository,
);
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
const testRecipe = Recipe(
id: 1,
@@ -145,10 +128,8 @@ void main() {
),
);
when(mockRecipeRepository.getRecipe('1'))
.thenAnswer((_) async => testRecipe);
when(mockRecipeRepository.likeRecipe('1'))
.thenAnswer((_) async => {});
when(mockRecipeRepository.getRecipe('1')).thenAnswer((_) async => testRecipe);
when(mockRecipeRepository.likeRecipe('1')).thenAnswer((_) async => {});
await aboutRecipeCubit!.getRecipe('1');
await aboutRecipeCubit!.likeRecipe('1', null);
@@ -159,6 +140,211 @@ void main() {
expect(loadedState.recipe.wasLikedByMe, isTrue);
expect(loadedState.recipe.likesCount, 11);
});
test('initRecipe - успешная инициализация рецепта', () async {
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
const testRecipe = Recipe(
id: 1,
image: 'https://example.com/image.jpg',
state: 'active',
cookingTime: 30,
activeCookingTime: 15,
ingredients: [],
description: 'Test description',
steps: [],
videoUrl: null,
createdAt: null,
updatedAt: null,
title: 'Test Recipe',
type: 'main',
slug: 'test-recipe',
commentsCount: 5,
wasLikedByMe: false,
isFavorite: false,
calories: 300.0,
servings: 4,
likesCount: 10,
viewsCount: 100,
tags: [],
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
);
when(mockRecipeRepository.getRecipe('1')).thenAnswer((_) async => testRecipe);
await aboutRecipeCubit!.initRecipe('1');
expect(aboutRecipeCubit!.state, isA<AboutRecipeStateLoaded>());
final loadedState = aboutRecipeCubit!.state as AboutRecipeStateLoaded;
expect(loadedState.recipe.id, 1);
});
test('initRecipe - обработка ошибки', () async {
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
when(mockRecipeRepository.getRecipe('1')).thenThrow(Exception('Ошибка загрузки'));
await aboutRecipeCubit!.initRecipe('1');
expect(aboutRecipeCubit!.state, isA<RecipeStateError>());
});
test('likeRecipe - убирает лайк если уже лайкнуто', () async {
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
const testRecipe = Recipe(
id: 1,
image: 'https://example.com/image.jpg',
state: 'active',
cookingTime: 30,
activeCookingTime: 15,
ingredients: [],
description: 'Test description',
steps: [],
videoUrl: null,
createdAt: null,
updatedAt: null,
title: 'Test Recipe',
type: 'main',
slug: 'test-recipe',
commentsCount: 5,
wasLikedByMe: true,
isFavorite: false,
calories: 300.0,
servings: 4,
likesCount: 10,
viewsCount: 100,
tags: [],
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
);
when(mockRecipeRepository.getRecipe('1')).thenAnswer((_) async => testRecipe);
when(mockRecipeRepository.likeRecipe('1')).thenAnswer((_) async => Future<void>.value());
await aboutRecipeCubit!.getRecipe('1');
await aboutRecipeCubit!.likeRecipe('1', null);
expect(aboutRecipeCubit!.state, isA<AboutRecipeStateLoaded>());
final loadedState = aboutRecipeCubit!.state as AboutRecipeStateLoaded;
expect(loadedState.recipe.wasLikedByMe, isFalse);
expect(loadedState.recipe.likesCount, 9);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockRecipeRepository.likeRecipe('1')).called(1);
});
test('addToFavorite - добавляет рецепт в избранное', () async {
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
const testRecipe = Recipe(
id: 1,
image: 'https://example.com/image.jpg',
state: 'active',
cookingTime: 30,
activeCookingTime: 15,
ingredients: [],
description: 'Test description',
steps: [],
videoUrl: null,
createdAt: null,
updatedAt: null,
title: 'Test Recipe',
type: 'main',
slug: 'test-recipe',
commentsCount: 5,
wasLikedByMe: false,
isFavorite: false,
calories: 300.0,
servings: 4,
likesCount: 10,
viewsCount: 100,
tags: [],
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
);
when(mockRecipeRepository.getRecipe('1')).thenAnswer((_) async => testRecipe);
when(mockRecipeRepository.addToFavorite('1')).thenAnswer((_) async => Future<void>.value());
await aboutRecipeCubit!.getRecipe('1');
await aboutRecipeCubit!.addToFavorite('1', null);
expect(aboutRecipeCubit!.state, isA<AboutRecipeStateLoaded>());
final loadedState = aboutRecipeCubit!.state as AboutRecipeStateLoaded;
expect(loadedState.recipe.isFavorite, true);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockRecipeRepository.addToFavorite('1')).called(1);
});
test('removeFromFavorite - удаляет рецепт из избранного', () async {
aboutRecipeCubit = AboutRecipeCubit(recipeRepository: mockRecipeRepository, profileRepository: mockProfileRepository);
const testRecipe = Recipe(
id: 1,
image: 'https://example.com/image.jpg',
state: 'active',
cookingTime: 30,
activeCookingTime: 15,
ingredients: [],
description: 'Test description',
steps: [],
videoUrl: null,
createdAt: null,
updatedAt: null,
title: 'Test Recipe',
type: 'main',
slug: 'test-recipe',
commentsCount: 5,
wasLikedByMe: false,
isFavorite: true,
calories: 300.0,
servings: 4,
likesCount: 10,
viewsCount: 100,
tags: [],
author: Author(
authorSlug: 'author',
authorName: 'Author Name',
authorProfileIcon: 'https://example.com/icon.jpg',
authorIsMe: false,
isEntitySubscribedByMe: null,
authorPlaceInRating: null,
),
);
when(mockRecipeRepository.getRecipe('1')).thenAnswer((_) async => testRecipe);
when(mockRecipeRepository.removeFromFavorite('1')).thenAnswer((_) async => Future<void>.value());
await aboutRecipeCubit!.getRecipe('1');
await aboutRecipeCubit!.removeFromFavorite('1', null);
expect(aboutRecipeCubit!.state, isA<AboutRecipeStateLoaded>());
final loadedState = aboutRecipeCubit!.state as AboutRecipeStateLoaded;
expect(loadedState.recipe.isFavorite, false);
await Future.delayed(const Duration(milliseconds: 1200));
verify(mockRecipeRepository.removeFromFavorite('1')).called(1);
});
});
}

View File

@@ -0,0 +1,85 @@
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/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/recipes_model.dart';
import 'package:eatvio_app/features/recipe/presentation/widgets/ingredients_widget.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';
void main() {
group('IngredientsWidget 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 mockAuthor = Author(
authorSlug: 'test-author',
authorName: 'Test Author',
authorProfileIcon: '/test.jpg',
authorIsMe: false,
isEntitySubscribedByMe: false,
authorPlaceInRating: null,
);
final mockRecipe = Recipe(
id: 1,
image: '/test.jpg',
state: 'active',
cookingTime: 30,
activeCookingTime: 15,
ingredients: const [
Ingredient(name: 'Flour', unit: 'г', portion: '200', productId: 1),
Ingredient(name: 'Sugar', unit: 'г', portion: '100', productId: 2),
],
description: 'Test description',
steps: const [],
videoUrl: null,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
title: 'Test Recipe',
type: 'main',
slug: 'test-recipe',
commentsCount: 0,
wasLikedByMe: false,
isFavorite: false,
calories: 200.0,
servings: 4,
likesCount: 10,
viewsCount: 100,
tags: const [],
author: mockAuthor,
);
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('renders ingredients widget', (tester) async {
await tester.pumpWidget(wrap(IngredientsWidget(recipe: mockRecipe)));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(find.byType(IngredientsWidget), findsOneWidget);
});
});
}

View File

@@ -0,0 +1,31 @@
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/features/recipe/presentation/widgets/my_recipes_tab.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('MyRecipesTab smoke', () {
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('renders my recipes tab', (tester) async {
await tester.pumpWidget(wrap(const MyRecipesTab()));
await tester.pump();
expect(find.byType(MyRecipesTab), findsOneWidget);
});
});
}