From bb59156cd2d1ac8489a6c8e4440a26c69b09a6ea Mon Sep 17 00:00:00 2001 From: MohitMaulekhi <126096986+MohitMaulekhi@users.noreply.github.com> Date: Sun, 5 Jan 2025 01:29:55 +0530 Subject: [PATCH] Unit test chat service (#2693) * test for chat * test for chat * Test ready for chat service --- test/service_tests/chat_service_test.dart | 108 ++++++++++++++++++++-- 1 file changed, 100 insertions(+), 8 deletions(-) diff --git a/test/service_tests/chat_service_test.dart b/test/service_tests/chat_service_test.dart index cd6da0d8d..a93e19853 100644 --- a/test/service_tests/chat_service_test.dart +++ b/test/service_tests/chat_service_test.dart @@ -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,7 +21,7 @@ void main() { group('Test ChatService', () { test('Test SendMessageToDirectChat Method', () async { final dataBaseMutationFunctions = locator(); - const id = "1"; + const chatId = "1"; const messageContent = "test"; final query = ChatQueries().sendMessageToDirectChat(); @@ -25,7 +29,7 @@ void main() { 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(); + final messages = []; + 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>()); + 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(); const userId = "xzy1"; final query = ChatQueries().fetchDirectChatsByUserId(userId); - // when(locator()).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': 'john@example.com', + }, + { + '_id': 'xzy1', + 'firstName': 'Jane', + 'email': 'jane@example.com', + }, + ], + '_id': 'chat1', + }, ], }, source: QueryResultSource.network, ), ); final service = ChatService(); + final completer = Completer(); + final chats = []; + 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>()); + expect(chats.length, 1); + expect(chats.first.users?.length, 2); + expect(chats.first.users?.first.firstName, 'John'); }); + test("getDirectChatMessagesByChatId Method", () async { final dataBaseMutationFunctions = locator(); 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(); + final messages = []; + 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>()); + 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>()); + }); + + test('chatMessagesStream returns a stream of ChatMessage', () async { + final chatService = ChatService(); + expect(chatService.chatMessagesStream, isA>()); }); }); }