forked from eatvio/eatvio_app
feat(tests): add fixtures
This commit is contained in:
@@ -17,12 +17,14 @@ void main() {
|
||||
late BlogPostCreateCubit blogPostCreateCubit;
|
||||
late Map<String, dynamic> fullCreateFixture;
|
||||
late Map<String, dynamic> updateFixture;
|
||||
late Map<String, dynamic> deleteFixture;
|
||||
|
||||
setUp(() {
|
||||
blogPostRepository = FixtureBlogPostRepository();
|
||||
blogPostCreateCubit = BlogPostCreateCubit(blogPostRepository: blogPostRepository);
|
||||
fullCreateFixture = FixtureReader.readAsMap('blog_post_create/create_request_full.json');
|
||||
updateFixture = FixtureReader.readAsMap('blog_post_create/update_request.json');
|
||||
deleteFixture = FixtureReader.readAsMap('blog_post_create/delete_request.json');
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
@@ -257,7 +259,7 @@ void main() {
|
||||
await tester.pump();
|
||||
|
||||
// Assert
|
||||
expect(blogPostRepository.deleteCalls.single, 1);
|
||||
expect(blogPostRepository.deleteCalls.single, deleteFixture['id']);
|
||||
});
|
||||
|
||||
testWidgets('should handle loading state during blog post creation', (WidgetTester tester) async {
|
||||
|
||||
@@ -230,7 +230,8 @@ void main() {
|
||||
await tester.pump();
|
||||
|
||||
// Assert
|
||||
expect(blogPostRepository.deleteCalls.single, 1);
|
||||
final deleteFixture = FixtureReader.readAsMap('blog_post_create/delete_request.json');
|
||||
expect(blogPostRepository.deleteCalls.single, deleteFixture['id']);
|
||||
expect(feedProbe.loadRequests, 1);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/features/create_diet/presentation/cubit/create_diet_cubit.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../../fixtures/doubles/create_diet_doubles.dart';
|
||||
import '../../fixtures/fixture_reader.dart';
|
||||
|
||||
void main() {
|
||||
group('Diet Creation Basic Widget Tests', () {
|
||||
@@ -12,8 +17,10 @@ void main() {
|
||||
late FixtureProfileRepository profileRepository;
|
||||
late FixtureGlossaryRepository glossaryRepository;
|
||||
late CreateDietCubit createDietCubit;
|
||||
late Map<String, dynamic> dietFixture;
|
||||
|
||||
setUp(() {
|
||||
dietFixture = FixtureReader.readAsMap('create_diet/create_diet_request.json');
|
||||
createDietRepository = FixtureCreateDietRepository();
|
||||
profileRepository = FixtureProfileRepository();
|
||||
glossaryRepository = FixtureGlossaryRepository();
|
||||
@@ -475,5 +482,78 @@ void main() {
|
||||
expect(find.text('• 21 день - трехнедельная диета'), findsOneWidget);
|
||||
expect(find.text('• 30 дней - месячная диета'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('should send correct request when creating diet', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
// Create a minimal valid JPEG file (1x1 pixel)
|
||||
final jpegHeader = [
|
||||
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01,
|
||||
0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x43,
|
||||
0x00, 0x08, 0x06, 0x06, 0x07, 0x06, 0x05, 0x08, 0x07, 0x07, 0x07, 0x09,
|
||||
0x09, 0x08, 0x0A, 0x0C, 0x14, 0x0D, 0x0C, 0x0B, 0x0B, 0x0C, 0x19, 0x12,
|
||||
0x13, 0x0F, 0x14, 0x1D, 0x1A, 0x1F, 0x1E, 0x1D, 0x1A, 0x1C, 0x1C, 0x20,
|
||||
0x24, 0x2E, 0x27, 0x20, 0x22, 0x2C, 0x23, 0x1C, 0x1C, 0x28, 0x37, 0x29,
|
||||
0x2C, 0x30, 0x31, 0x34, 0x34, 0x34, 0x1F, 0x27, 0x39, 0x3D, 0x38, 0x32,
|
||||
0x3C, 0x2E, 0x33, 0x34, 0x32, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0x01,
|
||||
0x00, 0x01, 0x01, 0x01, 0x11, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
|
||||
0xFF, 0xC4, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xFF, 0xC4,
|
||||
0x00, 0x14, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xDA, 0x00, 0x0C,
|
||||
0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3F, 0x00, 0x3F, 0xFF,
|
||||
0xD9
|
||||
];
|
||||
final tempFile = File('${Directory.systemTemp.path}/test_image.jpg');
|
||||
await tempFile.writeAsBytes(jpegHeader);
|
||||
final xFile = XFile(tempFile.path);
|
||||
|
||||
createDietCubit.setName(dietFixture['name'] as String);
|
||||
createDietCubit.setDetails(dietFixture['details'] as String);
|
||||
createDietCubit.setDuration(0); // 7 days - this creates dailySchedule with 7 days
|
||||
createDietCubit.setDietState(DietState.draft);
|
||||
|
||||
final types = dietFixture['types'] as Map<String, dynamic>;
|
||||
createDietCubit.setDietType(key: 'meat', value: types['meat'] as bool);
|
||||
createDietCubit.setDietType(key: 'carb', value: types['carb'] as bool);
|
||||
createDietCubit.setDietType(key: 'veg', value: types['veg'] as bool);
|
||||
createDietCubit.setDietType(key: 'light', value: types['light'] as bool);
|
||||
|
||||
// Add at least one product to each day to pass validation
|
||||
final glossaryResults = await glossaryRepository.getGlossaryListByQuery('test');
|
||||
if (glossaryResults.isNotEmpty) {
|
||||
final product = glossaryResults.first;
|
||||
final dailySchedule = DailySchedule(
|
||||
name: product.name,
|
||||
portion: Portion.grams,
|
||||
quantity: product.grInPortion,
|
||||
productNutritionalValueId: product.id.toString(),
|
||||
);
|
||||
// Add product to first meal of first day for all 7 days
|
||||
for (int day = 0; day < 7; day++) {
|
||||
createDietCubit.addPortion(dayIndex: day, mealIndex: 0, portion: dailySchedule);
|
||||
}
|
||||
}
|
||||
|
||||
createDietCubit.setImage(xFile, imageUrl: null);
|
||||
|
||||
// Act
|
||||
final result = await createDietCubit.createDiet();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Assert
|
||||
expect(result, isNull); // Should return null on success
|
||||
expect(createDietRepository.createRequests.length, 1);
|
||||
final request = createDietRepository.createRequests.single;
|
||||
expect(request['name'], dietFixture['name']);
|
||||
expect(request['details'], dietFixture['details']);
|
||||
expect(request['duration'], dietFixture['duration']);
|
||||
expect(request['state'], dietFixture['state']);
|
||||
expect(request['types'], dietFixture['types']);
|
||||
|
||||
// Cleanup
|
||||
if (await tempFile.exists()) {
|
||||
await tempFile.delete();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/features/create_plan/presentation/cubit/create_plan_cubit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import '../../fixtures/doubles/create_plan_doubles.dart';
|
||||
@@ -581,5 +584,45 @@ void main() {
|
||||
expect(find.byKey(const Key('basicOptionContainer')), findsOneWidget);
|
||||
expect(find.byKey(const Key('premiumOptionContainer')), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('should send correct request when creating plan', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
final tempFile = File('${Directory.systemTemp.path}/test_image.jpg');
|
||||
await tempFile.writeAsBytes([0, 1, 2, 3]);
|
||||
final xFile = XFile(tempFile.path);
|
||||
|
||||
createPlanCubit.setName(planFixture['name'] as String);
|
||||
createPlanCubit.setDetails(planFixture['description'] as String);
|
||||
createPlanCubit.setDuration(0); // 7 days
|
||||
createPlanCubit.setPlanState(planFixture['status'] as String);
|
||||
createPlanCubit.setImage(xFile);
|
||||
createPlanCubit.setPlanType([true, false, false, false]); // At least one type must be selected
|
||||
|
||||
// Set at least one option (required for validation)
|
||||
final options = planFixture['options'] as List<dynamic>;
|
||||
if (options.isNotEmpty) {
|
||||
final firstOption = options[0] as Map<String, dynamic>;
|
||||
createPlanCubit.setOptionTitle(firstOption['title'] as String);
|
||||
createPlanCubit.setPrice((firstOption['price'] as num).toDouble());
|
||||
createPlanCubit.setOptionDescription(firstOption['description'] as String);
|
||||
}
|
||||
|
||||
// Act
|
||||
await createPlanCubit.publishPlan();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Assert
|
||||
expect(createPlanRepository.createPlanRequests.length, 1);
|
||||
final request = createPlanRepository.createPlanRequests.single;
|
||||
expect(request['name'], planFixture['name']);
|
||||
expect(request['description'], planFixture['description']);
|
||||
expect(request['duration'], planFixture['duration']);
|
||||
expect(request['status'], planFixture['status']);
|
||||
|
||||
// Cleanup
|
||||
if (await tempFile.exists()) {
|
||||
await tempFile.delete();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user