forked from eatvio/eatvio_app
feat(tests): increased tests coverage
This commit is contained in:
@@ -119,5 +119,324 @@ void main() {
|
||||
expect(recipesCubit!.state.selectedTags.contains(1), isFalse);
|
||||
expect(recipesCubit!.state.selectedTagsCounter, 0);
|
||||
});
|
||||
|
||||
test('loadNextPage - загружает следующую страницу', () async {
|
||||
recipesCubit = RecipesCubit(feedRepository: mockFeedRepository, recipeRepository: mockRecipeRepository, aboutRecipeRepository: mockAboutRecipeRepository);
|
||||
|
||||
const testRecipe1 = Recipe(
|
||||
id: 1,
|
||||
image: 'https://example.com/image1.jpg',
|
||||
state: 'active',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
ingredients: [],
|
||||
description: 'Test description 1',
|
||||
steps: [],
|
||||
videoUrl: null,
|
||||
createdAt: null,
|
||||
updatedAt: null,
|
||||
title: 'Test Recipe 1',
|
||||
type: 'main',
|
||||
slug: 'test-recipe-1',
|
||||
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,
|
||||
),
|
||||
);
|
||||
|
||||
const testRecipe2 = Recipe(
|
||||
id: 2,
|
||||
image: 'https://example.com/image2.jpg',
|
||||
state: 'active',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
ingredients: [],
|
||||
description: 'Test description 2',
|
||||
steps: [],
|
||||
videoUrl: null,
|
||||
createdAt: null,
|
||||
updatedAt: null,
|
||||
title: 'Test Recipe 2',
|
||||
type: 'main',
|
||||
slug: 'test-recipe-2',
|
||||
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 => [testRecipe1]);
|
||||
when(mockFeedRepository.getRecipes(page: 2, tags: anyNamed('tags'), query: anyNamed('query'))).thenAnswer((_) async => [testRecipe2]);
|
||||
when(mockRecipeRepository.getRecipesTags()).thenAnswer((_) async => testTags);
|
||||
|
||||
await recipesCubit!.loadRecipesList();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
// Устанавливаем lastPage вручную, так как loadRecipesList устанавливает lastPage = currentPage
|
||||
recipesCubit!.lastPage = 2;
|
||||
|
||||
await recipesCubit!.loadNextPage();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
expect(recipesCubit!.state.recipesList.length, 2);
|
||||
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));
|
||||
|
||||
// lastPage = currentPage, поэтому loadNextPage не должен загружать
|
||||
await recipesCubit!.loadNextPage();
|
||||
|
||||
verifyNever(mockFeedRepository.getRecipes(page: 2, tags: anyNamed('tags'), query: anyNamed('query')));
|
||||
});
|
||||
|
||||
test('addRecipeToFavorite - добавляет рецепт в избранное', () 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.addToFavorite('test-recipe')).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
await recipesCubit!.loadRecipesList();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await recipesCubit!.addRecipeToFavorite('test-recipe', 0);
|
||||
|
||||
expect(recipesCubit!.state.recipesList[0].isFavorite, true);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
verify(mockAboutRecipeRepository.addToFavorite('test-recipe')).called(1);
|
||||
});
|
||||
|
||||
test('removeRecipeFromFavorite - удаляет рецепт из избранного', () 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: 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,
|
||||
),
|
||||
);
|
||||
|
||||
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.removeFromFavorite('test-recipe')).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
await recipesCubit!.loadRecipesList();
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
|
||||
await recipesCubit!.removeRecipeFromFavorite('test-recipe', 0);
|
||||
|
||||
expect(recipesCubit!.state.recipesList[0].isFavorite, false);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
verify(mockAboutRecipeRepository.removeFromFavorite('test-recipe')).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')).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, true);
|
||||
expect(recipesCubit!.state.recipesList[0].likesCount, 11);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
verify(mockAboutRecipeRepository.likeRecipe('test-recipe')).called(1);
|
||||
});
|
||||
|
||||
test('deleteRecipe - удаляет рецепт', () async {
|
||||
recipesCubit = RecipesCubit(feedRepository: mockFeedRepository, recipeRepository: mockRecipeRepository, aboutRecipeRepository: mockAboutRecipeRepository);
|
||||
|
||||
when(mockAboutRecipeRepository.deleteRecipe('test-recipe')).thenAnswer((_) async => Future<void>.value());
|
||||
|
||||
final result = await recipesCubit!.deleteRecipe('test-recipe');
|
||||
|
||||
expect(result, true);
|
||||
verify(mockAboutRecipeRepository.deleteRecipe('test-recipe')).called(1);
|
||||
});
|
||||
|
||||
test('deleteRecipe - возвращает false при ошибке', () async {
|
||||
recipesCubit = RecipesCubit(feedRepository: mockFeedRepository, recipeRepository: mockRecipeRepository, aboutRecipeRepository: mockAboutRecipeRepository);
|
||||
|
||||
when(mockAboutRecipeRepository.deleteRecipe('test-recipe')).thenThrow(Exception('Ошибка удаления'));
|
||||
|
||||
final result = await recipesCubit!.deleteRecipe('test-recipe');
|
||||
|
||||
expect(result, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user