diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f02219..76fb9a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.1.1 + +* Fixed `MediaInfo.fromJson` parsing and added unit tests + ## 3.1.0 * Updated Dart SDK constraint to `>=3.0.0 <4.0.0` diff --git a/lib/src/models/media_info.dart b/lib/src/models/media_info.dart index db1f08e..f5ce151 100644 --- a/lib/src/models/media_info.dart +++ b/lib/src/models/media_info.dart @@ -11,24 +11,22 @@ class MediaInfo { this.data, }); - /// Factory constructor to generate [MediaInfo] from [Map]. + /// Factory constructor to generate [MediaInfo] from a [Map]. factory MediaInfo.fromJson(Map json) { - switch (json) { - case { - 'name': final String? name, - 'base64': final String? base64, - 'data_scheme': final String? dataScheme, - 'data': final String data, - }: - return MediaInfo( - fileName: name, - base64: base64, - base64WithScheme: dataScheme, - data: base64Decode(data), - ); - default: - throw const FormatException('Invalid media info format'); + final name = json['name']; + final dataScheme = json['data_scheme']; + final data = json['data']; + + if (name is! String? || dataScheme is! String? || data is! String?) { + throw const FormatException('Invalid media info format'); } + + return MediaInfo( + fileName: name, + base64: data, + base64WithScheme: dataScheme, + data: data != null ? base64Decode(data) : null, + ); } /// Name of the file. @@ -43,12 +41,14 @@ class MediaInfo { /// File's bytes data. final Uint8List? data; - /// Convert [MediaInfo] to [Map] format - Map toJson() => { - 'name': fileName, - 'data': base64, - 'data_scheme': base64WithScheme, - }; + /// Convert [MediaInfo] to JSON format + Map toJson() { + return { + 'name': fileName, + 'data': base64, + 'data_scheme': base64WithScheme, + }; + } } /// Image's type. diff --git a/pubspec.yaml b/pubspec.yaml index 6cd67cd..6783ef5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: image_picker_web description: Flutter Web Plugin to pick Images (as Widget, File or Uint8List) and Videos (as File or Uint8List) -version: 3.1.0 +version: 3.1.1 repository: https://github.com/Ahmadre/image_picker_web environment: diff --git a/test/src/models/media_info_test.dart b/test/src/models/media_info_test.dart new file mode 100644 index 0000000..f42818f --- /dev/null +++ b/test/src/models/media_info_test.dart @@ -0,0 +1,34 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:image_picker_web/src/models/media_info.dart'; + +void main() { + group('MediaInfo', () { + group('fromJson', () { + test('parse full JSON', () { + const validJson = { + 'name': 'name', + 'data': 'data', + 'data_scheme': 'data_scheme', + }; + + final mediaInfo = MediaInfo.fromJson(validJson); + + expect(mediaInfo.fileName, validJson['name']); + expect(mediaInfo.base64, validJson['data']); + expect(mediaInfo.base64WithScheme, validJson['data_scheme']); + }); + + test('parse partial JSON', () { + const partialJson = { + 'name': 'name', + 'data': 'data', + }; + + final mediaInfo = MediaInfo.fromJson(partialJson); + + expect(mediaInfo.fileName, partialJson['name']); + expect(mediaInfo.base64, partialJson['data']); + }); + }); + }); +}