forked from eatvio/eatvio_app
feat(dependencies): add rxdart package and improve string extension methods for better handling of empty strings and gender parsing
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
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/cubit/about_recipe_cubit.dart';
|
||||
import 'package:eatvio_app/features/recipe/presentation/cubit/about_recipe_state.dart';
|
||||
import 'package:eatvio_app/features/recipe/repositories/about_recipe_repository.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'about_recipe_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<AboutRecipeRepository>(),
|
||||
MockSpec<ProfileRepository>(),
|
||||
])
|
||||
void main() {
|
||||
group('AboutRecipeCubit', () {
|
||||
AboutRecipeCubit? aboutRecipeCubit;
|
||||
late MockAboutRecipeRepository mockRecipeRepository;
|
||||
late MockProfileRepository mockProfileRepository;
|
||||
|
||||
setUp(() {
|
||||
mockRecipeRepository = MockAboutRecipeRepository();
|
||||
mockProfileRepository = MockProfileRepository();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (aboutRecipeCubit != null && !aboutRecipeCubit!.isClosed) {
|
||||
await aboutRecipeCubit!.close();
|
||||
aboutRecipeCubit = null;
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - initial', () {
|
||||
aboutRecipeCubit = AboutRecipeCubit(
|
||||
recipeRepository: mockRecipeRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
);
|
||||
|
||||
expect(aboutRecipeCubit!.state, isA<AboutRecipeState>());
|
||||
});
|
||||
|
||||
test('getRecipe - успешная загрузка рецепта', () 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!.getRecipe('1');
|
||||
|
||||
expect(aboutRecipeCubit!.state, isA<AboutRecipeStateLoaded>());
|
||||
final loadedState = aboutRecipeCubit!.state as AboutRecipeStateLoaded;
|
||||
expect(loadedState.recipe.id, 1);
|
||||
});
|
||||
|
||||
test('getRecipe - обработка ошибки', () async {
|
||||
aboutRecipeCubit = AboutRecipeCubit(
|
||||
recipeRepository: mockRecipeRepository,
|
||||
profileRepository: mockProfileRepository,
|
||||
);
|
||||
|
||||
when(mockRecipeRepository.getRecipe(any))
|
||||
.thenThrow(Exception('Ошибка загрузки'));
|
||||
|
||||
await aboutRecipeCubit!.getRecipe('1');
|
||||
|
||||
expect(aboutRecipeCubit!.state, isA<RecipeStateError>());
|
||||
final errorState = aboutRecipeCubit!.state as RecipeStateError;
|
||||
expect(errorState.message, isNotEmpty);
|
||||
});
|
||||
|
||||
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: 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.likeRecipe('1'))
|
||||
.thenAnswer((_) async => {});
|
||||
|
||||
await aboutRecipeCubit!.getRecipe('1');
|
||||
await aboutRecipeCubit!.likeRecipe('1', null);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 1200));
|
||||
expect(aboutRecipeCubit!.state, isA<AboutRecipeStateLoaded>());
|
||||
final loadedState = aboutRecipeCubit!.state as AboutRecipeStateLoaded;
|
||||
expect(loadedState.recipe.wasLikedByMe, isTrue);
|
||||
expect(loadedState.recipe.likesCount, 11);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
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/repositories/about_recipe_repository.dart';
|
||||
import 'package:eatvio_app/features/recipe/repositories/my_recipes_repository.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'my_recipes_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([
|
||||
MockSpec<MyRecipesRepository>(),
|
||||
MockSpec<AboutRecipeRepository>(),
|
||||
])
|
||||
void main() {
|
||||
group('MyRecipesCubit', () {
|
||||
MyRecipesCubit? cubit;
|
||||
late MockMyRecipesRepository mockMyRecipesRepository;
|
||||
late MockAboutRecipeRepository mockAboutRecipeRepository;
|
||||
|
||||
setUp(() {
|
||||
mockMyRecipesRepository = MockMyRecipesRepository();
|
||||
mockAboutRecipeRepository = MockAboutRecipeRepository();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (cubit != null && !cubit!.isClosed) {
|
||||
await cubit!.close();
|
||||
cubit = null;
|
||||
}
|
||||
});
|
||||
|
||||
test('начальное состояние - loading', () {
|
||||
cubit = MyRecipesCubit(
|
||||
myRecipesRepository: mockMyRecipesRepository,
|
||||
aboutRecipeRepository: mockAboutRecipeRepository,
|
||||
);
|
||||
|
||||
expect(cubit!.state, isA<MyRecipesStateLoading>());
|
||||
});
|
||||
|
||||
test('getMyRecipes - успешная загрузка активных рецептов', () async {
|
||||
cubit = MyRecipesCubit(
|
||||
myRecipesRepository: mockMyRecipesRepository,
|
||||
aboutRecipeRepository: mockAboutRecipeRepository,
|
||||
);
|
||||
|
||||
final testRecipes = [
|
||||
const MyRecipeModel(
|
||||
id: 1,
|
||||
slug: 'test-recipe',
|
||||
image: 'https://example.com/image.jpg',
|
||||
state: 'active',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test description',
|
||||
steps: [],
|
||||
videoUrl: null,
|
||||
title: 'Test Recipe',
|
||||
calories: 300.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
),
|
||||
];
|
||||
|
||||
when(mockMyRecipesRepository.getMyRecipes())
|
||||
.thenAnswer((_) async => testRecipes);
|
||||
|
||||
await cubit!.getMyRecipes();
|
||||
|
||||
expect(cubit!.state, isA<MyRecipesStateLoaded>());
|
||||
final loadedState = cubit!.state as MyRecipesStateLoaded;
|
||||
expect(loadedState.recipes.length, 1);
|
||||
expect(loadedState.selectedState, 'active');
|
||||
});
|
||||
|
||||
test('setActiveState - фильтрует активные рецепты', () async {
|
||||
cubit = MyRecipesCubit(
|
||||
myRecipesRepository: mockMyRecipesRepository,
|
||||
aboutRecipeRepository: mockAboutRecipeRepository,
|
||||
);
|
||||
|
||||
final testRecipes = [
|
||||
const MyRecipeModel(
|
||||
id: 1,
|
||||
slug: 'test-recipe-1',
|
||||
image: 'https://example.com/image1.jpg',
|
||||
state: 'active',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test description 1',
|
||||
steps: [],
|
||||
videoUrl: null,
|
||||
title: 'Test Recipe 1',
|
||||
calories: 300.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
),
|
||||
const MyRecipeModel(
|
||||
id: 2,
|
||||
slug: 'test-recipe-2',
|
||||
image: 'https://example.com/image2.jpg',
|
||||
state: 'archived',
|
||||
cookingTime: 45,
|
||||
activeCookingTime: 20,
|
||||
description: 'Test description 2',
|
||||
steps: [],
|
||||
videoUrl: null,
|
||||
title: 'Test Recipe 2',
|
||||
calories: 400.0,
|
||||
likesCount: 20,
|
||||
viewsCount: 200,
|
||||
),
|
||||
];
|
||||
|
||||
when(mockMyRecipesRepository.getMyRecipes())
|
||||
.thenAnswer((_) async => testRecipes);
|
||||
|
||||
await cubit!.setActiveState();
|
||||
|
||||
expect(cubit!.state, isA<MyRecipesStateLoaded>());
|
||||
final loadedState = cubit!.state as MyRecipesStateLoaded;
|
||||
expect(loadedState.recipes.length, 1);
|
||||
expect(loadedState.recipes.first.state, 'active');
|
||||
});
|
||||
|
||||
test('setArchivedState - фильтрует архивированные рецепты', () async {
|
||||
cubit = MyRecipesCubit(
|
||||
myRecipesRepository: mockMyRecipesRepository,
|
||||
aboutRecipeRepository: mockAboutRecipeRepository,
|
||||
);
|
||||
|
||||
final testRecipes = [
|
||||
const MyRecipeModel(
|
||||
id: 1,
|
||||
slug: 'test-recipe-1',
|
||||
image: 'https://example.com/image1.jpg',
|
||||
state: 'active',
|
||||
cookingTime: 30,
|
||||
activeCookingTime: 15,
|
||||
description: 'Test description 1',
|
||||
steps: [],
|
||||
videoUrl: null,
|
||||
title: 'Test Recipe 1',
|
||||
calories: 300.0,
|
||||
likesCount: 10,
|
||||
viewsCount: 100,
|
||||
),
|
||||
const MyRecipeModel(
|
||||
id: 2,
|
||||
slug: 'test-recipe-2',
|
||||
image: 'https://example.com/image2.jpg',
|
||||
state: 'archived',
|
||||
cookingTime: 45,
|
||||
activeCookingTime: 20,
|
||||
description: 'Test description 2',
|
||||
steps: [],
|
||||
videoUrl: null,
|
||||
title: 'Test Recipe 2',
|
||||
calories: 400.0,
|
||||
likesCount: 20,
|
||||
viewsCount: 200,
|
||||
),
|
||||
];
|
||||
|
||||
when(mockMyRecipesRepository.getMyRecipes())
|
||||
.thenAnswer((_) async => testRecipes);
|
||||
|
||||
await cubit!.setArchivedState();
|
||||
|
||||
expect(cubit!.state, isA<MyRecipesStateLoaded>());
|
||||
final loadedState = cubit!.state as MyRecipesStateLoaded;
|
||||
expect(loadedState.recipes.length, 1);
|
||||
expect(loadedState.recipes.first.state, 'archived');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user