feat(tests): tests coverage 67.2

This commit is contained in:
misha
2026-01-19 12:32:11 +03:00
parent 13ccf56df0
commit 44ede73ceb
20 changed files with 5084 additions and 409 deletions

View File

@@ -547,5 +547,207 @@ void main() {
await Future.delayed(const Duration(milliseconds: 300));
verify(mockAboutRecipeRepository.likeRecipe('test-recipe')).called(1);
});
test('loadRecipesList - загружает рецепты с query', () 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(query: 'test');
await Future.delayed(const Duration(milliseconds: 200));
verify(mockFeedRepository.getRecipes(tags: anyNamed('tags'), query: 'test')).called(1);
});
test('loadRecipesList - загружает рецепты с выбранными тегами', () 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)),
RecipeTag(id: 2, name: 'Tag 2', recipesCount: 20, createdAt: DateTime(2024, 1, 1), updatedAt: DateTime(2024, 1, 2)),
];
final tag1 = testTags[0];
recipesCubit!.updateTags(tag1);
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));
verify(mockFeedRepository.getRecipes(tags: [1], query: anyNamed('query'))).called(1);
});
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'))).thenAnswer((_) async {
await Future.delayed(const Duration(milliseconds: 100));
return [testRecipe];
});
final firstLoad = recipesCubit!.loadNextPage();
await recipesCubit!.loadNextPage();
await firstLoad;
await Future.delayed(const Duration(milliseconds: 200));
verify(mockFeedRepository.getRecipes(page: 2, tags: anyNamed('tags'), query: anyNamed('query'))).called(1);
});
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: 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);
when(mockAboutRecipeRepository.likeRecipe('test-recipe')).thenThrow(Exception('Ошибка'));
await recipesCubit!.loadRecipesList();
await Future.delayed(const Duration(milliseconds: 200));
await recipesCubit!.likeRecipe('test-recipe', 0);
await Future.delayed(const Duration(milliseconds: 400));
// После ошибки состояние должно вернуться к исходному
expect(recipesCubit!.state.recipesList[0].wasLikedByMe, false);
expect(recipesCubit!.state.recipesList[0].likesCount, 10);
});
test('updateTags - работает с несколькими тегами', () {
recipesCubit = RecipesCubit(feedRepository: mockFeedRepository, recipeRepository: mockRecipeRepository, aboutRecipeRepository: mockAboutRecipeRepository);
final tag1 = RecipeTag(id: 1, name: 'Tag 1', recipesCount: 10, createdAt: DateTime(2024, 1, 1), updatedAt: DateTime(2024, 1, 2));
final tag2 = RecipeTag(id: 2, name: 'Tag 2', recipesCount: 20, createdAt: DateTime(2024, 1, 1), updatedAt: DateTime(2024, 1, 2));
recipesCubit!.updateTags(tag1);
recipesCubit!.updateTags(tag2);
expect(recipesCubit!.state.selectedTags.contains(1), isTrue);
expect(recipesCubit!.state.selectedTags.contains(2), isTrue);
expect(recipesCubit!.state.selectedTagsCounter, 2);
recipesCubit!.updateTags(tag1);
expect(recipesCubit!.state.selectedTags.contains(1), isFalse);
expect(recipesCubit!.state.selectedTags.contains(2), isTrue);
expect(recipesCubit!.state.selectedTagsCounter, 1);
});
});
}