feat(tests): updated tests coverage to 66.5%

This commit is contained in:
misha
2026-01-14 18:47:11 +03:00
parent 497ff19cfa
commit 13ccf56df0
102 changed files with 10146 additions and 379 deletions

View File

@@ -1,3 +1,4 @@
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/features/create_recipe/models/recipe_tag_model.dart';
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
@@ -35,6 +36,8 @@ void main() {
});
tearDown(() async {
EasyDebounce.cancel('add_or_remove_plan_from_favorite');
EasyDebounce.cancel('like_plan');
if (recipesCubit != null && !recipesCubit!.isClosed) {
await Future.delayed(const Duration(milliseconds: 100));
await recipesCubit!.close();
@@ -438,5 +441,111 @@ void main() {
expect(result, false);
});
test('loadRecipesList - обработка ошибки', () async {
recipesCubit = RecipesCubit(feedRepository: mockFeedRepository, recipeRepository: mockRecipeRepository, aboutRecipeRepository: mockAboutRecipeRepository);
when(mockFeedRepository.getRecipes(tags: anyNamed('tags'), query: anyNamed('query'))).thenThrow(Exception('Ошибка загрузки'));
await recipesCubit!.loadRecipesList();
await Future.delayed(const Duration(milliseconds: 200));
expect(recipesCubit!.state.errorMessage, isNotNull);
expect(recipesCubit!.state.isLoading, false);
});
test('loadNextPage - обработка ошибки', () async {
recipesCubit = RecipesCubit(feedRepository: mockFeedRepository, recipeRepository: mockRecipeRepository, aboutRecipeRepository: mockAboutRecipeRepository);
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),
);
final testTags = [RecipeTag(id: 1, name: 'Tag 1', recipesCount: 10, createdAt: DateTime(2024, 1, 1), updatedAt: DateTime(2024, 1, 2))];
when(mockFeedRepository.getRecipes(tags: anyNamed('tags'), query: anyNamed('query'))).thenAnswer((_) async => [testRecipe]);
when(mockRecipeRepository.getRecipesTags()).thenAnswer((_) async => testTags);
await recipesCubit!.loadRecipesList();
await Future.delayed(const Duration(milliseconds: 200));
recipesCubit!.lastPage = 2;
when(mockFeedRepository.getRecipes(page: 2, tags: anyNamed('tags'), query: anyNamed('query'))).thenThrow(Exception('Ошибка загрузки'));
await recipesCubit!.loadNextPage();
await Future.delayed(const Duration(milliseconds: 200));
expect(recipesCubit!.state.errorMessage, isNotNull);
expect(recipesCubit!.state.isLoading, false);
});
test('likeRecipe - убирает лайк', () async {
recipesCubit = RecipesCubit(feedRepository: mockFeedRepository, recipeRepository: mockRecipeRepository, aboutRecipeRepository: mockAboutRecipeRepository);
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),
);
final testTags = [RecipeTag(id: 1, name: 'Tag 1', recipesCount: 10, createdAt: DateTime(2024, 1, 1), updatedAt: DateTime(2024, 1, 2))];
when(mockFeedRepository.getRecipes(tags: anyNamed('tags'), query: anyNamed('query'))).thenAnswer((_) async => [testRecipe]);
when(mockRecipeRepository.getRecipesTags()).thenAnswer((_) async => testTags);
when(mockAboutRecipeRepository.likeRecipe('test-recipe')).thenAnswer((_) async => Future<void>.value());
await recipesCubit!.loadRecipesList();
await Future.delayed(const Duration(milliseconds: 200));
await recipesCubit!.likeRecipe('test-recipe', 0);
expect(recipesCubit!.state.recipesList[0].wasLikedByMe, false);
expect(recipesCubit!.state.recipesList[0].likesCount, 9);
await Future.delayed(const Duration(milliseconds: 300));
verify(mockAboutRecipeRepository.likeRecipe('test-recipe')).called(1);
});
});
}

View File

@@ -72,6 +72,27 @@ void main() {
expect(state.recipesTags.length, 1);
expect(state.selectedTags.length, 1);
});
test('copyWith creates new instance with updated values', () {
const original = RecipesState(
recipesList: [],
recipesTags: [],
selectedTags: {},
selectedTagsCounter: 0,
isLoading: true,
errorMessage: null,
);
final updated = original.copyWith(
isLoading: false,
errorMessage: 'Test error',
);
expect(updated.isLoading, isFalse);
expect(updated.errorMessage, 'Test error');
expect(updated.recipesList, original.recipesList);
expect(updated.recipesTags, original.recipesTags);
});
});
}