Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test chat service #2693

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 100 additions & 8 deletions test/service_tests/chat_service_test.dart
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';
Expand All @@ -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,
},
),
Expand All @@ -34,7 +38,7 @@ void main() {
options: QueryOptions(document: gql(query)),
data: {
'sendMessageToDirectChat': {
'_id': id,
'_id': chatId,
'messageContent': messageContent,
'sender': {
'firstName': 'Mohamed',
Expand All @@ -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(
Expand All @@ -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';
Expand All @@ -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',
},
}
],
},
Expand All @@ -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>>());
});
});
}
Loading