From 0030de3dd6b0dad03f891cc6c8b56f606e807c50 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Tue, 2 Feb 2021 17:23:18 +0530 Subject: [PATCH] [LLC] Fix reactions tests Signed-off-by: Sahil Kumar --- packages/stream_chat/lib/src/api/channel.dart | 30 +++++++++---------- .../test/src/api/channel_test.dart | 21 ++++++------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/packages/stream_chat/lib/src/api/channel.dart b/packages/stream_chat/lib/src/api/channel.dart index e5bf528c..a7034dfd 100644 --- a/packages/stream_chat/lib/src/api/channel.dart +++ b/packages/stream_chat/lib/src/api/channel.dart @@ -329,11 +329,9 @@ class Channel { _client.decode(res.data, SendReactionResponse.fromJson); state?.addMessage(reactionResp.message); return reactionResp; - } catch (error) { - if (error is DioError && error.type != DioErrorType.RESPONSE) { - // Reset the message if the update fails - state?.addMessage(message); - } + } catch (_) { + // Reset the message if the update fails + state?.addMessage(message); rethrow; } } @@ -341,14 +339,16 @@ class Channel { /// Delete a reaction from this channel Future deleteReaction( Message message, Reaction reaction) async { - _checkInitialized(); - final type = reaction.type; - final reactionCounts = {...message.reactionCounts} - ..update(type, (value) => value - 1); - final reactionScores = {...message.reactionScores} - ..update(type, (value) => value - 1); + final reactionCounts = {...message.reactionCounts}; + if (reactionCounts.containsKey(type)) { + reactionCounts.update(type, (value) => value - 1); + } + final reactionScores = {...message.reactionScores}; + if (reactionScores.containsKey(type)) { + reactionScores.update(type, (value) => value - 1); + } final removeWhere = (Reaction r) => r.userId == reaction.userId && @@ -368,11 +368,9 @@ class Channel { final res = await client .delete('/messages/${message.id}/reaction/${reaction.type}'); return _client.decode(res.data, EmptyResponse.fromJson); - } catch (error) { - if (error is DioError && error.type != DioErrorType.RESPONSE) { - // Reset the message if the update fails - state?.addMessage(message); - } + } catch (_) { + // Reset the message if the update fails + state?.addMessage(message); rethrow; } } diff --git a/packages/stream_chat/test/src/api/channel_test.dart b/packages/stream_chat/test/src/api/channel_test.dart index 4b274a94..9baf4235 100644 --- a/packages/stream_chat/test/src/api/channel_test.dart +++ b/packages/stream_chat/test/src/api/channel_test.dart @@ -396,6 +396,10 @@ void main() { await channelClient.sendReaction( Message( id: 'messageid', + reactionCounts: const {}, + reactionScores: const {}, + latestReactions: const [], + ownReactions: const [], ), reactionType, ); @@ -421,20 +425,17 @@ void main() { ); final channelClient = client.channel('messaging', id: 'testid'); - when(mockDio.post( - any, - data: anyNamed('data'), - )).thenAnswer((_) async => Response( - data: '{}', - statusCode: 200, - )); - await channelClient.watch(); - when(mockDio.delete('/messages/messageid/reaction/test')) .thenAnswer((_) async => Response(data: '{}', statusCode: 200)); await channelClient.deleteReaction( - Message(id: 'messageid'), + Message( + id: 'messageid', + reactionCounts: const {}, + reactionScores: const {}, + latestReactions: const [], + ownReactions: const [], + ), Reaction(type: 'test'), );