From b78aea6103da18b557e73b4abed2064803d2aeb7 Mon Sep 17 00:00:00 2001 From: dab246 Date: Fri, 7 Mar 2025 15:17:24 +0700 Subject: [PATCH] TF-3514 Add unit test for `getMediaTypeFromBase64ImageTag` method Signed-off-by: dab246 Signed-off-by: dab246 --- core/test/utils/string_convert_test.dart | 47 +++++++++++++++++++ .../controller/upload_controller.dart | 9 ++++ .../composer_repository_impl_test.dart | 3 ++ 3 files changed, 59 insertions(+) diff --git a/core/test/utils/string_convert_test.dart b/core/test/utils/string_convert_test.dart index 5842b1d4b..d51ed847a 100644 --- a/core/test/utils/string_convert_test.dart +++ b/core/test/utils/string_convert_test.dart @@ -283,4 +283,51 @@ void main() { ); }); }); + + group('StringConvert::getMediaTypeFromBase64ImageTag::', () { + test('should return correct MediaType for valid JPEG base64 tag', () { + const validJpegTag = 'data:image/jpeg;base64,/9j/4AAQSkZJRg=='; + final result = StringConvert.getMediaTypeFromBase64ImageTag(validJpegTag); + expect(result, isNotNull); + expect(result!.type, 'image'); + expect(result.subtype, 'jpeg'); + }); + + test('should return correct MediaType for valid PNG base64 tag', () { + const validPngTag = 'data:image/png;base64,iVBORw0KGgo==='; + final result = StringConvert.getMediaTypeFromBase64ImageTag(validPngTag); + expect(result, isNotNull); + expect(result!.type, 'image'); + expect(result.subtype, 'png'); + }); + + test('should return null for string not starting with "data:"', () { + const invalidTag = 'image/jpeg;base64,/9j/4AAQSkZJRg=='; + final result = StringConvert.getMediaTypeFromBase64ImageTag(invalidTag); + expect(result, isNull); + }); + + test('should return null for string without ";base64,"', () { + const invalidTag = 'data:image/jpeg,/9j/4AAQSkZJRg=='; + final result = StringConvert.getMediaTypeFromBase64ImageTag(invalidTag); + expect(result, isNull); + }); + + test('should return null for invalid format', () { + const invalidTag = 'data:invalid;base64,data'; + final result = StringConvert.getMediaTypeFromBase64ImageTag(invalidTag); + expect(result, isNull); + }); + + test('should return null for empty string', () { + const emptyTag = ''; + final result = StringConvert.getMediaTypeFromBase64ImageTag(emptyTag); + expect(result, isNull); + }); + + test('should handle null input gracefully', () { + const String? nullTag = null; + expect(() => StringConvert.getMediaTypeFromBase64ImageTag(nullTag!), throwsA(isA())); + }); + }); } diff --git a/lib/features/upload/presentation/controller/upload_controller.dart b/lib/features/upload/presentation/controller/upload_controller.dart index f107aad90..0df28807b 100644 --- a/lib/features/upload/presentation/controller/upload_controller.dart +++ b/lib/features/upload/presentation/controller/upload_controller.dart @@ -428,6 +428,9 @@ class UploadController extends BaseController { } List get inlineAttachmentsUploaded { + if (_uploadingStateInlineFiles.uploadingStateFiles.isEmpty) { + return []; + } return _uploadingStateInlineFiles.uploadingStateFiles.fold>( [], (list, fileState) { @@ -441,6 +444,9 @@ class UploadController extends BaseController { } List get inlineAttachmentsPicked { + if (_uploadingStateInlineFiles.uploadingStateFiles.isEmpty) { + return []; + } return _uploadingStateInlineFiles.uploadingStateFiles.fold>( [], (list, fileState) { @@ -454,6 +460,9 @@ class UploadController extends BaseController { } Map get mapInlineAttachments { + if (_uploadingStateInlineFiles.uploadingStateFiles.isEmpty) { + return {}; + } final mapInlineAttachments = _uploadingStateInlineFiles.uploadingStateFiles.fold>( {}, (map, fileState) { diff --git a/test/features/composer/data/repository/composer_repository_impl_test.dart b/test/features/composer/data/repository/composer_repository_impl_test.dart index 3a4a1e81e..a2c677652 100644 --- a/test/features/composer/data/repository/composer_repository_impl_test.dart +++ b/test/features/composer/data/repository/composer_repository_impl_test.dart @@ -102,6 +102,7 @@ void main() { when(mockCreateEmailRequest.outboxMailboxId).thenReturn(null); when(mockCreateEmailRequest.emailActionType).thenReturn(EmailActionType.compose); when(mockCreateEmailRequest.hasRequestReadReceipt).thenReturn(false); + when(mockCreateEmailRequest.isMarkAsImportant).thenReturn(false); when(mockHtmlDataSource.replaceImageBase64ToImageCID( emailContent: emailContentWithBase64, @@ -158,6 +159,7 @@ void main() { when(mockCreateEmailRequest.outboxMailboxId).thenReturn(null); when(mockCreateEmailRequest.emailActionType).thenReturn(EmailActionType.compose); when(mockCreateEmailRequest.hasRequestReadReceipt).thenReturn(false); + when(mockCreateEmailRequest.isMarkAsImportant).thenReturn(false); when(mockHtmlDataSource.replaceImageBase64ToImageCID( emailContent: emailContentWithBase64, @@ -211,6 +213,7 @@ void main() { when(mockCreateEmailRequest.outboxMailboxId).thenReturn(null); when(mockCreateEmailRequest.emailActionType).thenReturn(EmailActionType.compose); when(mockCreateEmailRequest.hasRequestReadReceipt).thenReturn(false); + when(mockCreateEmailRequest.isMarkAsImportant).thenReturn(false); when(mockHtmlDataSource.replaceImageBase64ToImageCID( emailContent: plainEmailContent,