Skip to content

Commit

Permalink
added new tests cases for the ManageAgendaScreen to enhance testing c…
Browse files Browse the repository at this point in the history
…overage (#2696)

Co-authored-by: Satyam Jha <satyamj210@gmail.com>
  • Loading branch information
may-tas and Satyam Jha authored Jan 6, 2025
1 parent a80fdde commit b5b49cb
Showing 1 changed file with 117 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ignore_for_file: talawa_api_doc
import 'package:delightful_toast/toast/components/toast_card.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -15,6 +16,7 @@ import 'package:talawa/view_model/after_auth_view_models/event_view_models/event
import 'package:talawa/view_model/after_auth_view_models/event_view_models/explore_events_view_model.dart';
import 'package:talawa/view_model/lang_view_model.dart';
import 'package:talawa/views/after_auth_screens/events/create_agenda_item_page.dart';
import 'package:talawa/views/after_auth_screens/events/edit_agenda_item_page.dart';
import 'package:talawa/views/after_auth_screens/events/manage_agenda_items_screen.dart';
import 'package:talawa/views/base_view.dart';
import 'package:talawa/widgets/agenda_item_tile.dart';
Expand All @@ -26,9 +28,10 @@ Event getTestEvent({
bool isPublic = false,
bool viewOnMap = true,
bool asAdmin = false,
String id = "1",
}) {
return Event(
id: "1",
id: id,
title: "test_event",
creator: User(
id: asAdmin ? "xzy1" : "acb1",
Expand Down Expand Up @@ -63,7 +66,7 @@ Event getTestEvent({
);
}

Widget createManageAgendaScreen() {
Widget createManageAgendaScreen(String id) {
return BaseView<AppLanguage>(
onModelReady: (model) => model.initialize(),
builder: (context, langModel, child) {
Expand All @@ -75,6 +78,7 @@ Widget createManageAgendaScreen() {
isPublic: true,
viewOnMap: false,
asAdmin: true,
id: id,
),
"exploreEventViewModel": ExploreEventsViewModel(),
},
Expand Down Expand Up @@ -115,7 +119,7 @@ void main() {
group('ManageAgendaScreen Widget Tests', () {
testWidgets('Shows empty state when no agenda items',
(WidgetTester tester) async {
await tester.pumpWidget(createManageAgendaScreen());
await tester.pumpWidget(createManageAgendaScreen('1'));
await tester.pumpAndSettle();

expect(find.text('No agenda items yet'), findsOneWidget);
Expand Down Expand Up @@ -150,7 +154,7 @@ void main() {
when(eventService.fetchAgendaItems('1'))
.thenAnswer((_) async => mockResult);

await tester.pumpWidget(createManageAgendaScreen());
await tester.pumpWidget(createManageAgendaScreen('1'));
await tester.pumpAndSettle();

expect(find.text('No agenda items yet'), findsNothing);
Expand Down Expand Up @@ -185,7 +189,7 @@ void main() {
when(eventService.fetchAgendaItems('1'))
.thenAnswer((_) async => mockResult);

await tester.pumpWidget(createManageAgendaScreen());
await tester.pumpWidget(createManageAgendaScreen('1'));
await tester.pumpAndSettle();

final firstItemFinder = find.text('Agenda 1');
Expand All @@ -201,6 +205,112 @@ void main() {
await tester.pumpAndSettle();
});

testWidgets('Can navigate to EditAgendaItemPage and update agenda item',
(WidgetTester tester) async {
final mockResult = QueryResult(
source: QueryResultSource.network,
data: {
'agendaItemByEvent': [
{
'_id': '1',
'title': 'Agenda 1',
'duration': '1h',
'sequence': 1,
},
{
'_id': '2',
'title': 'Agenda 2',
'duration': '30m',
'sequence': 2,
},
],
},
options: QueryOptions(
document: gql(EventQueries().fetchAgendaItemsByEvent('2')),
),
);

when(eventService.fetchAgendaItems('2'))
.thenAnswer((_) async => mockResult);

await tester.pumpWidget(createManageAgendaScreen('2'));
await tester.pumpAndSettle();

// Find and tap the edit button
final Finder editButtonFinder =
find.byKey(const Key('edit_agenda_item2'));
await tester.tap(editButtonFinder);
await tester.pumpAndSettle();

// Verify navigation to EditAgendaItemPage
expect(find.byType(EditAgendaItemPage), findsOneWidget);

// Simulate returning without changes (true)
Navigator.of(tester.element(find.byType(EditAgendaItemPage))).pop(true);
await tester.pumpAndSettle();

// Verify that the agenda items are refreshed
verify(eventService.fetchAgendaItems('2')).called(2);
});

testWidgets('Delete agenda item shows toast notification',
(WidgetTester tester) async {
final mockResult = QueryResult(
source: QueryResultSource.network,
data: {
'agendaItemByEvent': [
{
'_id': '1',
'title': 'Agenda 1',
'duration': '1h',
'sequence': 1,
}
],
},
options: QueryOptions(
document: gql(EventQueries().fetchAgendaItemsByEvent('1')),
),
);

when(eventService.fetchAgendaItems('1'))
.thenAnswer((_) async => mockResult);
when(eventService.deleteAgendaItem({"removeAgendaItemId": "1"}))
.thenAnswer((_) async => true);

await tester.pumpWidget(createManageAgendaScreen('1'));
await tester.pumpAndSettle();

// Find and tap the delete button
final Finder deleteButtonFinder =
find.byKey(const Key('delete_agenda_item1'));
await tester.tap(deleteButtonFinder);

// Pump the widget to trigger the delete operation
await tester.pumpAndSettle();

// Verify toast appears with correct content
final Finder toastTitleFinder = find.descendant(
of: find.byType(ToastCard),
matching: find.byType(Text),
);

expect(toastTitleFinder, findsOneWidget);

final Text toastTitle = tester.widget<Text>(toastTitleFinder);
expect(toastTitle.data, 'Agenda item removed');

// Verify icon
expect(find.byIcon(Icons.error_outline), findsOneWidget);

// Verify toast styling
final ToastCard toastCard =
tester.widget<ToastCard>(find.byType(ToastCard));
expect(toastCard.color, Colors.black.withOpacity(0.8));

// Wait for auto-dismiss
await tester.pump(const Duration(seconds: 2));
});

testWidgets('Can delete agenda item', (WidgetTester tester) async {
final mockResult = QueryResult(
source: QueryResultSource.network,
Expand Down Expand Up @@ -229,15 +339,15 @@ void main() {
.thenAnswer((_) async => mockResult);
when(eventService.deleteAgendaItem({"removeAgendaItemId": '1'}))
.thenAnswer((_) async => true);
await tester.pumpWidget(createManageAgendaScreen());
await tester.pumpWidget(createManageAgendaScreen('1'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key("delete_agenda_item1")));
await tester.pump();
});

testWidgets('Can navigate to CreateAgendaItemPage',
(WidgetTester tester) async {
await tester.pumpWidget(createManageAgendaScreen());
await tester.pumpWidget(createManageAgendaScreen('1'));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('add_item_btn')));
await tester.pumpAndSettle();
Expand Down

0 comments on commit b5b49cb

Please sign in to comment.