chore(llc): fix tests, minor changes

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-08-11 12:47:20 +05:30
committed by xsahil03x
parent 5e2c6975e2
commit b3a1d42f92
2 changed files with 73 additions and 76 deletions
@@ -73,14 +73,14 @@ class Channel {
this._client, this._client,
this._type, this._type,
this._id, { this._id, {
Map<String, Object?>? extraData,
String? image,
String? name, String? name,
String? image,
Map<String, Object?>? extraData,
}) : _cid = _id != null ? '$_type:$_id' : null, }) : _cid = _id != null ? '$_type:$_id' : null,
_extraData = { _extraData = {
...?extraData, ...?extraData,
if (image != null) 'image': image,
if (name != null) 'name': name, if (name != null) 'name': name,
if (image != null) 'image': image,
} { } {
_client.logger.info('New Channel instance created, not yet initialized'); _client.logger.info('New Channel instance created, not yet initialized');
} }
@@ -110,14 +110,17 @@ class Channel {
String? _cid; String? _cid;
final Map<String, Object?> _extraData; final Map<String, Object?> _extraData;
set extraData(Map<String, Object?> extraData) { /// Shortcut to set channel name.
///
/// {@macro name}
set name(String? name) {
if (_initializedCompleter.isCompleted) { if (_initializedCompleter.isCompleted) {
throw StateError( throw StateError(
'Once the channel is initialized you should use channel.update ' 'Once the channel is initialized you should use `channel.updateName` '
'to update channel data', 'to update the channel name',
); );
} }
_extraData.addAll(extraData); _extraData.addAll({'name': name});
} }
/// Shortcut to set channel image. /// Shortcut to set channel image.
@@ -126,24 +129,21 @@ class Channel {
set image(String? image) { set image(String? image) {
if (_initializedCompleter.isCompleted) { if (_initializedCompleter.isCompleted) {
throw StateError( throw StateError(
'Once the channel is initialized you should use channel.updateImage ' 'Once the channel is initialized you should use `channel.updateImage` '
'to update the channel image', 'to update the channel image',
); );
} }
_extraData.addAll({'image': image}); _extraData.addAll({'image': image});
} }
/// Shortcut to set channel name. set extraData(Map<String, Object?> extraData) {
///
/// {@macro name}
set name(String? name) {
if (_initializedCompleter.isCompleted) { if (_initializedCompleter.isCompleted) {
throw StateError( throw StateError(
'Once the channel is initialized you should use channel.updateName ' 'Once the channel is initialized you should use `channel.update` '
'to update the channel name', 'to update channel data',
); );
} }
_extraData.addAll({'name': name}); _extraData.addAll(extraData);
} }
/// Returns true if the channel is muted. /// Returns true if the channel is muted.
@@ -301,23 +301,6 @@ class Channel {
); );
} }
/// Shortcut to get channel image.
///
/// {@macro image}
String? get image => extraData['image'] as String?;
/// Channel [image] as a stream.
///
/// The channel needs to be initialized.
///
/// {@macro image}
Stream<String?> get imageStream {
_checkInitialized();
return state!.channelStateStream.map(
(cs) => (cs.channel?.extraData['image'] as String?) ?? image,
);
}
/// Shortcut to get channel name. /// Shortcut to get channel name.
/// ///
/// If no name is set this returns the channel cid, else null. /// If no name is set this returns the channel cid, else null.
@@ -342,15 +325,34 @@ class Channel {
_checkInitialized(); _checkInitialized();
return state!.channelStateStream.map( return state!.channelStateStream.map(
(cs) { (cs) {
if (cs.channel?.extraData.containsKey('name') ?? false) { final extraData = cs.channel?.extraData;
final name = cs.channel!.extraData['name']! as String; if (extraData != null && extraData.containsKey('name')) {
final name = extraData['name']! as String;
if (name.isNotEmpty) return name; if (name.isNotEmpty) return name;
} }
// this can never be null once the channel is initialized
return name!; return name!;
}, },
); );
} }
/// Shortcut to get channel image.
///
/// {@macro image}
String? get image => extraData['image'] as String?;
/// Channel [image] as a stream.
///
/// The channel needs to be initialized.
///
/// {@macro image}
Stream<String?> get imageStream {
_checkInitialized();
return state!.channelStateStream.map(
(cs) => (cs.channel?.extraData['image'] as String?) ?? image,
);
}
/// The main Stream chat client. /// The main Stream chat client.
StreamChatClient get client => _client; StreamChatClient get client => _client;
final StreamChatClient _client; final StreamChatClient _client;
@@ -932,26 +934,6 @@ class Channel {
} }
} }
/// Update the channel's [image].
///
/// This is the same as calling [updatePartial] and providing a map with an
/// 'image' key:
///
/// ```dart
/// channel.updatePartial(
/// set: {'image': 'https://getstream.io/new-image'}
/// );
/// ```
///
/// Instead do:
/// ```dart
/// channel.updateImage('https://getstream.io/new-image');
/// ```
Future<PartialUpdateChannelResponse> updateImage(
String image,
) =>
updatePartial(set: {'image': image});
/// Update the channel's [name]. /// Update the channel's [name].
/// ///
/// This is the same as calling [updatePartial] and providing a map with a /// This is the same as calling [updatePartial] and providing a map with a
@@ -967,11 +949,27 @@ class Channel {
/// ```dart /// ```dart
/// channel.updateName('Updated channel name'); /// channel.updateName('Updated channel name');
/// ``` /// ```
Future<PartialUpdateChannelResponse> updateName( Future<PartialUpdateChannelResponse> updateName(String name) =>
String name,
) =>
updatePartial(set: {'name': name}); updatePartial(set: {'name': name});
/// Update the channel's [image].
///
/// This is the same as calling [updatePartial] and providing a map with an
/// 'image' key:
///
/// ```dart
/// channel.updatePartial(
/// set: {'image': 'https://getstream.io/new-image'}
/// );
/// ```
///
/// Instead do:
/// ```dart
/// channel.updateImage('https://getstream.io/new-image');
/// ```
Future<PartialUpdateChannelResponse> updateImage(String image) =>
updatePartial(set: {'image': image});
/// Update the channel custom data. This replaces all of the channel data /// Update the channel custom data. This replaces all of the channel data
/// with the given [channelData]. /// with the given [channelData].
/// ///
@@ -39,6 +39,7 @@ void main() {
late final client = MockStreamChatClient(); late final client = MockStreamChatClient();
const channelId = 'test-channel-id'; const channelId = 'test-channel-id';
const channelType = 'test-channel-type'; const channelType = 'test-channel-type';
const channelCid = '$channelType:$channelId';
late Channel channel; late Channel channel;
setUpAll(() { setUpAll(() {
@@ -94,7 +95,7 @@ void main() {
expect(channel.extraData.isEmpty, isTrue); expect(channel.extraData.isEmpty, isTrue);
expect( expect(
channel.name, channel.name,
channelId, channelCid,
reason: 'if name is not set then use channel id', reason: 'if name is not set then use channel id',
); );
@@ -1256,24 +1257,23 @@ void main() {
); );
when(() => client.updateChannelPartial( when(() => client.updateChannelPartial(
any(), channelId,
any(), channelType,
set: {'image': image}, set: {'image': image},
)).thenAnswer( )).thenAnswer(
(_) async => PartialUpdateChannelResponse()..channel = channelModel, (_) async => PartialUpdateChannelResponse()..channel = channelModel,
); );
final res = await channel.updateImage(image); final res = await channel.updateImage(image);
expect(res, isNotNull); expect(res, isNotNull);
expect(res.channel.extraData['image'], image); expect(res.channel.extraData['image'], image);
verify( verify(() => client.updateChannelPartial(
() => client.updateChannelPartial( channelId,
any(), channelType,
any(), set: {'image': image},
set: {'image': image}, )).called(1);
),
).called(1);
}); });
test('`.updateName`', () async { test('`.updateName`', () async {
@@ -1285,24 +1285,23 @@ void main() {
); );
when(() => client.updateChannelPartial( when(() => client.updateChannelPartial(
any(), channelId,
any(), channelType,
set: {'name': name}, set: {'name': name},
)).thenAnswer( )).thenAnswer(
(_) async => PartialUpdateChannelResponse()..channel = channelModel, (_) async => PartialUpdateChannelResponse()..channel = channelModel,
); );
final res = await channel.updateName(name); final res = await channel.updateName(name);
expect(res, isNotNull); expect(res, isNotNull);
expect(res.channel.extraData['name'], name); expect(res.channel.extraData['name'], name);
verify( verify(() => client.updateChannelPartial(
() => client.updateChannelPartial( channelId,
any(), channelType,
any(), set: {'name': name},
set: {'name': name}, )).called(1);
),
).called(1);
}); });
test('`.updatePartial`', () async { test('`.updatePartial`', () async {