forked from eatvio/eatvio_app
feat(tests): test coverage 60%
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
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_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
|
||||
import 'create_plan_cubit_test.mocks.dart';
|
||||
|
||||
@GenerateNiceMocks([MockSpec<CreatePlanRepository>()])
|
||||
void main() {
|
||||
group('CreatePlanCubit unit', () {
|
||||
late CreatePlanCubit cubit;
|
||||
late MockCreatePlanRepository repository;
|
||||
|
||||
setUp(() {
|
||||
repository = MockCreatePlanRepository();
|
||||
cubit = CreatePlanCubit(createPlanRepository: repository);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
cubit.close();
|
||||
});
|
||||
|
||||
test('initial state is correct', () {
|
||||
expect(cubit.state.isLoading, false);
|
||||
expect(cubit.state.planCreated, false);
|
||||
expect(cubit.state.errorMessage, null);
|
||||
expect(cubit.state.planData.name, null);
|
||||
expect(cubit.state.planData.options.length, 3);
|
||||
});
|
||||
|
||||
test('setName updates name and validates', () {
|
||||
cubit.setName('Test Plan');
|
||||
expect(cubit.state.planData.name, 'Test Plan');
|
||||
expect(cubit.state.validationState.titleHasError, false);
|
||||
|
||||
cubit.setName('AB');
|
||||
expect(cubit.state.validationState.titleHasError, true);
|
||||
});
|
||||
|
||||
test('setDetails updates details and validates', () {
|
||||
cubit.setDetails('Test description');
|
||||
expect(cubit.state.planData.details, 'Test description');
|
||||
expect(cubit.state.validationState.descriptionHasError, false);
|
||||
|
||||
cubit.setDetails('AB');
|
||||
expect(cubit.state.validationState.descriptionHasError, true);
|
||||
});
|
||||
|
||||
test('setDuration updates duration and validates', () {
|
||||
cubit.setDuration(0);
|
||||
expect(cubit.state.durationIndex, 0);
|
||||
expect(cubit.state.planData.duration, 7);
|
||||
expect(cubit.state.validationState.durationHasError, false);
|
||||
});
|
||||
|
||||
test('setPlanType updates type and validates', () {
|
||||
cubit.setPlanType([true, false, false, false]);
|
||||
expect(cubit.state.planData.type, [true, false, false, false]);
|
||||
expect(cubit.state.validationState.typeHasError, false);
|
||||
|
||||
cubit.setPlanType([false, false, false, false]);
|
||||
expect(cubit.state.planData.type, [false, false, false, false]);
|
||||
});
|
||||
|
||||
test('setSelectedOptionIndex updates selected index', () {
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
expect(cubit.state.selectedOptionIndex, 1);
|
||||
|
||||
cubit.setSelectedOptionIndex(null);
|
||||
expect(cubit.state.selectedOptionIndex, null);
|
||||
});
|
||||
|
||||
test('setOptionTitle updates option title and validates', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionTitle('Option 1');
|
||||
expect(cubit.state.planData.options[0].short, 'Option 1');
|
||||
});
|
||||
|
||||
test('setPrice updates price and validates', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setPrice(1500.0);
|
||||
expect(cubit.state.planData.options[0].price, 1500.0);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].priceHasError, false);
|
||||
|
||||
cubit.setPrice(1000.0);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].priceHasError, true);
|
||||
});
|
||||
|
||||
test('setOptionDescription updates description and validates', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionDescription('Test description');
|
||||
expect(cubit.state.planData.options[0].description, 'Test description');
|
||||
});
|
||||
|
||||
test('setOptionContent adds and updates items', () {
|
||||
cubit.setSelectedOptionIndex(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');
|
||||
|
||||
cubit.setOptionContent('Item 1 Updated', 0);
|
||||
expect(cubit.state.planData.options[0].items[0], 'Item 1 Updated');
|
||||
});
|
||||
|
||||
test('removeOptionContentItem removes item', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setOptionContent('Item 1', 0);
|
||||
cubit.setOptionContent('Item 2', 1);
|
||||
expect(cubit.state.planData.options[0].items.length, 2);
|
||||
|
||||
cubit.removeOptionContentItem(0);
|
||||
expect(cubit.state.planData.options[0].items.length, 1);
|
||||
expect(cubit.state.planData.options[0].items[0], 'Item 2');
|
||||
});
|
||||
|
||||
test('togglePersonalizedDietOptionState toggles hasPersonalDiet', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
final initialValue = cubit.state.planData.options[0].hasPersonalDiet;
|
||||
|
||||
cubit.togglePersonalizedDietOptionState();
|
||||
expect(cubit.state.planData.options[0].hasPersonalDiet, !initialValue);
|
||||
|
||||
cubit.togglePersonalizedDietOptionState();
|
||||
expect(cubit.state.planData.options[0].hasPersonalDiet, initialValue);
|
||||
});
|
||||
|
||||
test('validateSelectedOptionItems validates items', () {
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
expect(cubit.validateSelectedOptionItems(), false);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].isItemsEmpty, true);
|
||||
|
||||
cubit.setOptionContent('Valid item', 0);
|
||||
expect(cubit.validateSelectedOptionItems(), true);
|
||||
expect(cubit.state.validationState.optionsValidationState[0].isItemsEmpty, false);
|
||||
|
||||
cubit.setOptionContent('AB', 1);
|
||||
expect(cubit.validateSelectedOptionItems(), false);
|
||||
});
|
||||
|
||||
test('validatePriceOfAllFilledOptions validates all options', () {
|
||||
// Set prices for all options
|
||||
cubit.setSelectedOptionIndex(0);
|
||||
cubit.setPrice(1500.0);
|
||||
cubit.setSelectedOptionIndex(1);
|
||||
cubit.setPrice(1500.0);
|
||||
cubit.setSelectedOptionIndex(2);
|
||||
cubit.setPrice(1500.0);
|
||||
expect(cubit.validatePriceOfAllFilledOptions(), true);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user