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

Added test cases for volunteer-group-view-model #2662

Merged
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter_test/flutter_test.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:mockito/mockito.dart';
Expand All @@ -9,20 +11,31 @@ import 'package:talawa/view_model/after_auth_view_models/event_view_models/manag
import '../../../helpers/test_helpers.dart';
import '../../../helpers/test_locator.dart';

// This file contains unit tests for the ManageVolunteerGroupViewModel.
// It ensures that all methods and functionalities of the view model are working correctly.
void main() {
late ManageVolunteerGroupViewModel model;

// Setup tasks to be run before all tests
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
testSetupLocator();
registerServices();
});

// Clean up
tearDownAll(() {
unregisterServices();
});

group('ManageVolunteerGroupViewModel Tests', () {
final model = ManageVolunteerGroupViewModel();
// Setup after every test.
// Ensuring each test is independent by providing a new object for each class
setUp(() {
model = ManageVolunteerGroupViewModel();
});

// Tests the initialization of the view model.
test("Test initialization", () async {
final Event event = Event(id: "1");
final EventVolunteerGroup group =
Expand All @@ -34,12 +47,19 @@ void main() {
expect(model.volunteers, isEmpty);
});

// Testing isFetchingVolunteers getter returning False as it is a final value
test("Test isFetchingVolunteers getter", () async {
expect(model.isFetchingVolunteers, isFalse);
});

// Tests fetching the current organization users list.
test("Test getCurrentOrgUsersList success", () async {
final users = await model.getCurrentOrgUsersList();
expect(users.length, 2);
expect(users[0].id, "fakeUser1");
});

// Tests adding a volunteer to a group successfully.
test("Test addVolunteerToGroup success", () async {
final mockEventService = locator<EventService>();

Expand Down Expand Up @@ -70,6 +90,44 @@ void main() {
expect(model.volunteers.first.id, "volunteer1");
});

// Tests the failure case when adding a volunteer to a group.
test("Test addVolunteerToGroup Failure", () async {
final mockEventService = locator<EventService>();
// Verify initial state
expect(model.volunteers.length, 0);
when(
mockEventService.addVolunteerToGroup({
'eventId': "1",
'userId': "volunteer1",
'groupId': "group1",
}),
).thenThrow(Exception('Failed to add volunteer'));
String log = "";
await runZonedGuarded(
() async {
await model.addVolunteerToGroup("volunteer1", "1", "group1");
},
(error, stack) {
expect(error, isA<Exception>());
expect(error.toString(), contains('Failed to add volunteer'));
expect(stack, isNotNull);
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);
expect(
log,
contains("Failed to add volunteer"),
);
expect(model.volunteers.length, 0);
// Verify model is in a clean state after error
expect(model.isFetchingVolunteers, isFalse);
});

// Tests removing a volunteer from a group successfully.
test("Test removeVolunteerFromGroup success", () async {
final mockEventService = locator<EventService>();
final mockResult = {
Expand Down Expand Up @@ -97,6 +155,91 @@ void main() {
expect(model.volunteers.isEmpty, true);
});

// Tests the failure case when removing a volunteer from a group with null data.
test("Test removeVolunteerFromGroup with null data", () async {
final mockEventService = locator<EventService>();
final int prevLength = model.volunteers.length;
when(
mockEventService.removeVolunteerFromGroup({
'id': 'volunteer1',
}),
).thenAnswer(
(_) async => QueryResult(
data: null,
source: QueryResultSource.network,
options: QueryOptions(
document: gql(EventQueries().removeVolunteerMutation()),
),
),
);

await model.removeVolunteerFromGroup("volunteer1");

expect(model.volunteers.length, prevLength);
});

// Tests the failure case when removing a volunteer from a group with null removeEventVolunteer response.
test(
"Test removeVolunteerFromGroup with null removeEventVolunteer response",
() async {
final mockEventService = locator<EventService>();
final int prevLength = model.volunteers.length;
when(
mockEventService.removeVolunteerFromGroup({
'id': 'volunteer1',
}),
).thenAnswer(
(_) async => QueryResult(
data: {'removeEventVolunteer': null},
source: QueryResultSource.network,
options: QueryOptions(
document: gql(EventQueries().removeVolunteerMutation()),
),
),
);

await model.removeVolunteerFromGroup("volunteer1");

expect(model.volunteers.length, prevLength);
});

// Tests the failure case when removing a volunteer from a group.
test("Test removeVolunteerFromGroup failure", () async {
final mockEventService = locator<EventService>();
final int prevlength = model.volunteers.length;
when(
mockEventService.removeVolunteerFromGroup(
{
'volunteerId': 'volunteer1',
},
),
).thenThrow(Exception('Failed to remove volunteer'));
String log = "";
await runZonedGuarded(
() async {
await model.removeVolunteerFromGroup("volunteer1");
},
(error, stack) {
expect(error, isA<Exception>());
expect(stack, isNotNull);
expect(error.toString(), contains('Failed to remove volunteer'));
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);

expect(
log,
contains('Failed to remove volunteer'),
);

expect(model.volunteers.length, prevlength);
});

// Tests deleting a volunteer group successfully.
test("Test deleteVolunteerGroup success", () async {
final mockEventService = locator<EventService>();
final mockResult = {
Expand All @@ -121,6 +264,46 @@ void main() {
verify(mockEventService.removeVolunteerGroup({"id": "group1"})).called(1);
});

// Tests the failure case when deleting a volunteer group.
test("Test deleteVolunteerGroup failure", () async {
final mockEventService = locator<EventService>();

when(
mockEventService.removeVolunteerGroup(
{"id": "group1"},
),
).thenThrow(Exception("Failed to delete group"));

String log = "";
await runZonedGuarded(
() async {
await model.deleteVolunteerGroup("group1");
},
(error, stack) {
expect(
error,
isA<Exception>(),
);
expect(error.toString(), contains('Failed to delete group'));
expect(stack, isNotNull);
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);

expect(
log,
contains('Failed to delete group'),
);

// Assuming the method should notify listeners
verify(mockEventService.removeVolunteerGroup({"id": "group1"})).called(1);
});

// Tests updating a volunteer group successfully.
test("Test updateVolunteerGroup success", () async {
final EventVolunteerGroup group = EventVolunteerGroup(id: "group1");

Expand Down Expand Up @@ -155,5 +338,51 @@ void main() {
expect(group.name, "Updated Group");
expect(group.volunteersRequired, 20);
});

// Tests the failure case when updating a volunteer group.
test('Test updateVolunteerGroup failure', () async {
final mockEventService = locator<EventService>();
final group = EventVolunteerGroup(
id: "group1",
name: "Old Name",
volunteersRequired: 0,
);
when(
mockEventService.updateVolunteerGroup({
'id': group.id,
'data': {
'eventId': "1",
'name': "Updated Group",
'volunteersRequired': 20,
},
}),
).thenThrow(Exception('Failed to update group'));
String log = "";
await runZonedGuarded(
() async {
await model.updateVolunteerGroup(group, "1", "Updated Group", 20);
},
(error, stack) {
expect(
error,
isA<Exception>(),
);
expect(error.toString(), "Failed to update group");
expect(stack, isNotNull);
},
zoneSpecification: ZoneSpecification(
print: (self, parent, zone, line) {
log = line;
},
),
);

expect(
log,
contains('Failed to update group'),
);
expect(group.name, "Old Name");
expect(group.volunteersRequired, 0);
});
});
}
Loading