forked from eatvio/eatvio_app
feat(tests): test coverage is 69.2
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import 'package:eatvio_app/core/app_config.dart';
|
||||
import 'package:eatvio_app/core/app_initializer.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
import 'package:eatvio_app/features/create_diet/presentation/cubit/create_diet_cubit.dart';
|
||||
import 'package:eatvio_app/features/create_diet/repositories/create_diet_repository.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:eatvio_app/features/glossary/models/glossary_model.dart';
|
||||
import 'package:eatvio_app/features/glossary/repositories/glossary_repository.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
@@ -19,7 +23,13 @@ void main() {
|
||||
late MockProfileRepository profileRepository;
|
||||
late MockGlossaryRepository glossaryRepository;
|
||||
|
||||
setUp(() {
|
||||
setUpAll(() async {
|
||||
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
await sl.reset();
|
||||
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
|
||||
dietRepository = MockCreateDietRepository();
|
||||
profileRepository = MockProfileRepository();
|
||||
glossaryRepository = MockGlossaryRepository();
|
||||
@@ -335,5 +345,213 @@ void main() {
|
||||
cubit.removePortion(day: 0, meal: 0, portionIndex: 0);
|
||||
expect(cubit.state.dietData.dailySchedule?[0][0].length, 0);
|
||||
});
|
||||
|
||||
test('setInitialData returns early when diet is null', () async {
|
||||
await cubit.setInitialData(null);
|
||||
expect(cubit.state.isDietBeingEdited, false);
|
||||
expect(cubit.dietId, isNull);
|
||||
});
|
||||
|
||||
test('setInitialData initializes data for editing diet', () async {
|
||||
const testDiet = Diet(
|
||||
id: 1,
|
||||
slug: 'test-diet',
|
||||
name: 'Test Diet',
|
||||
image: 'test-image.jpg',
|
||||
duration: 7,
|
||||
state: DietState.active,
|
||||
type: DietType(light: true, veg: false, carb: false, meat: false),
|
||||
details: 'Test details',
|
||||
dailySchedule: {
|
||||
1: {
|
||||
1: {1: DailySchedule(name: 'Apple', portion: Portion.grams, quantity: 100, dailyNutrition: null, productNutritionalValueId: null)},
|
||||
},
|
||||
},
|
||||
author: Author(authorSlug: 'test-author', authorName: 'Test Author', authorProfileIcon: '', authorIsMe: false),
|
||||
totalLikes: 0,
|
||||
totalViews: 0,
|
||||
totalComments: 0,
|
||||
wasLikedByMe: false,
|
||||
isFavorite: false,
|
||||
dailyNutrition: [],
|
||||
);
|
||||
|
||||
final dailyScheduleWithNutrition = {
|
||||
1: {
|
||||
1: {1: const DailySchedule(name: 'Apple', portion: Portion.grams, quantity: 100, dailyNutrition: null, productNutritionalValueId: null)},
|
||||
},
|
||||
};
|
||||
when(dietRepository.getDietPortionsNutrition(1)).thenAnswer((_) async => dailyScheduleWithNutrition);
|
||||
|
||||
await cubit.setInitialData(testDiet);
|
||||
|
||||
expect(cubit.dietId, 1);
|
||||
expect(cubit.state.isDietBeingEdited, true);
|
||||
expect(cubit.state.dietData.name, 'Test Diet');
|
||||
expect(cubit.state.dietData.details, 'Test details');
|
||||
expect(cubit.state.dietData.duration, 7);
|
||||
expect(cubit.state.dietData.type['light'], true);
|
||||
expect(cubit.state.dietData.type['veg'], false);
|
||||
verify(dietRepository.getDietPortionsNutrition(1)).called(1);
|
||||
});
|
||||
|
||||
test('createDiet validates empty name', () async {
|
||||
cubit.setName('');
|
||||
cubit.setDetails('Test Description');
|
||||
cubit.setDuration(0);
|
||||
cubit.setDietState(DietState.active);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
|
||||
final result = await cubit.createDiet();
|
||||
|
||||
expect(result, 'validation');
|
||||
expect(cubit.state.dietNameError, 'Поле не заполнено');
|
||||
});
|
||||
|
||||
test('createDiet validates empty details', () async {
|
||||
cubit.setName('Test Diet');
|
||||
cubit.setDetails('');
|
||||
cubit.setDuration(0);
|
||||
cubit.setDietState(DietState.active);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
|
||||
final result = await cubit.createDiet();
|
||||
|
||||
expect(result, 'validation');
|
||||
expect(cubit.state.dietDescriptionError, 'Поле не заполнено');
|
||||
});
|
||||
|
||||
test('createDiet validates missing duration', () async {
|
||||
cubit.setName('Test Diet');
|
||||
cubit.setDetails('Test Description');
|
||||
cubit.setDietState(DietState.active);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
|
||||
final result = await cubit.createDiet();
|
||||
|
||||
expect(result, 'validation');
|
||||
expect(cubit.state.dietDurationError, 'Срок диеты не выбран');
|
||||
});
|
||||
|
||||
test('createDiet validates missing state', () async {
|
||||
cubit.setName('Test Diet');
|
||||
cubit.setDetails('Test Description');
|
||||
cubit.setDuration(0);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
|
||||
final result = await cubit.createDiet();
|
||||
|
||||
expect(result, 'validation');
|
||||
expect(cubit.state.dietStateError, 'Статус диеты не выбран');
|
||||
});
|
||||
|
||||
test('createDiet validates missing image', () async {
|
||||
cubit.setName('Test Diet');
|
||||
cubit.setDetails('Test Description');
|
||||
cubit.setDuration(0);
|
||||
cubit.setDietState(DietState.active);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
|
||||
final result = await cubit.createDiet();
|
||||
|
||||
expect(result, 'validation');
|
||||
expect(cubit.state.dietImageError, 'Не добавлена обложка');
|
||||
});
|
||||
|
||||
test('createDiet validates empty daily schedule', () async {
|
||||
cubit.setName('Test Diet');
|
||||
cubit.setDetails('Test Description');
|
||||
cubit.setDuration(0);
|
||||
cubit.setDietState(DietState.active);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
|
||||
final result = await cubit.createDiet();
|
||||
|
||||
expect(result, 'validation');
|
||||
expect(cubit.state.dailyScheduleErrors[0], 'Добавьте продукты');
|
||||
});
|
||||
|
||||
test('pasteCopiedDay validates day when pasting', () {
|
||||
cubit.setDuration(0);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
);
|
||||
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
cubit.copyDay(0);
|
||||
cubit.pasteCopiedDay(1);
|
||||
|
||||
expect(cubit.state.dietData.dailySchedule?[1][0].length, 1);
|
||||
expect(cubit.state.dailyScheduleErrors[1], '');
|
||||
});
|
||||
|
||||
test('_isDayValid returns false when day is empty', () {
|
||||
cubit.setDuration(0);
|
||||
final isValid = cubit.state.dietData.dailySchedule?[0].every((meal) => meal.isEmpty) ?? false;
|
||||
expect(isValid, true);
|
||||
});
|
||||
|
||||
test('_isDayValid returns true when day has portions', () {
|
||||
cubit.setDuration(0);
|
||||
const portion = DailySchedule(
|
||||
name: 'Apple',
|
||||
portion: Portion.grams,
|
||||
quantity: 100,
|
||||
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
|
||||
productNutritionalValueId: '1',
|
||||
);
|
||||
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
final isValid = cubit.state.dietData.dailySchedule?[0].every((meal) => meal.isEmpty) ?? true;
|
||||
expect(isValid, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user