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 for services post_services.dart #2244

Merged
merged 9 commits into from Dec 15, 2023
Merged
Show file tree
Hide file tree
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
19 changes: 12 additions & 7 deletions lib/services/post_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// ignore_for_file: talawa_api_doc, avoid_dynamic_calls
// ignore_for_file: talawa_good_doc_comments

import 'dart:async';

import 'package:talawa/locator.dart';
Expand Down Expand Up @@ -31,7 +28,7 @@ class PostService {
StreamController<List<Post>>();
late Stream<List<Post>> _postStream;

//Stream for individul post update
//Stream for individual post update
final StreamController<Post> _updatedPostStreamController =
StreamController<Post>();
late Stream<Post> _updatedPostStream;
Expand Down Expand Up @@ -59,17 +56,25 @@ class PostService {
});
}

/// This function used to get all posts of an organization.
/// The function reference the organization Id from `_currentOrg`.
/// Retrieves all posts of the organization.
///
/// This method queries the organization ID from `_currentOrg` and fetches
/// posts using a GraphQL query. The retrieved posts are added to the internal
/// post stream
Future<void> getPosts() async {
// variables
final String currentOrgID = _currentOrg.id!;
final String query = PostQueries().getPostsById(currentOrgID);
final result = await _dbFunctions.gqlAuthQuery(query);

//Checking if the dbFunctions return the postJSON, if not return.
if (result.data!['postsByOrganization'] == null) return;
// ignore:avoid_dynamic_calls
if (result == null || result.data == null) {
// Handle the case where the result or result.data is null
return;
}

// ignore:avoid_dynamic_calls
final List postsJson = result.data!['postsByOrganization'] as List;

postsJson.forEach((postJson) {
Expand Down
90 changes: 90 additions & 0 deletions test/service_tests/post_service_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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/organization/org_info.dart';
import 'package:talawa/models/post/post_model.dart';
import 'package:talawa/services/database_mutation_functions.dart';
import 'package:talawa/services/post_service.dart';
import 'package:talawa/services/user_config.dart';
import 'package:talawa/utils/post_queries.dart';
import '../helpers/test_helpers.dart';

Expand Down Expand Up @@ -189,5 +192,92 @@ void main() {
//Testing if the post got a comment
expect(commentedPost.comments!.length, 1);
});
test('Test updatedPostStream Stream', () async {
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>();

final query = PostQueries().getPostsById(currentOrgID);
// Mocking GetPosts
when(
dataBaseMutationFunctions.gqlAuthQuery(
query,
),
).thenAnswer(
(_) async => QueryResult(
options: QueryOptions(document: gql(query)),
data: demoJson,
source: QueryResultSource.network,
),
);

final service = PostService();
// Populating posts Stream
await service.getPosts();

// Listen to updatedPostStream and collect emitted values
final List<Post> updatedPosts = [];
final subscription = service.updatedPostStream.listen((post) {
updatedPosts.add(post);
});

// Trigger an event that should update the post
await service.addLike(postID);

// Wait for the stream to emit values
await Future.delayed(
const Duration(seconds: 1),
); // Adjust the delay as needed

// Verify that the correct post was emitted
expect(updatedPosts.length, 1);
expect(updatedPosts[0].sId, postID);
// Cancel the subscription to avoid memory leaks
await subscription.cancel();
});
test(
'Test setOrgStreamSubscription method after the organization is updated',
() async {
final dataBaseMutationFunctions = locator<DataBaseMutationFunctions>();

final query = PostQueries().getPostsById(currentOrgID);
// Mocking GetPosts
when(
dataBaseMutationFunctions.gqlAuthQuery(
query,
),
).thenAnswer(
(_) async => QueryResult(
options: QueryOptions(document: gql(query)),
data: demoJson,
source: QueryResultSource.network,
),
);

final service = PostService();
// Populating posts Stream
await service.getPosts();

// Set up mock for currentOrgInfoStream
final mockUserConfig = locator<UserConfig>();
final orgInfoStreamController = StreamController<OrgInfo>();
when(mockUserConfig.currentOrgInfoStream)
.thenAnswer((_) => orgInfoStreamController.stream);

// Call setOrgStreamSubscription
service.setOrgStreamSubscription();

// Trigger an event that should update the organization
orgInfoStreamController.add(OrgInfo(id: 'newOrgId'));

// Wait for the setOrgStreamSubscription logic to execute
await Future.delayed(
const Duration(seconds: 1),
); // Adjust the delay as needed

// Verify that getPosts was called after the organization update
verify(service.getPosts()).called(1);

// Close the stream controller to avoid memory leaks
await orgInfoStreamController.close();
});
});
}
Loading