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

fetch plugin list test #2115

Merged
merged 6 commits into from
Nov 20, 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
40 changes: 27 additions & 13 deletions test/helpers/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,33 @@ GraphQLClient getAndRegisterGraphQLClient() {
// Either fill this with mock data or override this stub
// and return null

// when(service.query(QueryOptions(
// document: gql(queries.getPluginsList()),
// ))).thenAnswer(
// (realInvocation) async {
// return QueryResult.internal(
// source: QueryResultSource.network,
// parserFn: (data) => {},
// data: {
// "getPlugins": [],
// },
// );
// },
// );
when(service.query(any)).thenAnswer(
(realInvocation) async {
if (locator.isRegistered<GraphQLClient>()) {
return Future.value(
QueryResult<Map<String, dynamic>>(
source: QueryResultSource.network,
data: {
"getPlugins": null,
},
options: QueryOptions(
document: gql(queries.getPluginsList()),
),
),
);
} else {
return Future.value(
QueryResult<Map<String, dynamic>>(
source: QueryResultSource.network,
data: null,
options: QueryOptions(
document: gql(queries.getPluginsList()),
),
),
);
}
},
);

when(service.defaultPolicies).thenAnswer(
(realInvocation) => DefaultPolicies(),
Expand Down
86 changes: 86 additions & 0 deletions test/plugins/fetch_plugin_list_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';
import 'package:talawa/plugins/fetch_plugin_list.dart';
import 'package:talawa/services/graphql_config.dart';

import '../helpers/test_helpers.dart';
import '../helpers/test_locator.dart';

/// Tests fetch_plugin_list.dart.
///
/// more_info_if_required
///
/// **params**:
/// None
///
/// **returns**:
/// None
void main() async {
testSetupLocator();
locator<GraphqlConfig>().test();
late FetchPluginList fetchPluginList;

final Directory dir = await Directory.systemTemp.createTemp('talawa_test');
Hive.init(dir.path);
await Hive.openBox('pluginBox');

setUp(() {
registerServices();
locator.allowReassignment = true;

locator.registerLazySingleton<FetchPluginList>(() => FetchPluginList());
fetchPluginList = locator<FetchPluginList>();
});
group('FetchPluginList', () {
test('fetchList should fetch plugins and store them in Hive', () async {
final queryResult = QueryResult(
data: {
'getPlugins': [
{
'_id': '1',
'pluginName': 'Plugin 1',
'pluginCreatedBy': 'User A',
'pluginDesc': 'Description A',
'pluginInstallStatus': true,
'installedOrgs': ['Org A'],
},
],
},
options: QueryOptions(
document: gql(queries.getPluginsList()),
),
source: QueryResultSource.network,
);

when(locator<GraphqlConfig>().clientToQuery()).thenAnswer(
(_) => locator<GraphQLClient>(),
);
when(
locator<GraphQLClient>().query(
QueryOptions(
document: gql(queries.getPluginsList()),
),
),
).thenAnswer((_) async => queryResult);
await fetchPluginList.fetchList();

verify(locator<GraphqlConfig>().clientToQuery()).called(2);
verify(
locator<GraphQLClient>().query(
QueryOptions(
document: gql(queries.getPluginsList()),
),
),
).called(2);

expect(fetchPluginList.box, isNotNull);
expect(
fetchPluginList.box.get('plugins'),
equals(queryResult.data!["getPlugins"]),
);
});
});
}
Loading