forked from eatvio/eatvio_app
feat(tests): updated tests coverage to 66.5%
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
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_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';
|
||||
|
||||
@@ -131,5 +134,91 @@ 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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