forked from eatvio/eatvio_app
feat(tests): add fixtures
This commit is contained in:
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user