Skip to content

Commit

Permalink
Test updated
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaulekhi committed Dec 25, 2024
1 parent 982c099 commit bc024b4
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/view_model/pre_auth_view_models/login_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LoginViewModel extends BaseModel {
String? prevUserPassword;

/// Secure local storage instance.
final secureStorage = const FlutterSecureStorage();
FlutterSecureStorage secureStorage = const FlutterSecureStorage();

/// List of maps to store greetings..
late List<Map<String, dynamic>> greeting;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// ignore_for_file: talawa_good_doc_comments

// import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -170,6 +172,48 @@ Future<void> main() async {
expect(model.validate, AutovalidateMode.disabled);
verify(databaseFunctions.gqlNonAuthMutation(queries.loginUser('', '')));
});

test("Check if prev user is fetched correctly with success", () async {
final model = LoginViewModel();
FlutterSecureStorage.setMockInitialValues(
{"userEmail": "[email protected]", "userPassword": "password123"},
);
await model.fetchPrevUser();

expect(model.prevUserEmail, "[email protected]");
expect(model.prevUserPassword, "password123");
});

test("Check if fetching previous user result in error", () async {
final model = LoginViewModel();
FlutterSecureStorage.setMockInitialValues(
{"userEmail": "[email protected]", "userPassword": "password123"},
);
final mockSecureStorage = MockFlutterSecureStorage();
model.secureStorage = mockSecureStorage;

String log = "";

await runZonedGuarded(
() async {
await model.fetchPrevUser();
},
(error, stack) {
expect(error, isA<Exception>());
expect(error.toString(), contains("Unable to read"));
expect(stack, isNotNull);
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);
expect(
log,
contains("Unable to read"),
);
});
});
}

Expand All @@ -191,3 +235,22 @@ class MockUserConfig extends Mock implements UserConfig {
@override
Future<bool> updateUser(User user) async => true;
}

/// Mock Class for Flutter Secure Storage.
class MockFlutterSecureStorage extends Mock implements FlutterSecureStorage {
@override
Future<String?> read({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
if (key == "userEmail" || key == "userPassword") {
throw Exception("Unable to read");
}
return Future.value(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
Expand All @@ -23,6 +26,25 @@ import '../../../helpers/test_locator.dart';
/// MockBuildContext class helps to mock the BuildContext class.
class MockBuildContext extends Mock implements BuildContext {}

/// Mock Class for Flutter Secure Storage.
class MockFlutterSecureStorage extends Mock implements FlutterSecureStorage {
@override
Future<void> delete({
required String key,
IOSOptions? iOptions,
AndroidOptions? aOptions,
LinuxOptions? lOptions,
WebOptions? webOptions,
MacOsOptions? mOptions,
WindowsOptions? wOptions,
}) async {
if (key == "userEmail" || key == "userPassword") {
throw Exception("Deletion error");
}
return Future.value(null);
}
}

/// MockCallbackFunction class helps to mock the callback function.
class MockCallbackFunction extends Mock {
/// call function helps to mock the call function.
Expand Down Expand Up @@ -307,5 +329,60 @@ Future<void> main() async {

verify(model.logout());
});
test('Should delete stored values if checkBoxVal is false', () async {
FlutterSecureStorage.setMockInitialValues(
{"userEmail": "[email protected]", "userPassword": "password123"},
);
const secureStorage = FlutterSecureStorage();
const bool checkBoxVal = false;
if (checkBoxVal == false) {
try {
await secureStorage.delete(key: "userEmail");
await secureStorage.delete(key: "userPassword");
} catch (e) {
print("Unable to delete stored value : $e");
}
}
final userEmail = await secureStorage.read(key: "userEmail");
final userPassword = await secureStorage.read(key: "userPassword");
expect(userEmail, isNull);
expect(userPassword, isNull);
});
test('Should handle exception during deletion', () async {
FlutterSecureStorage.setMockInitialValues(
{"userEmail": "[email protected]", "userPassword": "password123"},
);
final mockSecureStorage = MockFlutterSecureStorage();
const checkBoxVal = false;

String log = "";

await runZonedGuarded(
() async {
if (checkBoxVal == false) {
try {
await mockSecureStorage.delete(key: "userEmail");
await mockSecureStorage.delete(key: "userPassword");
} catch (e) {
print("Unable to delete stored value: $e");
}
}
},
(error, stack) {
expect(error, isA<Exception>());
expect(error.toString(), contains("Deletion error"));
expect(stack, isNotNull);
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);
expect(
log,
contains("Deletion error"),
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,54 @@ import 'package:talawa/widgets/raised_round_edge_button.dart';

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

/// success variable for checkBox.
bool success = false;

/// cancellation variabe for checkBox.
bool cancelled = false;

/// Checkbox variable.
bool checkboxValue = false;

/// Function to run on pressing success.
///
/// Running this function will idicate that success is chosed and success is working properly.
///
/// **params**:
/// * `value`: Represents the new value of check
///
/// **returns**:
/// None
void onSuccess(bool? value) {
success = true;
checkboxValue = value!;
}

/// Function to run on pressing cancellation.
///
/// Running this function will idicate that cancel is chosed and cancellation is working properly.
///
/// **params**:
/// None
///
/// **returns**:
/// None
void onCancel() {
cancelled = true;
}

/// Function to run on pressing cancellation.
///
/// Running this function will idicate that cancel is chosed and cancellation is working properly.
///
/// **params**:
/// * `reverse`: Indicates whether the order of action buttons should be reversed.
/// * `dialogTitle`: Title displayed in the dialog.
/// * `initialCheckboxValue`: Initial value for the checkbox.
/// * `passSecondaryFunc`: Indicates whether to pass secondary function.
///
/// **returns**:
/// * `Widget`: Widget that we will be using for testing
Widget createCustomAlertDialogWithCheckbox({
bool reverse = false,
String? dialogTitle,
Expand Down
3 changes: 3 additions & 0 deletions test/widget_tests/widgets/directly_login_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ void main() {
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));

expect(mockLoginViewModel.email.text, "");
expect(mockLoginViewModel.password.text, "");
expect(find.byType(SizedBox), findsOneWidget);
});
}

0 comments on commit bc024b4

Please sign in to comment.