forked from eatvio/eatvio_app
merge upstream
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
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';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'create_diet_cubit_test.mocks.dart';
|
||||
|
||||
@@ -16,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();
|
||||
@@ -131,5 +144,414 @@ void main() {
|
||||
expect(cubit.state.dietData.dailySchedule, schedule);
|
||||
expect(cubit.state.dietDurationError, null);
|
||||
});
|
||||
|
||||
test('setImage updates image and clears error', () {
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image, imageUrl: 'https://example.com/image.jpg');
|
||||
expect(cubit.state.dietData.image, image);
|
||||
expect(cubit.state.dietData.imageUrl, 'https://example.com/image.jpg');
|
||||
expect(cubit.state.dietImageError, null);
|
||||
});
|
||||
|
||||
test('setImage with null clears image', () {
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
expect(cubit.state.dietData.image, image);
|
||||
|
||||
cubit.setImage(null);
|
||||
expect(cubit.state.dietData.image, null);
|
||||
expect(cubit.state.dietImageError, null);
|
||||
});
|
||||
|
||||
test('getGlossaryListByQuery returns glossary list', () async {
|
||||
final glossaryList = [
|
||||
const GlossaryModel(
|
||||
id: 1,
|
||||
name: 'Apple',
|
||||
grInPortion: 100,
|
||||
energyOn100Gr: 52.0,
|
||||
proteinsOn100Gr: 0.3,
|
||||
fatsOn100Gr: 0.2,
|
||||
carbohydratesOn100Gr: 14.0,
|
||||
energyOnPortion: 52.0,
|
||||
proteinsOnPortion: 0.3,
|
||||
fatsOnPortion: 0.2,
|
||||
carbohydratesOnPortion: 14.0,
|
||||
),
|
||||
];
|
||||
when(glossaryRepository.getGlossaryListByQuery('apple')).thenAnswer((_) async => glossaryList);
|
||||
|
||||
final result = await cubit.getGlossaryListByQuery('apple');
|
||||
|
||||
expect(result, glossaryList);
|
||||
verify(glossaryRepository.getGlossaryListByQuery('apple')).called(1);
|
||||
});
|
||||
|
||||
test('setDuration decreases daily schedule when duration is reduced', () {
|
||||
cubit.setDuration(1); // 14 days
|
||||
expect(cubit.state.dietData.dailySchedule?.length, 14);
|
||||
|
||||
cubit.setDuration(0); // 7 days
|
||||
expect(cubit.state.dietData.dailySchedule?.length, 7);
|
||||
});
|
||||
|
||||
test('setDuration increases daily schedule when duration is increased', () {
|
||||
cubit.setDuration(0); // 7 days
|
||||
expect(cubit.state.dietData.dailySchedule?.length, 7);
|
||||
|
||||
cubit.setDuration(1); // 14 days
|
||||
expect(cubit.state.dietData.dailySchedule?.length, 14);
|
||||
});
|
||||
|
||||
test('addPortion does not add when dailySchedule is null', () {
|
||||
const portion = DailySchedule(name: 'Apple', portion: Portion.grams, quantity: 100, dailyNutrition: null, productNutritionalValueId: null);
|
||||
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
|
||||
expect(cubit.state.dietData.dailySchedule, null);
|
||||
});
|
||||
|
||||
test('removePortion does not remove when dailySchedule is null', () {
|
||||
cubit.removePortion(day: 0, meal: 0, portionIndex: 0);
|
||||
expect(cubit.state.dietData.dailySchedule, null);
|
||||
});
|
||||
|
||||
test('updatePortion does not update when dailySchedule is null', () {
|
||||
const portion = DailySchedule(name: 'Apple', portion: Portion.grams, quantity: 100, dailyNutrition: null, productNutritionalValueId: null);
|
||||
cubit.updatePortion(dayIndex: 0, mealIndex: 0, portionIndex: 0, portion: portion);
|
||||
expect(cubit.state.dietData.dailySchedule, null);
|
||||
});
|
||||
|
||||
test('copyDay does not copy when dailySchedule is null', () {
|
||||
cubit.copyDay(0);
|
||||
expect(cubit.state.copiedDay, null);
|
||||
});
|
||||
|
||||
test('pasteCopiedDay does not paste when copiedDay is null', () {
|
||||
cubit.setDuration(0);
|
||||
cubit.pasteCopiedDay(0);
|
||||
expect(cubit.state.dietData.dailySchedule?[0][0], isEmpty);
|
||||
});
|
||||
|
||||
test('getDietForPreview returns null when form is invalid', () async {
|
||||
final result = await cubit.getDietForPreview();
|
||||
expect(result, null);
|
||||
});
|
||||
|
||||
test('createDiet returns validation error when form is invalid', () async {
|
||||
final result = await cubit.createDiet();
|
||||
expect(result, 'validation');
|
||||
expect(cubit.state.isDietPublishing, false);
|
||||
});
|
||||
|
||||
test('updateDiet returns error when form is invalid', () async {
|
||||
final result = await cubit.updateDiet(1);
|
||||
expect(result, 'error');
|
||||
});
|
||||
|
||||
test('updateDiet handles error when image compression fails', () 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);
|
||||
// Set image with non-existent file to trigger error in compression
|
||||
final image = XFile('non_existent_image.jpg');
|
||||
cubit.setImage(image);
|
||||
|
||||
final result = await cubit.updateDiet(1);
|
||||
|
||||
// Should return 'error' due to image compression failure
|
||||
expect(result, 'error');
|
||||
});
|
||||
|
||||
test('createDiet validates name length', () async {
|
||||
cubit.setName('Te'); // Less than 3 characters
|
||||
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, 'Длинна названия не может быть меньше 3 символов');
|
||||
});
|
||||
|
||||
test('createDiet validates details length', () async {
|
||||
cubit.setName('Test Diet');
|
||||
cubit.setDetails('Te'); // Less than 3 characters
|
||||
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, 'Длинна описания не может быть меньше 3 символов');
|
||||
});
|
||||
|
||||
test('addPortion validates day when adding portion', () {
|
||||
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);
|
||||
|
||||
expect(cubit.state.dailyScheduleErrors[0], '');
|
||||
});
|
||||
|
||||
test('removePortion removes portion from daily schedule', () {
|
||||
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);
|
||||
expect(cubit.state.dietData.dailySchedule?[0][0].length, 1);
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import 'package:eatvio_app/core/const.dart';
|
||||
import 'package:eatvio_app/features/create_diet/models/create_diet_model.dart';
|
||||
import 'package:eatvio_app/features/create_diet/presentation/cubit/create_diet_state.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('CreateDietState', () {
|
||||
test('initial creates state with default values', () {
|
||||
final state = CreateDietState.initial();
|
||||
|
||||
expect(state.isLoading, isFalse);
|
||||
expect(state.isDietPublishing, isFalse);
|
||||
expect(state.errorMessage, isNull);
|
||||
expect(state.dietData.type, isNotNull);
|
||||
expect(state.dailyScheduleErrors.length, 21);
|
||||
expect(state.published, isFalse);
|
||||
expect(state.drafted, isFalse);
|
||||
expect(state.isDietBeingEdited, isFalse);
|
||||
});
|
||||
|
||||
test('creates CreateDietState with custom values', () {
|
||||
final dietData = CreateDietModel(
|
||||
name: 'Test Diet',
|
||||
details: 'Test details',
|
||||
duration: 7,
|
||||
type: Map.fromIterable(dietTypeValues, value: (element) => false),
|
||||
);
|
||||
|
||||
final state = CreateDietState(
|
||||
isLoading: true,
|
||||
isDietPublishing: false,
|
||||
errorMessage: 'Test error',
|
||||
dietData: dietData,
|
||||
copiedDay: null,
|
||||
dietNameError: 'Name error',
|
||||
dietDescriptionError: null,
|
||||
dietDurationError: null,
|
||||
dietStateError: null,
|
||||
dietImageError: null,
|
||||
dailyScheduleErrors: [],
|
||||
durationIndex: 0,
|
||||
dietState: DietState.active,
|
||||
published: false,
|
||||
drafted: false,
|
||||
isDietBeingEdited: false,
|
||||
);
|
||||
|
||||
expect(state.isLoading, isTrue);
|
||||
expect(state.errorMessage, 'Test error');
|
||||
expect(state.dietNameError, 'Name error');
|
||||
expect(state.dietState, DietState.active);
|
||||
});
|
||||
|
||||
test('copyWith creates new instance with updated values', () {
|
||||
final original = CreateDietState.initial();
|
||||
final updated = original.copyWith(
|
||||
isLoading: true,
|
||||
errorMessage: 'New error',
|
||||
);
|
||||
|
||||
expect(updated.isLoading, isTrue);
|
||||
expect(updated.errorMessage, 'New error');
|
||||
expect(updated.isDietPublishing, original.isDietPublishing);
|
||||
expect(updated.dietData, original.dietData);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user