forked from eatvio/eatvio_app
feat(consultant): Issue-420 + Tests
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:eatvio_app/core/app_initializer.dart';
|
||||
import 'package:eatvio_app/core/app_config.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
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_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:image/image.dart' as img;
|
||||
|
||||
import '../../fixtures/doubles/create_diet_doubles.dart';
|
||||
import '../../fixtures/fixture_reader.dart';
|
||||
@@ -19,7 +24,11 @@ void main() {
|
||||
late CreateDietCubit createDietCubit;
|
||||
late Map<String, dynamic> dietFixture;
|
||||
|
||||
setUp(() {
|
||||
setUp(() async {
|
||||
// Initialize AppConfig for tests to enable compression bypass
|
||||
await sl.reset();
|
||||
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
|
||||
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
|
||||
dietFixture = FixtureReader.readAsMap('create_diet/create_diet_request.json');
|
||||
createDietRepository = FixtureCreateDietRepository();
|
||||
profileRepository = FixtureProfileRepository();
|
||||
@@ -485,26 +494,12 @@ void main() {
|
||||
|
||||
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);
|
||||
// Create a valid JPEG file using image package (100x100 pixel image)
|
||||
final testImage = img.Image(width: 100, height: 100);
|
||||
img.fill(testImage, color: img.ColorRgb8(128, 128, 128));
|
||||
final jpegBytes = img.encodeJpg(testImage, quality: 85);
|
||||
final tempFile = File('${Directory.systemTemp.path}/test_image_${DateTime.now().millisecondsSinceEpoch}.jpg');
|
||||
await tempFile.writeAsBytes(jpegBytes);
|
||||
final xFile = XFile(tempFile.path);
|
||||
|
||||
createDietCubit.setName(dietFixture['name'] as String);
|
||||
@@ -537,8 +532,11 @@ void main() {
|
||||
createDietCubit.setImage(xFile, imageUrl: null);
|
||||
|
||||
// Act
|
||||
final result = await createDietCubit.createDiet();
|
||||
await tester.pumpAndSettle();
|
||||
final result = await createDietCubit.createDiet().timeout(
|
||||
const Duration(seconds: 10),
|
||||
);
|
||||
await tester.pump(); // Allow state changes to propagate
|
||||
await tester.pump(); // Additional pump for any delayed updates
|
||||
|
||||
// Assert
|
||||
expect(result, isNull); // Should return null on success
|
||||
@@ -550,10 +548,16 @@ void main() {
|
||||
expect(request['state'], dietFixture['state']);
|
||||
expect(request['types'], dietFixture['types']);
|
||||
|
||||
// Cleanup
|
||||
// Cleanup - remove original and compressed files
|
||||
if (await tempFile.exists()) {
|
||||
await tempFile.delete();
|
||||
}
|
||||
// ImageCompressUtility creates compressed file with _compressed suffix
|
||||
final filePathWithoutExtension = tempFile.path.replaceAll(RegExp(r'\.(jpg|jpeg)$', caseSensitive: false), '');
|
||||
final compressedFile = File('${filePathWithoutExtension}_compressed.jpg');
|
||||
if (await compressedFile.exists()) {
|
||||
await compressedFile.delete();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user