Skip to content

Commit

Permalink
fetch plugin list test (PalisadoesFoundation#2115)
Browse files Browse the repository at this point in the history
* fetch plugin list test

* test helpers

* fixing workflow

* fixing workflow

* fixing failing test

* fixing failing tets
  • Loading branch information
Dante291 authored and palisadian committed Jan 10, 2024
1 parent 2112759 commit 96a8028
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 13 deletions.
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"]),
);
});
});
}

0 comments on commit 96a8028

Please sign in to comment.