diff --git a/packages/stream_chat/lib/src/core/models/attachment.dart b/packages/stream_chat/lib/src/core/models/attachment.dart index a5f9f19d..8973aee2 100644 --- a/packages/stream_chat/lib/src/core/models/attachment.dart +++ b/packages/stream_chat/lib/src/core/models/attachment.dart @@ -4,7 +4,7 @@ import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:stream_chat/src/core/models/action.dart'; import 'package:stream_chat/src/core/models/attachment_file.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; import 'package:uuid/uuid.dart'; part 'attachment.g.dart'; @@ -49,11 +49,11 @@ class Attachment extends Equatable { /// Create a new instance from a json factory Attachment.fromJson(Map json) => _$AttachmentFromJson( - Serialization.moveToExtraDataFromRoot(json, topLevelFields)); + Serializer.moveToExtraDataFromRoot(json, topLevelFields)); /// Create a new instance from a db data factory Attachment.fromData(Map json) => - _$AttachmentFromJson(Serialization.moveToExtraDataFromRoot( + _$AttachmentFromJson(Serializer.moveToExtraDataFromRoot( json, topLevelFields + dbSpecificTopLevelFields)); ///The attachment type based on the URL resource. This can be: audio, @@ -122,7 +122,7 @@ class Attachment extends Equatable { final String id; /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static const topLevelFields = [ 'type', 'title_link', @@ -145,7 +145,7 @@ class Attachment extends Equatable { ]; /// Known db specific top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static const dbSpecificTopLevelFields = [ 'id', 'upload_state', @@ -154,12 +154,12 @@ class Attachment extends Equatable { /// Serialize to json Map toJson() => - Serialization.moveFromExtraDataToRoot(_$AttachmentToJson(this)) + Serializer.moveFromExtraDataToRoot(_$AttachmentToJson(this)) ..removeWhere((key, value) => dbSpecificTopLevelFields.contains(key)); /// Serialize to db data Map toData() => - Serialization.moveFromExtraDataToRoot(_$AttachmentToJson(this)); + Serializer.moveFromExtraDataToRoot(_$AttachmentToJson(this)); Attachment copyWith({ String? id, diff --git a/packages/stream_chat/lib/src/core/models/channel_model.dart b/packages/stream_chat/lib/src/core/models/channel_model.dart index 9e50ed3e..74195c71 100644 --- a/packages/stream_chat/lib/src/core/models/channel_model.dart +++ b/packages/stream_chat/lib/src/core/models/channel_model.dart @@ -1,6 +1,6 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:stream_chat/src/core/models/channel_config.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; import 'package:stream_chat/src/core/models/user.dart'; part 'channel_model.g.dart'; @@ -37,7 +37,7 @@ class ChannelModel { /// Create a new instance from a json factory ChannelModel.fromJson(Map json) => _$ChannelModelFromJson( - Serialization.moveToExtraDataFromRoot(json, topLevelFields)); + Serializer.moveToExtraDataFromRoot(json, topLevelFields)); /// The id of this channel final String id; @@ -46,15 +46,15 @@ class ChannelModel { final String type; /// The cid of this channel - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final String cid; /// The channel configuration data - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final ChannelConfig config; /// The user that created this channel - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final User? createdBy; /// True if this channel is frozen @@ -62,24 +62,24 @@ class ChannelModel { final bool frozen; /// The date of the last message - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime? lastMessageAt; /// The date of channel creation - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime createdAt; /// The date of the last channel update - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime updatedAt; /// The date of channel deletion - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime? deletedAt; /// The count of this channel members @JsonKey( - includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0) + includeIfNull: false, toJson: Serializer.readOnly, defaultValue: 0) final int memberCount; /// Map of custom channel extraData @@ -90,11 +90,11 @@ class ChannelModel { final Map extraData; /// The team the channel belongs to - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final String? team; /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static const topLevelFields = [ 'id', 'type', @@ -115,7 +115,7 @@ class ChannelModel { extraData.containsKey('name') ? extraData['name']! as String : cid; /// Serialize to json - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$ChannelModelToJson(this), ); diff --git a/packages/stream_chat/lib/src/core/models/event.dart b/packages/stream_chat/lib/src/core/models/event.dart index 7c1be679..33e696c4 100644 --- a/packages/stream_chat/lib/src/core/models/event.dart +++ b/packages/stream_chat/lib/src/core/models/event.dart @@ -1,7 +1,7 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:stream_chat/src/core/models/channel_model.dart'; import 'package:stream_chat/src/core/models/message.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; import 'package:stream_chat/stream_chat.dart'; part 'event.g.dart'; @@ -33,7 +33,7 @@ class Event { /// Create a new instance from a json factory Event.fromJson(Map json) => - _$EventFromJson(Serialization.moveToExtraDataFromRoot( + _$EventFromJson(Serializer.moveToExtraDataFromRoot( json, topLevelFields, )); @@ -96,7 +96,7 @@ class Event { final Map extraData; /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static final topLevelFields = [ 'type', 'cid', @@ -118,7 +118,7 @@ class Event { ]; /// Serialize to json - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$EventToJson(this), ); @@ -198,7 +198,7 @@ class EventChannel extends ChannelModel { /// Create a new instance from a json factory EventChannel.fromJson(Map json) => - _$EventChannelFromJson(Serialization.moveToExtraDataFromRoot( + _$EventChannelFromJson(Serializer.moveToExtraDataFromRoot( json, topLevelFields, )); @@ -207,7 +207,7 @@ class EventChannel extends ChannelModel { final List? members; /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static final topLevelFields = [ 'members', ...ChannelModel.topLevelFields, @@ -215,7 +215,7 @@ class EventChannel extends ChannelModel { /// Serialize to json @override - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$EventChannelToJson(this), ); } diff --git a/packages/stream_chat/lib/src/core/models/message.dart b/packages/stream_chat/lib/src/core/models/message.dart index 1266519c..426b863a 100644 --- a/packages/stream_chat/lib/src/core/models/message.dart +++ b/packages/stream_chat/lib/src/core/models/message.dart @@ -2,7 +2,7 @@ import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:stream_chat/src/core/models/attachment.dart'; import 'package:stream_chat/src/core/models/reaction.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; import 'package:stream_chat/src/core/models/user.dart'; import 'package:uuid/uuid.dart'; @@ -81,7 +81,7 @@ class Message extends Equatable { /// Create a new instance from a json factory Message.fromJson(Map json) => _$MessageFromJson( - Serialization.moveToExtraDataFromRoot(json, topLevelFields)); + Serializer.moveToExtraDataFromRoot(json, topLevelFields)); /// The message ID. This is either created by Stream or set client side when /// the message is added. @@ -97,7 +97,7 @@ class Message extends Equatable { /// The message type @JsonKey( includeIfNull: false, - toJson: Serialization.readOnly, + toJson: Serializer.readOnly, defaultValue: 'regular', ) final String type; @@ -118,37 +118,37 @@ class Message extends Equatable { final List mentionedUsers; /// A map describing the count of number of every reaction - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final Map? reactionCounts; /// A map describing the count of score of every reaction - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final Map? reactionScores; /// The latest reactions to the message created by any user. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final List? latestReactions; /// The reactions added to the message by the current user. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final List? ownReactions; /// The ID of the parent message, if the message is a thread reply. final String? parentId; /// A quoted reply message - @JsonKey(toJson: Serialization.readOnly) + @JsonKey(toJson: Serializer.readOnly) final Message? quotedMessage; /// The ID of the quoted message, if the message is a quoted reply. final String? quotedMessageId; /// Reserved field indicating the number of replies for this message. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final int? replyCount; /// Reserved field indicating the thread participants for this message. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final List? threadParticipants; /// Check if this message needs to show in the channel. @@ -165,25 +165,25 @@ class Message extends Equatable { /// If true the message is shadowed @JsonKey( includeIfNull: false, - toJson: Serialization.readOnly, + toJson: Serializer.readOnly, defaultValue: false, ) final bool shadowed; /// A used command name. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final String? command; /// Reserved field indicating when the message was created. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime createdAt; /// Reserved field indicating when the message was updated last time. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime updatedAt; /// User who sent the message - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final User? user; /// If true the message is pinned @@ -191,7 +191,7 @@ class Message extends Equatable { final bool pinned; /// Reserved field indicating when the message was pinned - @JsonKey(toJson: Serialization.readOnly) + @JsonKey(toJson: Serializer.readOnly) final DateTime? pinnedAt; /// Reserved field indicating when the message will expire @@ -200,7 +200,7 @@ class Message extends Equatable { final DateTime? pinExpires; /// Reserved field indicating who pinned the message - @JsonKey(toJson: Serialization.readOnly) + @JsonKey(toJson: Serializer.readOnly) final User? pinnedBy; /// Message custom extraData @@ -220,11 +220,11 @@ class Message extends Equatable { bool get isEphemeral => type == 'ephemeral'; /// Reserved field indicating when the message was deleted. - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime? deletedAt; /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static const topLevelFields = [ 'id', 'text', @@ -257,7 +257,7 @@ class Message extends Equatable { ]; /// Serialize to json - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$MessageToJson(this), ); @@ -412,14 +412,14 @@ class TranslatedMessage extends Message { /// Create a new instance from a json factory TranslatedMessage.fromJson(Map json) => _$TranslatedMessageFromJson( - Serialization.moveToExtraDataFromRoot(json, topLevelFields), + Serializer.moveToExtraDataFromRoot(json, topLevelFields), ); /// A Map of final Map? i18n; /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static final topLevelFields = [ 'i18n', ...Message.topLevelFields, @@ -427,7 +427,7 @@ class TranslatedMessage extends Message { /// Serialize to json @override - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$TranslatedMessageToJson(this), ); } diff --git a/packages/stream_chat/lib/src/core/models/mute.dart b/packages/stream_chat/lib/src/core/models/mute.dart index 7a82fc54..9166d3e4 100644 --- a/packages/stream_chat/lib/src/core/models/mute.dart +++ b/packages/stream_chat/lib/src/core/models/mute.dart @@ -1,6 +1,6 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:stream_chat/src/core/models/channel_model.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; import 'package:stream_chat/src/core/models/user.dart'; part 'mute.g.dart'; @@ -20,19 +20,19 @@ class Mute { factory Mute.fromJson(Map json) => _$MuteFromJson(json); /// The user that performed the muting action - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final User user; /// The target user - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final ChannelModel channel; /// The date in which the use was muted - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime createdAt; /// The date of the last update - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime updatedAt; /// Serialize to json diff --git a/packages/stream_chat/lib/src/core/models/own_user.dart b/packages/stream_chat/lib/src/core/models/own_user.dart index b9cdf0fd..70529687 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.dart @@ -1,7 +1,7 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:stream_chat/src/core/models/device.dart'; import 'package:stream_chat/src/core/models/mute.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; import 'package:stream_chat/src/core/models/user.dart'; part 'own_user.g.dart'; @@ -38,7 +38,7 @@ class OwnUser extends User { /// Create a new instance from a json factory OwnUser.fromJson(Map json) => _$OwnUserFromJson( - Serialization.moveToExtraDataFromRoot(json, topLevelFields)); + Serializer.moveToExtraDataFromRoot(json, topLevelFields)); /// Create a new instance from [User] object factory OwnUser.fromUser(User user) => OwnUser( @@ -55,35 +55,35 @@ class OwnUser extends User { /// List of user devices @JsonKey( includeIfNull: false, - toJson: Serialization.readOnly, + toJson: Serializer.readOnly, defaultValue: []) final List devices; /// List of users muted by the user @JsonKey( includeIfNull: false, - toJson: Serialization.readOnly, + toJson: Serializer.readOnly, defaultValue: []) final List mutes; /// List of users muted by the user @JsonKey( includeIfNull: false, - toJson: Serialization.readOnly, + toJson: Serializer.readOnly, defaultValue: []) final List channelMutes; /// Total unread messages by the user @JsonKey( - includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0) + includeIfNull: false, toJson: Serializer.readOnly, defaultValue: 0) final int totalUnreadCount; /// Total unread channels by the user - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final int? unreadChannels; /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static final topLevelFields = [ 'devices', 'mutes', @@ -95,7 +95,7 @@ class OwnUser extends User { /// Serialize to json @override - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$OwnUserToJson(this), ); } diff --git a/packages/stream_chat/lib/src/core/models/reaction.dart b/packages/stream_chat/lib/src/core/models/reaction.dart index 997d833c..57d0b48d 100644 --- a/packages/stream_chat/lib/src/core/models/reaction.dart +++ b/packages/stream_chat/lib/src/core/models/reaction.dart @@ -1,5 +1,5 @@ import 'package:json_annotation/json_annotation.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; import 'package:stream_chat/src/core/models/user.dart'; part 'reaction.g.dart'; @@ -21,7 +21,7 @@ class Reaction { /// Create a new instance from a json factory Reaction.fromJson(Map json) => - _$ReactionFromJson(Serialization.moveToExtraDataFromRoot( + _$ReactionFromJson(Serializer.moveToExtraDataFromRoot( json, topLevelFields, )); @@ -33,11 +33,11 @@ class Reaction { final String type; /// The date of the reaction - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime createdAt; /// The user that sent the reaction - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final User? user; /// The score of the reaction (ie. number of reactions sent) @@ -45,7 +45,7 @@ class Reaction { final int score; /// The userId that sent the reaction - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final String? userId; /// Reaction custom extraData @@ -66,7 +66,7 @@ class Reaction { ]; /// Serialize to json - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$ReactionToJson(this), ); diff --git a/packages/stream_chat/lib/src/core/models/user.dart b/packages/stream_chat/lib/src/core/models/user.dart index b5fdec69..b1840ba2 100644 --- a/packages/stream_chat/lib/src/core/models/user.dart +++ b/packages/stream_chat/lib/src/core/models/user.dart @@ -1,5 +1,5 @@ import 'package:json_annotation/json_annotation.dart'; -import 'package:stream_chat/src/core/util/serialization.dart'; +import 'package:stream_chat/src/core/util/serializer.dart'; part 'user.g.dart'; @@ -22,10 +22,10 @@ class User { /// Create a new instance from a json factory User.fromJson(Map json) => _$UserFromJson( - Serialization.moveToExtraDataFromRoot(json, topLevelFields)); + Serializer.moveToExtraDataFromRoot(json, topLevelFields)); /// Known top level fields. - /// Useful for [Serialization] methods. + /// Useful for [Serializer] methods. static const topLevelFields = [ 'id', 'role', @@ -41,36 +41,36 @@ class User { final String id; /// User role - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final String? role; /// User role @JsonKey( includeIfNull: false, - toJson: Serialization.readOnly, + toJson: Serializer.readOnly, defaultValue: []) final List teams; /// Date of user creation - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime createdAt; /// Date of last user update - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime updatedAt; /// Date of last user connection - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final DateTime? lastActive; /// True if user is online @JsonKey( - includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false) + includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false) final bool online; /// True if user is banned from the chat @JsonKey( - includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false) + includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false) final bool banned; /// Map of custom user extraData @@ -102,7 +102,7 @@ class User { other is User && runtimeType == other.runtimeType && id == other.id; /// Serialize to json - Map toJson() => Serialization.moveFromExtraDataToRoot( + Map toJson() => Serializer.moveFromExtraDataToRoot( _$UserToJson(this), ); diff --git a/packages/stream_chat/lib/src/core/util/extension.dart b/packages/stream_chat/lib/src/core/util/extension.dart index 5dd8ea15..a28dc12b 100644 --- a/packages/stream_chat/lib/src/core/util/extension.dart +++ b/packages/stream_chat/lib/src/core/util/extension.dart @@ -9,10 +9,13 @@ extension IterableX on Iterable { } /// Useful extension functions for [Map] -extension MapX on Map { +extension MapX on Map { /// Returns a new map with null keys or values removed - Map get nullProtected => - Map.from(this)..removeWhere((key, value) => key == null || value == null); + Map get nullProtected { + final nullProtected = {...this} + ..removeWhere((key, value) => key == null || value == null); + return nullProtected.cast(); + } } /// Useful extension functions for [String] diff --git a/packages/stream_chat/lib/src/core/util/serialization.dart b/packages/stream_chat/lib/src/core/util/serializer.dart similarity index 98% rename from packages/stream_chat/lib/src/core/util/serialization.dart rename to packages/stream_chat/lib/src/core/util/serializer.dart index 1e933a32..2bf92e39 100644 --- a/packages/stream_chat/lib/src/core/util/serialization.dart +++ b/packages/stream_chat/lib/src/core/util/serializer.dart @@ -3,7 +3,7 @@ Null readonly(_) => null; /// Helper class for serialization to and from json -class Serialization { +class Serializer { /// Used to avoid to serialize properties to json static const Function readOnly = readonly; diff --git a/packages/stream_chat/test/src/core/util/extension_test.dart b/packages/stream_chat/test/src/core/util/extension_test.dart new file mode 100644 index 00000000..98f9f784 --- /dev/null +++ b/packages/stream_chat/test/src/core/util/extension_test.dart @@ -0,0 +1,48 @@ +import 'package:test/test.dart'; +import 'package:stream_chat/src/core/util/extension.dart'; + +void main() { + test('`.withNullifyer` converts the type into non-nullable', () { + final items = ['A', 'B', null, 'D']; + expect(items, isA>()); + expect(items.length, 4); + + final nullifiedItems = items.withNullifyer; + expect(nullifiedItems, isA>()); + expect(nullifiedItems.length, 3); + }); + + test('`.nullProtected should remove all the null keys, value`', () { + final map = {'name': 'sahil', 'age': null, null: 'India'}; + expect(map, isA>()); + expect(map.length, 3); + + final nullProtectedMap = map.nullProtected; + expect(nullProtectedMap, isA>()); + expect(nullProtectedMap.length, 1); + }); + + group('mimeType', () { + test('should return null if `String` is not a filename', () { + const fileName = 'not-a-file-name'; + final mimeType = fileName.mimeType; + expect(mimeType, isNull); + }); + + test('should return mimeType if string is a filename', () { + const fileName = 'dummyFileName.jpeg'; + final mimeType = fileName.mimeType; + expect(mimeType, isNotNull); + expect(mimeType!.type, 'image'); + expect(mimeType.subtype, 'jpeg'); + }); + + test('should return `image/heic` if ends with `heic`', () { + const fileName = 'dummyFileName.heic'; + final mimeType = fileName.mimeType; + expect(mimeType, isNotNull); + expect(mimeType!.type, 'image'); + expect(mimeType.subtype, 'heic'); + }); + }); +} diff --git a/packages/stream_chat/test/src/core/util/serializer_test.dart b/packages/stream_chat/test/src/core/util/serializer_test.dart new file mode 100644 index 00000000..fbbb96a2 --- /dev/null +++ b/packages/stream_chat/test/src/core/util/serializer_test.dart @@ -0,0 +1,43 @@ +import 'package:stream_chat/src/core/util/serializer.dart'; +import 'package:test/test.dart'; + +void main() { + group('Serializer', () { + test('moveKeysToMapInPlace', () { + final serializer = Serializer.moveToExtraDataFromRoot( + { + 'test': 'test', + 'name': 'Sahil', + 'age': 22, + 'country': 'India', + }, + ['test'], + ); + expect(serializer, { + 'test': 'test', + 'extra_data': { + 'name': 'Sahil', + 'age': 22, + 'country': 'India', + } + }); + }); + + test('moveKeysToMapInPlace', () { + final serializer = Serializer.moveFromExtraDataToRoot({ + 'test': 'test', + 'extra_data': { + 'name': 'Sahil', + 'age': 22, + 'country': 'India', + } + }); + expect(serializer, { + 'test': 'test', + 'name': 'Sahil', + 'age': 22, + 'country': 'India', + }); + }); + }); +} diff --git a/packages/stream_chat/test/src/core/util/utils_test.dart b/packages/stream_chat/test/src/core/util/utils_test.dart new file mode 100644 index 00000000..5d4c272f --- /dev/null +++ b/packages/stream_chat/test/src/core/util/utils_test.dart @@ -0,0 +1,15 @@ +import 'package:stream_chat/src/core/util/utils.dart'; +import 'package:test/test.dart'; + +void main() { + test('should generate a `randomId` of length 33', () { + final id = randomId(size: 33); + expect(id.length, 33); + }); + + test('should `generateHash` for the passed objects', () { + final objects = ['Sahil', 23, 'Flutter Engineer', 'India']; + final hash = generateHash(objects); + expect(hash, 'WyJTYWhpbCIsMjMsIkZsdXR0ZXIgRW5naW5lZXIiLCJJbmRpYSJd'); + }); +}