-
-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test for chat * test for chat * Test ready for chat service
- Loading branch information
1 parent
918eb7e
commit bb59156
Showing
1 changed file
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
// ignore_for_file: talawa_api_doc | ||
// ignore_for_file: talawa_good_doc_comments | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:graphql_flutter/graphql_flutter.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:talawa/locator.dart'; | ||
import 'package:talawa/models/chats/chat_list_tile_data_model.dart'; | ||
import 'package:talawa/models/chats/chat_message.dart'; | ||
import 'package:talawa/services/chat_service.dart'; | ||
import 'package:talawa/services/database_mutation_functions.dart'; | ||
import 'package:talawa/utils/chat_queries.dart'; | ||
|
@@ -17,15 +21,15 @@ void main() { | |
group('Test ChatService', () { | ||
test('Test SendMessageToDirectChat Method', () async { | ||
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>(); | ||
const id = "1"; | ||
const chatId = "1"; | ||
const messageContent = "test"; | ||
|
||
final query = ChatQueries().sendMessageToDirectChat(); | ||
when( | ||
dataBaseMutationFunctions.gqlAuthMutation( | ||
query, | ||
variables: { | ||
"chatId": id, | ||
"chatId": chatId, | ||
"messageContent": messageContent, | ||
}, | ||
), | ||
|
@@ -34,7 +38,7 @@ void main() { | |
options: QueryOptions(document: gql(query)), | ||
data: { | ||
'sendMessageToDirectChat': { | ||
'_id': id, | ||
'_id': chatId, | ||
'messageContent': messageContent, | ||
'sender': { | ||
'firstName': 'Mohamed', | ||
|
@@ -48,16 +52,35 @@ void main() { | |
), | ||
); | ||
final service = ChatService(); | ||
final completer = Completer<void>(); | ||
final messages = <ChatMessage>[]; | ||
final subscription = service.chatMessagesStream.listen((message) { | ||
messages.add(message); | ||
completer.complete(); | ||
}); | ||
|
||
await service.sendMessageToDirectChat( | ||
id, | ||
chatId, | ||
messageContent, | ||
); | ||
|
||
await completer.future; | ||
|
||
await subscription.cancel(); | ||
|
||
print(messages.first.id); | ||
expect(messages, isA<List<ChatMessage>>()); | ||
expect(messages.length, 1); | ||
expect(messages.first.messageContent, messageContent); | ||
expect(messages.first.sender?.firstName, 'Mohamed'); | ||
expect(messages.first.receiver?.firstName, 'Ali'); | ||
}); | ||
|
||
test('getDirectChatsByUserId Method', () async { | ||
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>(); | ||
const userId = "xzy1"; | ||
final query = ChatQueries().fetchDirectChatsByUserId(userId); | ||
// when(locator<UserConfig>()).thenAnswer((_) => UserConfig()); | ||
|
||
when(dataBaseMutationFunctions.gqlAuthQuery(query)).thenAnswer( | ||
(_) async => QueryResult( | ||
options: QueryOptions( | ||
|
@@ -66,17 +89,47 @@ void main() { | |
data: { | ||
'directChatsByUserID': [ | ||
{ | ||
'users': [], | ||
'_id': 'xzy1', | ||
} | ||
'users': [ | ||
{ | ||
'_id': 'user1', | ||
'firstName': 'John', | ||
'email': '[email protected]', | ||
}, | ||
{ | ||
'_id': 'xzy1', | ||
'firstName': 'Jane', | ||
'email': '[email protected]', | ||
}, | ||
], | ||
'_id': 'chat1', | ||
}, | ||
], | ||
}, | ||
source: QueryResultSource.network, | ||
), | ||
); | ||
final service = ChatService(); | ||
final completer = Completer<void>(); | ||
final chats = <ChatListTileDataModel>[]; | ||
final subscription = service.chatListStream.listen((chat) { | ||
chats.add(chat); | ||
if (!completer.isCompleted) { | ||
completer.complete(); | ||
} | ||
}); | ||
|
||
await service.getDirectChatsByUserId(); | ||
|
||
await completer.future; | ||
|
||
await subscription.cancel(); | ||
|
||
expect(chats, isA<List<ChatListTileDataModel>>()); | ||
expect(chats.length, 1); | ||
expect(chats.first.users?.length, 2); | ||
expect(chats.first.users?.first.firstName, 'John'); | ||
}); | ||
|
||
test("getDirectChatMessagesByChatId Method", () async { | ||
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>(); | ||
const chatId = 'test'; | ||
|
@@ -91,6 +144,16 @@ void main() { | |
{ | ||
'_id': 'test', | ||
'messageContent': 'test', | ||
'sender': { | ||
'_id': 'user1', | ||
'firstName': 'John', | ||
'image': 'image_url_1', | ||
}, | ||
'receiver': { | ||
'_id': 'user2', | ||
'firstName': 'Jane', | ||
'image': 'image_url_2', | ||
}, | ||
} | ||
], | ||
}, | ||
|
@@ -99,7 +162,36 @@ void main() { | |
); | ||
|
||
final service = ChatService(); | ||
final completer = Completer<void>(); | ||
final messages = <ChatMessage>[]; | ||
final subscription = service.chatMessagesStream.listen((message) { | ||
messages.add(message); | ||
if (!completer.isCompleted) { | ||
completer.complete(); | ||
} | ||
}); | ||
|
||
await service.getDirectChatMessagesByChatId(chatId); | ||
|
||
await completer.future; | ||
|
||
await subscription.cancel(); | ||
|
||
expect(messages, isA<List<ChatMessage>>()); | ||
expect(messages.length, 1); | ||
expect(messages.first.messageContent, 'test'); | ||
expect(messages.first.sender?.firstName, 'John'); | ||
expect(messages.first.receiver?.firstName, 'Jane'); | ||
}); | ||
|
||
test("chatListStream return a stream of ChatListTileDataModel", () { | ||
final service = ChatService(); | ||
expect(service.chatListStream, isA<Stream<ChatListTileDataModel>>()); | ||
}); | ||
|
||
test('chatMessagesStream returns a stream of ChatMessage', () async { | ||
final chatService = ChatService(); | ||
expect(chatService.chatMessagesStream, isA<Stream<ChatMessage>>()); | ||
}); | ||
}); | ||
} |