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
Future<QueryReactionsResponse> getReactions(
String messageId, {
PaginationParams? options,
PaginationParams? pagination,
}) =>
_client.getReactions(
messageId,
options: options,
pagination: pagination,
);
/// Retrieves a list of messages by ID
@@ -1149,11 +1149,11 @@ class StreamChatClient {
/// Get all the reactions for a [messageId]
Future<QueryReactionsResponse> getReactions(
String messageId, {
PaginationParams? options,
PaginationParams? pagination,
}) =>
_chatApi.message.getReactions(
messageId,
options: options,
pagination: pagination,
);
/// Update the given message
@@ -143,12 +143,12 @@ class MessageApi {
/// Get all the reactions for a [messageId]
Future<QueryReactionsResponse> getReactions(
String messageId, {
PaginationParams? options,
PaginationParams? pagination,
}) async {
final response = await _client.get(
'/messages/$messageId/reactions',
queryParameters: {
if (options != null) ...options.toJson(),
if (pagination != null) ...pagination.toJson(),
},
);
return QueryReactionsResponse.fromJson(response.data);
@@ -27,10 +27,15 @@ void main() {
const path = '/channels/$channelType/$channelId/message';
when(() => client.post(path, data: any(named: 'data')))
.thenAnswer((_) async => successResponse(path, data: {
'message': message.toJson(),
}));
when(() => client.post(
path,
data: {
'message': message,
'skip_push': false,
},
)).thenAnswer((_) async => successResponse(path, data: {
'message': message.toJson(),
}));
final res = await messageApi.sendMessage(channelId, channelType, message);
@@ -41,6 +46,37 @@ void main() {
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 {
const channelId = 'test-channel-id';
const channelType = 'test-channel-type';
@@ -53,10 +89,12 @@ void main() {
(index) => Message(id: 'test-message-id-$index'),
);
when(() => client.get(path, queryParameters: any(named: 'queryParameters')))
.thenAnswer((_) async => successResponse(path, data: {
'messages': [...messages.map((it) => it.toJson())],
}));
when(() => client.get(
path,
queryParameters: {'ids': messageIds.join(',')},
)).thenAnswer((_) async => successResponse(path, data: {
'messages': [...messages.map((it) => it.toJson())],
}));
final res = await messageApi.getMessagesById(
channelId,
@@ -97,7 +135,10 @@ void main() {
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()}),
);
@@ -169,8 +210,17 @@ void main() {
const path = '/messages/$messageId/action';
when(() => client.post(path, data: any(named: 'data'))).thenAnswer(
(_) async => successResponse(path, data: <String, dynamic>{}));
when(() => client.post(
path,
data: {
'id': channelId,
'type': channelType,
'form_data': formData,
'message_id': messageId,
},
))
.thenAnswer(
(_) async => successResponse(path, data: <String, dynamic>{}));
final res = await messageApi.sendAction(
channelId,
@@ -195,11 +245,17 @@ void main() {
final message = Message(id: messageId);
final reaction = Reaction(type: reactionType, messageId: messageId);
when(() => client.post(path, data: any(named: 'data')))
.thenAnswer((_) async => successResponse(path, data: {
'message': message.toJson(),
'reaction': reaction.toJson(),
}));
when(() => client.post(
path,
data: {
'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(
messageId,
@@ -216,6 +272,44 @@ void main() {
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 {
const messageId = 'test-message-id';
const reactionType = 'test-reaction-type';
@@ -247,12 +341,16 @@ void main() {
),
);
when(() => client.get(path, queryParameters: any(named: 'queryParameters')))
.thenAnswer((_) async => successResponse(path, data: {
'reactions': [...reactions.map((it) => it.toJson())]
}));
when(() => client.get(
path,
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.reactions.length, reactions.length);
@@ -277,10 +375,12 @@ void main() {
language: translatedMessageText,
});
when(() => client.post(path, data: any(named: 'data')))
.thenAnswer((_) async => successResponse(path, data: {
'message': translatedMessage.toJson(),
}));
when(() => client.post(
path,
data: {'language': language},
)).thenAnswer((_) async => successResponse(path, data: {
'message': translatedMessage.toJson(),
}));
final res = await messageApi.translateMessage(messageId, language);
@@ -306,10 +406,14 @@ void main() {
),
);
when(() => client.get(path, queryParameters: any(named: 'queryParameters')))
.thenAnswer((_) async => successResponse(path, data: {
'messages': [...messages.map((it) => it.toJson())]
}));
when(() => client.get(
path,
queryParameters: {
...options.toJson(),
},
)).thenAnswer((_) async => successResponse(path, data: {
'messages': [...messages.map((it) => it.toJson())]
}));
final res = await messageApi.getReplies(parentId, options: options);