forked from eatvio/eatvio_app
feat(tests): updated tests coverage to 66.5%
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:eatvio_app/features/create_plan/presentation/cubit/create_plan_cubit.dart';
|
||||
import 'package:eatvio_app/features/create_plan/repositories/create_plan_repository.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
|
||||
@@ -147,5 +148,304 @@ void main() {
|
||||
cubit.setPrice(1500.0);
|
||||
expect(cubit.validatePriceOfAllFilledOptions(), true);
|
||||
});
|
||||
|
||||
test('validateName returns false for null name', () {
|
||||
expect(cubit.validateName(), false);
|
||||
expect(cubit.state.validationState.titleHasError, true);
|
||||
});
|
||||
|
||||
test('validateName returns false for short name', () {
|
||||
cubit.setName('AB');
|
||||
expect(cubit.validateName(), false);
|
||||
expect(cubit.state.validationState.titleHasError, true);
|
||||
});
|
||||
|
||||
test('validateName returns true for valid name', () {
|
||||
cubit.setName('Test Plan Name');
|
||||
expect(cubit.validateName(), true);
|
||||
expect(cubit.state.validationState.titleHasError, false);
|
||||
});
|
||||
|
||||
test('validateDetails returns false for null details', () {
|
||||
expect(cubit.validateDetails(), false);
|
||||
expect(cubit.state.validationState.descriptionHasError, true);
|
||||
});
|
||||
|
||||
test('validateDetails returns false for short details', () {
|
||||
cubit.setDetails('AB');
|
||||
expect(cubit.validateDetails(), false);
|
||||
expect(cubit.state.validationState.descriptionHasError, true);
|
||||
});
|
||||
|
||||
test('validateDetails returns true for valid details', () {
|
||||
cubit.setDetails('Test description');
|
||||
expect(cubit.validateDetails(), true);
|
||||
expect(cubit.state.validationState.descriptionHasError, false);
|
||||
});
|
||||
|
||||
test('validateDuration returns false for null duration', () {
|
||||
expect(cubit.validateDuration(), false);
|
||||
expect(cubit.state.validationState.durationHasError, true);
|
||||
});
|
||||
|
||||
test('validateDuration returns true when duration is set', () {
|
||||
cubit.setDuration(0);
|
||||
expect(cubit.validateDuration(), true);
|
||||
expect(cubit.state.validationState.durationHasError, false);
|
||||
});
|
||||
|
||||
test('validateType returns false for empty type', () {
|
||||
cubit.setPlanType([]);
|
||||
expect(cubit.validateType(), false);
|
||||
expect(cubit.state.validationState.typeHasError, true);
|
||||
});
|
||||
|
||||
test('validateType returns false when all types are true', () {
|
||||
cubit.setPlanType([true, true, true, true]);
|
||||
expect(cubit.validateType(), false);
|
||||
expect(cubit.state.validationState.typeHasError, true);
|
||||
});
|
||||
|
||||
test('validateType returns true for valid type', () {
|
||||
cubit.setPlanType([true, false, false, false]);
|
||||
expect(cubit.validateType(), true);
|
||||
expect(cubit.state.validationState.typeHasError, false);
|
||||
});
|
||||
|
||||
test('validateImage returns false for null image', () {
|
||||
expect(cubit.validateImage(), false);
|
||||
expect(cubit.state.validationState.imageHasError, true);
|
||||
});
|
||||
|
||||
test('validateImage returns true when image is set', () {
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
expect(cubit.validateImage(), true);
|
||||
expect(cubit.state.validationState.imageHasError, false);
|
||||
});
|
||||
|
||||
test('validateSelectedOptionPrice returns false for price less than 1200', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setPrice(1000.0);
|
||||
expect(cubit.validateSelectedOptionPrice(), false);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].priceHasError, true);
|
||||
});
|
||||
|
||||
test('validateSelectedOptionPrice returns true for price >= 1200', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setPrice(1200.0);
|
||||
expect(cubit.validateSelectedOptionPrice(), true);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].priceHasError, false);
|
||||
});
|
||||
|
||||
test('setImage updates image and validates', () {
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
expect(cubit.state.planData.image, image);
|
||||
expect(cubit.state.validationState.imageHasError, false);
|
||||
});
|
||||
|
||||
test('setPlanState updates state', () {
|
||||
cubit.setPlanState('active');
|
||||
expect(cubit.state.planData.state, 'active');
|
||||
});
|
||||
|
||||
test('setOptionContent adds new item when index is out of bounds', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
expect(cubit.state.planData.options[0].items.length, 0);
|
||||
|
||||
cubit.setOptionContent('Item 1', 0);
|
||||
expect(cubit.state.planData.options[0].items.length, 1);
|
||||
expect(cubit.state.planData.options[0].items[0], 'Item 1');
|
||||
});
|
||||
|
||||
test('validatePriceOfAllFilledOptions returns false when some prices are invalid', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setPrice(1500.0);
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setPrice(1000.0); // Invalid
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setPrice(1500.0);
|
||||
expect(cubit.validatePriceOfAllFilledOptions(), false);
|
||||
});
|
||||
|
||||
test('validateSelectedOptionTitle returns false for short title', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('AB');
|
||||
expect(cubit.validateSelectedOptionTitle(), false);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].titleHasError, true);
|
||||
});
|
||||
|
||||
test('validateSelectedOptionTitle returns true for valid title', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Valid Title');
|
||||
expect(cubit.validateSelectedOptionTitle(), true);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].titleHasError, false);
|
||||
});
|
||||
|
||||
test('validateTitlesOfAllFilledOptions validates all options', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Title 1');
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setOptionTitle('Title 2');
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setOptionTitle('Title 3');
|
||||
expect(cubit.validateTitlesOfAllFilledOptions(), true);
|
||||
});
|
||||
|
||||
test('validateTitlesOfAllFilledOptions returns false when some titles are invalid', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Valid Title');
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setOptionTitle('AB'); // Invalid
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setOptionTitle('Valid Title');
|
||||
expect(cubit.validateTitlesOfAllFilledOptions(), false);
|
||||
});
|
||||
|
||||
test('validateTitlesOfAllFilledOptions returns early when selectedOptionIndex is null', () {
|
||||
cubit.setSelectedOptionIndex(null);
|
||||
cubit.setOptionTitle('Title 1');
|
||||
final result = cubit.validateTitlesOfAllFilledOptions();
|
||||
// Should only validate first option when selectedOptionIndex is null
|
||||
expect(result, isA<bool>());
|
||||
});
|
||||
|
||||
test('validateSelectedOptionDescription returns false for short description', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionDescription('AB');
|
||||
expect(cubit.validateSelectedOptionDescription(), false);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].descriptionHasError, true);
|
||||
});
|
||||
|
||||
test('validateSelectedOptionDescription returns true for valid description', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionDescription('Valid description');
|
||||
expect(cubit.validateSelectedOptionDescription(), true);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].descriptionHasError, false);
|
||||
});
|
||||
|
||||
test('validateDescriptionsOfAllOptions validates all options', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionDescription('Description 1');
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setOptionDescription('Description 2');
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setOptionDescription('Description 3');
|
||||
expect(cubit.validateDescriptionsOfAllOptions(), true);
|
||||
});
|
||||
|
||||
test('validateDescriptionsOfAllOptions returns false when some descriptions are invalid', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionDescription('Valid description');
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setOptionDescription('AB'); // Invalid
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setOptionDescription('Valid description');
|
||||
expect(cubit.validateDescriptionsOfAllOptions(), false);
|
||||
});
|
||||
|
||||
test('validateDescriptionsOfAllOptions returns early when selectedOptionIndex is null', () {
|
||||
cubit.setSelectedOptionIndex(null);
|
||||
cubit.setOptionDescription('Description 1');
|
||||
final result = cubit.validateDescriptionsOfAllOptions();
|
||||
// Should only validate first option when selectedOptionIndex is null
|
||||
expect(result, isA<bool>());
|
||||
});
|
||||
|
||||
test('validateItemsOfAllFilledOptions validates all options', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionContent('Item 1', 0);
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setOptionContent('Item 2', 0);
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setOptionContent('Item 3', 0);
|
||||
expect(cubit.validateItemsOfAllFilledOptions(), true);
|
||||
});
|
||||
|
||||
test('validateItemsOfAllFilledOptions returns false when some items are invalid', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionContent('Valid item', 0);
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setOptionContent('AB', 0); // Invalid
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setOptionContent('Valid item', 0);
|
||||
expect(cubit.validateItemsOfAllFilledOptions(), false);
|
||||
});
|
||||
|
||||
test('validateItemsOfAllFilledOptions returns false when items are empty', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
expect(cubit.validateItemsOfAllFilledOptions(), false);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].isItemsEmpty, true);
|
||||
});
|
||||
|
||||
test('validateItemsOfAllFilledOptions returns early when selectedOptionIndex is null', () {
|
||||
cubit.setSelectedOptionIndex(null);
|
||||
cubit.setOptionContent('Item 1', 0);
|
||||
final result = cubit.validateItemsOfAllFilledOptions();
|
||||
// Should only validate first option when selectedOptionIndex is null
|
||||
expect(result, isA<bool>());
|
||||
});
|
||||
|
||||
test('validateAllFields returns false when name is invalid', () {
|
||||
cubit.setName('AB');
|
||||
cubit.setDetails('Valid description');
|
||||
cubit.setDuration(0);
|
||||
cubit.setPlanType([true, false, false, false]);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Valid Title');
|
||||
cubit.setPrice(1500.0);
|
||||
cubit.setOptionDescription('Valid description');
|
||||
cubit.setOptionContent('Valid item', 0);
|
||||
|
||||
expect(cubit.validateAllFields(), false);
|
||||
});
|
||||
|
||||
test('validateAllFields returns false when details is invalid', () {
|
||||
cubit.setName('Valid Name');
|
||||
cubit.setDetails('AB');
|
||||
cubit.setDuration(0);
|
||||
cubit.setPlanType([true, false, false, false]);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Valid Title');
|
||||
cubit.setPrice(1500.0);
|
||||
cubit.setOptionDescription('Valid description');
|
||||
cubit.setOptionContent('Valid item', 0);
|
||||
|
||||
expect(cubit.validateAllFields(), false);
|
||||
});
|
||||
|
||||
test('validateAllFields returns false when image is null', () {
|
||||
cubit.setName('Valid Name');
|
||||
cubit.setDetails('Valid description');
|
||||
cubit.setDuration(0);
|
||||
cubit.setPlanType([true, false, false, false]);
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Valid Title');
|
||||
cubit.setPrice(1500.0);
|
||||
cubit.setOptionDescription('Valid description');
|
||||
cubit.setOptionContent('Valid item', 0);
|
||||
|
||||
expect(cubit.validateAllFields(), false);
|
||||
});
|
||||
|
||||
test('validateAllFields returns false when duration is null', () {
|
||||
cubit.setName('Valid Name');
|
||||
cubit.setDetails('Valid description');
|
||||
cubit.setPlanType([true, false, false, false]);
|
||||
final image = XFile('test_image.jpg');
|
||||
cubit.setImage(image);
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Valid Title');
|
||||
cubit.setPrice(1500.0);
|
||||
cubit.setOptionDescription('Valid description');
|
||||
cubit.setOptionContent('Valid item', 0);
|
||||
|
||||
expect(cubit.validateAllFields(), false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import 'package:eatvio_app/features/create_plan/models/create_plan_model.dart';
|
||||
import 'package:eatvio_app/features/create_plan/presentation/cubit/create_plan_state.dart';
|
||||
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('CreatePlanState', () {
|
||||
test('initial creates state with default values', () {
|
||||
final state = CreatePlanState.initial();
|
||||
|
||||
expect(state.isLoading, isFalse);
|
||||
expect(state.errorMessage, isNull);
|
||||
expect(state.planCreated, isFalse);
|
||||
expect(state.durationIndex, isNull);
|
||||
expect(state.selectedOptionIndex, isNull);
|
||||
expect(state.planData.type.length, 4);
|
||||
expect(state.planData.options.length, 3);
|
||||
expect(state.validationState.titleHasError, isFalse);
|
||||
});
|
||||
|
||||
test('creates CreatePlanState with custom values', () {
|
||||
final planData = CreatePlanModel(
|
||||
name: 'Test Plan',
|
||||
details: 'Test details',
|
||||
duration: 7,
|
||||
type: [true, false, false, false],
|
||||
options: [PacketOptions.empty()],
|
||||
);
|
||||
|
||||
final state = CreatePlanState(
|
||||
isLoading: true,
|
||||
errorMessage: 'Test error',
|
||||
planData: planData,
|
||||
validationState: PlanValidationState.empty(),
|
||||
planCreated: false,
|
||||
durationIndex: 1,
|
||||
selectedOptionIndex: 0,
|
||||
);
|
||||
|
||||
expect(state.isLoading, isTrue);
|
||||
expect(state.errorMessage, 'Test error');
|
||||
expect(state.durationIndex, 1);
|
||||
expect(state.selectedOptionIndex, 0);
|
||||
});
|
||||
|
||||
test('copyWith creates new instance with updated values', () {
|
||||
final original = CreatePlanState.initial();
|
||||
final updated = original.copyWith(
|
||||
isLoading: true,
|
||||
errorMessage: 'New error',
|
||||
);
|
||||
|
||||
expect(updated.isLoading, isTrue);
|
||||
expect(updated.errorMessage, 'New error');
|
||||
expect(updated.planData, original.planData);
|
||||
expect(updated.validationState, original.validationState);
|
||||
});
|
||||
});
|
||||
|
||||
group('PlanValidationState', () {
|
||||
test('empty creates state with default values', () {
|
||||
final state = PlanValidationState.empty();
|
||||
|
||||
expect(state.titleHasError, isFalse);
|
||||
expect(state.durationHasError, isFalse);
|
||||
expect(state.typeHasError, isFalse);
|
||||
expect(state.descriptionHasError, isFalse);
|
||||
expect(state.imageHasError, isFalse);
|
||||
expect(state.optionsValidationState.length, 3);
|
||||
});
|
||||
|
||||
test('creates PlanValidationState with custom values', () {
|
||||
final state = PlanValidationState(
|
||||
titleHasError: true,
|
||||
durationHasError: false,
|
||||
typeHasError: true,
|
||||
descriptionHasError: false,
|
||||
imageHasError: true,
|
||||
optionsValidationState: [
|
||||
OptionValidationState.empty(),
|
||||
],
|
||||
);
|
||||
|
||||
expect(state.titleHasError, isTrue);
|
||||
expect(state.typeHasError, isTrue);
|
||||
expect(state.imageHasError, isTrue);
|
||||
expect(state.optionsValidationState.length, 1);
|
||||
});
|
||||
|
||||
test('copyWith creates new instance with updated values', () {
|
||||
final original = PlanValidationState.empty();
|
||||
final updated = original.copyWith(titleHasError: true);
|
||||
|
||||
expect(updated.titleHasError, isTrue);
|
||||
expect(updated.durationHasError, original.durationHasError);
|
||||
});
|
||||
});
|
||||
|
||||
group('OptionValidationState', () {
|
||||
test('empty creates state with default values', () {
|
||||
final state = OptionValidationState.empty();
|
||||
|
||||
expect(state.titleHasError, isFalse);
|
||||
expect(state.priceHasError, isFalse);
|
||||
expect(state.descriptionHasError, isFalse);
|
||||
expect(state.isItemsEmpty, isFalse);
|
||||
expect(state.itemsErrors, isEmpty);
|
||||
});
|
||||
|
||||
test('creates OptionValidationState with custom values', () {
|
||||
final state = OptionValidationState(
|
||||
titleHasError: true,
|
||||
priceHasError: true,
|
||||
descriptionHasError: false,
|
||||
itemsErrors: {0: true, 1: false},
|
||||
isItemsEmpty: true,
|
||||
);
|
||||
|
||||
expect(state.titleHasError, isTrue);
|
||||
expect(state.priceHasError, isTrue);
|
||||
expect(state.isItemsEmpty, isTrue);
|
||||
expect(state.itemsErrors.length, 2);
|
||||
});
|
||||
|
||||
test('copyWith creates new instance with updated values', () {
|
||||
final original = OptionValidationState.empty();
|
||||
final updated = original.copyWith(titleHasError: true);
|
||||
|
||||
expect(updated.titleHasError, isTrue);
|
||||
expect(updated.priceHasError, original.priceHasError);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user