Skip to content

Commit

Permalink
user_config.dart made 100% code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Azad99-9 committed Dec 18, 2023
1 parent 5416442 commit 757b640
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/services/user_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class UserConfig {
_currentOrgInfoController.add(_currentOrg!);

_currentUser = boxUser.get('user');

// if there is not currentUser then returns false.
if (_currentUser == null) {
_currentUser = User(id: 'null', authToken: 'null');
Expand All @@ -108,6 +109,7 @@ class UserConfig {
_currentOrgInfoController.add(_currentOrg!);

saveUserInHive();

return true;
} on Exception catch (e) {
print(e);
Expand Down
2 changes: 2 additions & 0 deletions test/helpers/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ GraphqlConfig getAndRegisterGraphqlConfig() {
);
});

when(service.getToken()).thenAnswer((_) async => "sample_token");

locator.registerSingleton<GraphqlConfig>(service);
return service;
}
Expand Down
113 changes: 107 additions & 6 deletions test/service_tests/user_config_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: talawa_api_doc
// ignore_for_file: talawa_good_doc_comments

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -78,7 +79,81 @@ void main() async {
setUpAll(() {
registerServices();
});
test('Test for User log out.', () async {

test('Test for getters & setters.', () {
final model = UserConfig();

// model.currentOrgInfoController
expect(model.currentOrgInfoController, isA<StreamController<OrgInfo>>());

// model.currentOrgName
expect(model.currentOrgName, isA<String>());

// model.currenOrg (setter)
model.currentOrg = OrgInfo(name: 'org');

// print(model.currentOrgInfoController);
});

test('Test for userLoggedIn method.', () async {
final model = UserConfig();
model.currentUser.id = 'fake_id';

userBox.put('user', User(id: 'fake', firstName: 'first'));

final Map<String, dynamic> data = {
'users': [
{
'_id': '1234567890',
'firstName': 'John',
'lastName': 'Doe',
'email': '[email protected]',
'image': 'https://example.com/profile.jpg',
'accessToken': 'exampleAccessToken',
'refreshToken': 'exampleRefreshToken',
}
],
};

when(
databaseFunctions.gqlAuthQuery(
queries.fetchUserInfo,
variables: anyNamed('variables'),
),
).thenAnswer((_) async {
return QueryResult(
source: QueryResultSource.network,
data: data,
options: QueryOptions(document: gql(queries.fetchUserInfo)),
);
});

// if there is _currentUser.
bool loggedIn = await model.userLoggedIn();
expect(loggedIn, true);

userBox.delete('user');

// if there is no _currentUser.
loggedIn = await model.userLoggedIn();
expect(loggedIn, false);

when(
databaseFunctions.gqlAuthQuery(
queries.fetchUserInfo,
variables: anyNamed('variables'),
),
).thenAnswer((_) async {
throw Exception('Simulated Exception.');
});

// show couldn't update errorsnackbar.
loggedIn = await model.userLoggedIn();
expect(loggedIn, true);
// print(model.currentUser);
});

test('Test for User log out method.', () async {
databaseFunctions.init();

when(databaseFunctions.gqlAuthMutation(queries.logout()))
Expand Down Expand Up @@ -120,7 +195,7 @@ void main() async {
expect(loggedOut, false);
});

test('Test for updateUserJoinedOrg', () async {
test('Test for updateUserJoinedOrg method', () async {
final model = UserConfig();
model.currentUser = mockUser;

Expand All @@ -129,7 +204,7 @@ void main() async {
expect(mockUser.joinedOrganizations, mockOrgDetails);
});

test('Test for updateUserCreatedOrg', () async {
test('Test for updateUserCreatedOrg method', () async {
final model = UserConfig();
model.currentUser = mockUser;

Expand All @@ -138,7 +213,7 @@ void main() async {
expect(mockUser.createdOrganizations, mockOrgDetails);
});

test('Test for updateUserMemberRequestOrg', () async {
test('Test for updateUserMemberRequestOrg method', () async {
final model = UserConfig();
model.currentUser = mockUser;
final expected = [...mockUser.membershipRequests!, ...mockOrgDetails];
Expand All @@ -147,7 +222,7 @@ void main() async {
expect(mockUser.membershipRequests, expected);
});

test('Test for updateUserAdminOrg', () async {
test('Test for updateUserAdminOrg method', () async {
final model = UserConfig();
model.currentUser = mockUser;

Expand All @@ -156,7 +231,7 @@ void main() async {
expect(mockUser.adminFor, mockOrgDetails);
});

test('Test for updateAccessToken', () async {
test('Test for updateAccessToken method.', () async {
final model = UserConfig();
model.currentUser = mockUser;
const newAuthToken = 'newAccessToken';
Expand All @@ -170,5 +245,31 @@ void main() async {
expect(mockUser.authToken, newAuthToken);
expect(mockUser.refreshToken, newRefreshToken);
});

test('Test for saveCurrentOrgInHive method.', () async {
final model = UserConfig();
model.currentUser = mockUser;

// To test the box.get('org') != null condition.
orgBox.put('org', OrgInfo(id: 'fakeId', name: 'org'));
model.saveCurrentOrgInHive(mockOrgDetails[0]);

// To test the box.get('org') == null condition.
orgBox.delete('org');
model.saveCurrentOrgInHive(mockOrgDetails[0]);
});

test('Test for updateUser method.', () async {
final model = UserConfig();

when(databaseFunctions.init()).thenAnswer((_) {
throw Exception('simulated exception.');
});

final updated = await model.updateUser(User(id: 'sampleId'));

// user updation failed.
expect(!updated, true);
});
});
}

0 comments on commit 757b640

Please sign in to comment.