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

@@ -0,0 +1,160 @@
import 'package:dio/dio.dart';
import 'package:eatvio_app/features/create_recipe/models/ingredient.dart' as i;
import 'package:eatvio_app/features/create_recipe/presentation/bloc/create_recipe_cubit/create_recipe_cubit.dart';
import 'package:eatvio_app/features/create_recipe/reporitories/recipe_repository.dart';
import 'package:eatvio_app/features/create_recipe/services/recipe_service.dart';
import 'package:eatvio_app/features/glossary/repositories/glossary_repository.dart';
import 'package:eatvio_app/features/glossary/services/glossary_service.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('CreateRecipeCubit', () {
late CreateRecipeCubit cubit;
setUp(() {
final recipeRepository = RecipeRepository(recipeService: CreateRecipeService(Dio(), baseUrl: ''));
final glossaryRepository = GlossaryRepository(glossaryService: GlossaryService(Dio(), baseUrl: ''));
cubit = CreateRecipeCubit(recipeRepository: recipeRepository, glossaryRepository: glossaryRepository);
});
tearDown(() async {
await cubit.close();
});
test('initial state has default values', () {
final state = cubit.state;
expect(state.loading, true);
expect(state.entity.steps.length, 1);
expect(state.entity.ingredients, isEmpty);
expect(state.entity.tags, isEmpty);
});
test('addStep adds a new empty step', () {
final initialLength = cubit.state.entity.steps.length;
cubit.addStep();
expect(cubit.state.entity.steps.length, initialLength + 1);
});
test('removeStep removes provided step', () {
cubit.addStep();
final initialLength = cubit.state.entity.steps.length;
final step = cubit.state.entity.steps.first;
cubit.removeStep(step);
expect(cubit.state.entity.steps.length, initialLength - 1);
});
test('addIngredient appends ingredient to list', () {
const ingredient = i.Ingredient(name: 'Apple', unit: 'pc', portion: '100', productId: 1);
cubit.addIngredient(ingredient);
expect(cubit.state.entity.ingredients.length, 1);
expect(cubit.state.entity.ingredients.first.name, 'Apple');
});
test('removeIngredient removes ingredient by index', () {
const ingredient = i.Ingredient(name: 'Apple', unit: 'pc', portion: '100', productId: 1);
cubit.addIngredient(ingredient);
expect(cubit.state.entity.ingredients, isNotEmpty);
cubit.removeIngredient(0);
expect(cubit.state.entity.ingredients, isEmpty);
});
test('updateIngredient replaces ingredient at index', () {
const ingredient1 = i.Ingredient(name: 'Apple', unit: 'pc', portion: '100', productId: 1);
const ingredient2 = i.Ingredient(name: 'Banana', unit: 'pc', portion: '150', productId: 2);
cubit.addIngredient(ingredient1);
cubit.updateIngredient(index: 0, ingredient: ingredient2);
expect(cubit.state.entity.ingredients.first.name, 'Banana');
});
test('replaceIngredientPosition moves ingredient', () {
const ingredient1 = i.Ingredient(name: 'Apple', unit: 'pc', portion: '100', productId: 1);
const ingredient2 = i.Ingredient(name: 'Banana', unit: 'pc', portion: '150', productId: 2);
cubit.addIngredient(ingredient1);
cubit.addIngredient(ingredient2);
cubit.replaceIngredientPosition(0, 1);
expect(cubit.state.entity.ingredients[1].name, 'Apple');
});
test('replaceTitle and replaceDescription update entity', () {
cubit.replaceTitle('New title');
cubit.replaceDescription('New description');
expect(cubit.state.entity.title, 'New title');
expect(cubit.state.entity.description, 'New description');
});
test('addTag and removeTag work correctly', () {
cubit.addTag('vegan');
cubit.addTag('breakfast');
expect(cubit.state.entity.tags, ['vegan', 'breakfast']);
cubit.removeTag('vegan');
expect(cubit.state.entity.tags, ['breakfast']);
});
test('portion and energy setters update entity', () {
cubit.replacePortionsCount(4);
cubit.replaceEnegryInPortion(250);
cubit.setTotalCookingMinutes(30);
cubit.setTotalCookingHours(1);
cubit.setActiveCookingMinutes(20);
cubit.setActiveCookingHours(0);
final entity = cubit.state.entity;
expect(entity.countOfPortions, 4);
expect(entity.energyInOnePortion, 250);
expect(entity.totalCookMinutes, 30);
expect(entity.totalCookHours, 1);
expect(entity.activeCookMinutes, 20);
expect(entity.activeCookHours, 0);
});
test('replaceVideoUrl and replaceImage update entity', () {
cubit.replaceVideoUrl('https://example.com/video');
cubit.replaceImage(null);
expect(cubit.state.entity.videoUrl, 'https://example.com/video');
expect(cubit.state.entity.image, isNull);
});
test('replaceDescriptionInStep and replaceImageInStep update step', () {
final initialStep = cubit.state.entity.steps.first;
expect(initialStep.description, isNull);
cubit.replaceDescriptionInStep('Step 1', 0);
expect(cubit.state.entity.steps.first.description, 'Step 1');
cubit.replaceImageInStep(null, 0);
expect(cubit.state.entity.steps.first.image, isNull);
});
test('disableLoading sets loading to false', () {
expect(cubit.state.loading, true);
cubit.disableLoading();
expect(cubit.state.loading, false);
});
});
}