From 757b640c6d9dee75cebd5ea36294046bf09e633a Mon Sep 17 00:00:00 2001 From: Azad99-9 Date: Tue, 19 Dec 2023 00:52:51 +0530 Subject: [PATCH] user_config.dart made 100% code coverage. --- lib/services/user_config.dart | 2 + test/helpers/test_helpers.dart | 2 + test/service_tests/user_config_test.dart | 113 +++++++++++++++++++++-- 3 files changed, 111 insertions(+), 6 deletions(-) diff --git a/lib/services/user_config.dart b/lib/services/user_config.dart index 73fcfa739..62683ad02 100644 --- a/lib/services/user_config.dart +++ b/lib/services/user_config.dart @@ -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'); @@ -108,6 +109,7 @@ class UserConfig { _currentOrgInfoController.add(_currentOrg!); saveUserInHive(); + return true; } on Exception catch (e) { print(e); diff --git a/test/helpers/test_helpers.dart b/test/helpers/test_helpers.dart index 6bafa6645..4fca02942 100644 --- a/test/helpers/test_helpers.dart +++ b/test/helpers/test_helpers.dart @@ -243,6 +243,8 @@ GraphqlConfig getAndRegisterGraphqlConfig() { ); }); + when(service.getToken()).thenAnswer((_) async => "sample_token"); + locator.registerSingleton(service); return service; } diff --git a/test/service_tests/user_config_test.dart b/test/service_tests/user_config_test.dart index 8f842156d..ef0bd9d78 100644 --- a/test/service_tests/user_config_test.dart +++ b/test/service_tests/user_config_test.dart @@ -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'; @@ -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>()); + + // model.currentOrgName + expect(model.currentOrgName, isA()); + + // 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 data = { + 'users': [ + { + '_id': '1234567890', + 'firstName': 'John', + 'lastName': 'Doe', + 'email': 'johndoe@example.com', + '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())) @@ -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; @@ -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; @@ -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]; @@ -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; @@ -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'; @@ -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); + }); }); }