better message api tests

This commit is contained in:
Salvatore Giordano
2021-06-26 13:37:40 +02:00
parent ff8ff6d7ce
commit 9857db0635
4 changed files with 139 additions and 35 deletions
@@ -989,11 +989,11 @@ class Channel {
/// List the reactions for a message in the channel /// List the reactions for a message in the channel
Future<QueryReactionsResponse> getReactions( Future<QueryReactionsResponse> getReactions(
String messageId, { String messageId, {
PaginationParams? options, PaginationParams? pagination,
}) => }) =>
_client.getReactions( _client.getReactions(
messageId, messageId,
options: options, pagination: pagination,
); );
/// Retrieves a list of messages by ID /// Retrieves a list of messages by ID
@@ -1149,11 +1149,11 @@ class StreamChatClient {
/// Get all the reactions for a [messageId] /// Get all the reactions for a [messageId]
Future<QueryReactionsResponse> getReactions( Future<QueryReactionsResponse> getReactions(
String messageId, { String messageId, {
PaginationParams? options, PaginationParams? pagination,
}) => }) =>
_chatApi.message.getReactions( _chatApi.message.getReactions(
messageId, messageId,
options: options, pagination: pagination,
); );
/// Update the given message /// Update the given message
@@ -143,12 +143,12 @@ class MessageApi {
/// Get all the reactions for a [messageId] /// Get all the reactions for a [messageId]
Future<QueryReactionsResponse> getReactions( Future<QueryReactionsResponse> getReactions(
String messageId, { String messageId, {
PaginationParams? options, PaginationParams? pagination,
}) async { }) async {
final response = await _client.get( final response = await _client.get(
'/messages/$messageId/reactions', '/messages/$messageId/reactions',
queryParameters: { queryParameters: {
if (options != null) ...options.toJson(), if (pagination != null) ...pagination.toJson(),
}, },
); );
return QueryReactionsResponse.fromJson(response.data); return QueryReactionsResponse.fromJson(response.data);
@@ -27,10 +27,15 @@ void main() {
const path = '/channels/$channelType/$channelId/message'; const path = '/channels/$channelType/$channelId/message';
when(() => client.post(path, data: any(named: 'data'))) when(() => client.post(
.thenAnswer((_) async => successResponse(path, data: { path,
'message': message.toJson(), data: {
})); 'message': message,
'skip_push': false,
},
)).thenAnswer((_) async => successResponse(path, data: {
'message': message.toJson(),
}));
final res = await messageApi.sendMessage(channelId, channelType, message); final res = await messageApi.sendMessage(channelId, channelType, message);
@@ -41,6 +46,37 @@ void main() {
verifyNoMoreInteractions(client); verifyNoMoreInteractions(client);
}); });
test('sendMessage with skipPush: true', () async {
const channelId = 'test-channel-id';
const channelType = 'test-channel-type';
final message = Message(id: 'test-message-id', text: 'test-message-text');
const path = '/channels/$channelType/$channelId/message';
when(() => client.post(
path,
data: {
'message': message,
'skip_push': true,
},
)).thenAnswer((_) async => successResponse(path, data: {
'message': message.toJson(),
}));
final res = await messageApi.sendMessage(
channelId,
channelType,
message,
skipPush: true,
);
expect(res, isNotNull);
expect(res.message.id, message.id);
verify(() => client.post(path, data: any(named: 'data'))).called(1);
verifyNoMoreInteractions(client);
});
test('getMessagesById', () async { test('getMessagesById', () async {
const channelId = 'test-channel-id'; const channelId = 'test-channel-id';
const channelType = 'test-channel-type'; const channelType = 'test-channel-type';
@@ -53,10 +89,12 @@ void main() {
(index) => Message(id: 'test-message-id-$index'), (index) => Message(id: 'test-message-id-$index'),
); );
when(() => client.get(path, queryParameters: any(named: 'queryParameters'))) when(() => client.get(
.thenAnswer((_) async => successResponse(path, data: { path,
'messages': [...messages.map((it) => it.toJson())], queryParameters: {'ids': messageIds.join(',')},
})); )).thenAnswer((_) async => successResponse(path, data: {
'messages': [...messages.map((it) => it.toJson())],
}));
final res = await messageApi.getMessagesById( final res = await messageApi.getMessagesById(
channelId, channelId,
@@ -97,7 +135,10 @@ void main() {
final path = '/messages/${message.id}'; final path = '/messages/${message.id}';
when(() => client.post(path, data: any(named: 'data'))).thenAnswer( when(() => client.post(
path,
data: {'message': message},
)).thenAnswer(
(_) async => successResponse(path, data: {'message': message.toJson()}), (_) async => successResponse(path, data: {'message': message.toJson()}),
); );
@@ -169,8 +210,17 @@ void main() {
const path = '/messages/$messageId/action'; const path = '/messages/$messageId/action';
when(() => client.post(path, data: any(named: 'data'))).thenAnswer( when(() => client.post(
(_) async => successResponse(path, data: <String, dynamic>{})); path,
data: {
'id': channelId,
'type': channelType,
'form_data': formData,
'message_id': messageId,
},
))
.thenAnswer(
(_) async => successResponse(path, data: <String, dynamic>{}));
final res = await messageApi.sendAction( final res = await messageApi.sendAction(
channelId, channelId,
@@ -195,11 +245,17 @@ void main() {
final message = Message(id: messageId); final message = Message(id: messageId);
final reaction = Reaction(type: reactionType, messageId: messageId); final reaction = Reaction(type: reactionType, messageId: messageId);
when(() => client.post(path, data: any(named: 'data'))) when(() => client.post(
.thenAnswer((_) async => successResponse(path, data: { path,
'message': message.toJson(), data: {
'reaction': reaction.toJson(), 'reaction': Map<String, Object?>.from(extraData)
})); ..addAll({'type': reactionType}),
'enforce_unique': false,
},
)).thenAnswer((_) async => successResponse(path, data: {
'message': message.toJson(),
'reaction': reaction.toJson(),
}));
final res = await messageApi.sendReaction( final res = await messageApi.sendReaction(
messageId, messageId,
@@ -216,6 +272,44 @@ void main() {
verifyNoMoreInteractions(client); verifyNoMoreInteractions(client);
}); });
test('sendReaction with enforceUnique: true', () async {
const messageId = 'test-message-id';
const reactionType = 'test-reaction-type';
const extraData = {'test-key': 'test-data'};
const path = '/messages/$messageId/reaction';
final message = Message(id: messageId);
final reaction = Reaction(type: reactionType, messageId: messageId);
when(() => client.post(
path,
data: {
'reaction': Map<String, Object?>.from(extraData)
..addAll({'type': reactionType}),
'enforce_unique': true,
},
)).thenAnswer((_) async => successResponse(path, data: {
'message': message.toJson(),
'reaction': reaction.toJson(),
}));
final res = await messageApi.sendReaction(
messageId,
reactionType,
extraData: extraData,
enforceUnique: true,
);
expect(res, isNotNull);
expect(res.message.id, messageId);
expect(res.reaction.messageId, messageId);
expect(res.reaction.type, reactionType);
verify(() => client.post(path, data: any(named: 'data'))).called(1);
verifyNoMoreInteractions(client);
});
test('deleteReaction', () async { test('deleteReaction', () async {
const messageId = 'test-message-id'; const messageId = 'test-message-id';
const reactionType = 'test-reaction-type'; const reactionType = 'test-reaction-type';
@@ -247,12 +341,16 @@ void main() {
), ),
); );
when(() => client.get(path, queryParameters: any(named: 'queryParameters'))) when(() => client.get(
.thenAnswer((_) async => successResponse(path, data: { path,
'reactions': [...reactions.map((it) => it.toJson())] queryParameters: {
})); ...const PaginationParams().toJson(),
},
)).thenAnswer((_) async => successResponse(path, data: {
'reactions': [...reactions.map((it) => it.toJson())]
}));
final res = await messageApi.getReactions(messageId, options: options); final res = await messageApi.getReactions(messageId, pagination: options);
expect(res, isNotNull); expect(res, isNotNull);
expect(res.reactions.length, reactions.length); expect(res.reactions.length, reactions.length);
@@ -277,10 +375,12 @@ void main() {
language: translatedMessageText, language: translatedMessageText,
}); });
when(() => client.post(path, data: any(named: 'data'))) when(() => client.post(
.thenAnswer((_) async => successResponse(path, data: { path,
'message': translatedMessage.toJson(), data: {'language': language},
})); )).thenAnswer((_) async => successResponse(path, data: {
'message': translatedMessage.toJson(),
}));
final res = await messageApi.translateMessage(messageId, language); final res = await messageApi.translateMessage(messageId, language);
@@ -306,10 +406,14 @@ void main() {
), ),
); );
when(() => client.get(path, queryParameters: any(named: 'queryParameters'))) when(() => client.get(
.thenAnswer((_) async => successResponse(path, data: { path,
'messages': [...messages.map((it) => it.toJson())] queryParameters: {
})); ...options.toJson(),
},
)).thenAnswer((_) async => successResponse(path, data: {
'messages': [...messages.map((it) => it.toJson())]
}));
final res = await messageApi.getReplies(parentId, options: options); final res = await messageApi.getReplies(parentId, options: options);