From a80fddef82239ddbaac4a48570c120dcc4ad92a7 Mon Sep 17 00:00:00 2001 From: Mayank Jha <132004139+MayankJha014@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:21:43 +0530 Subject: [PATCH] Pull Request for Unit Tests of `edit_agenda_item_page.dart` (#2694) * add remove catergory test case * adding title and description validation * removed unused import * code ai improvement * fix it --- .../events/edit_agenda_item_page_test.dart | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) diff --git a/test/views/after_auth_screens/events/edit_agenda_item_page_test.dart b/test/views/after_auth_screens/events/edit_agenda_item_page_test.dart index d27386233..a655d52ae 100644 --- a/test/views/after_auth_screens/events/edit_agenda_item_page_test.dart +++ b/test/views/after_auth_screens/events/edit_agenda_item_page_test.dart @@ -1,4 +1,5 @@ // ignore_for_file: talawa_api_doc + import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -139,5 +140,177 @@ void main() { await tester.tap(find.text('Add Attachments')); await tester.pumpAndSettle(); }); + + testWidgets('Remove a selected category', (WidgetTester tester) async { + await tester.pumpWidget(createEditAgendaItemScreen()); + await tester.pumpAndSettle(); + + // Step 1: Select a category + await tester.tap(find.byType(DropdownButtonFormField)); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Category 1').last); + await tester.pumpAndSettle(); + + // Verify the category was added + expect(find.byKey(const Key('Category 1')), findsOneWidget); + + // Step 2: Remove the selected category + await tester.tap(find.byType(DropdownButtonFormField)); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Category 1').last); + await tester.pumpAndSettle(); + + // Verify the category was removed + expect(find.byKey(const Key('Category 1')), findsNothing); + }); + + testWidgets('Chip onDeleted removes a category', + (WidgetTester tester) async { + await tester.pumpWidget(createEditAgendaItemScreen()); + await tester.pumpAndSettle(); + + // Step 1: Add a category to the selected categories + await tester.tap(find.byType(DropdownButtonFormField)); + await tester.pumpAndSettle(); + + await tester.tap(find.text('Category 1').last); + await tester.pumpAndSettle(); + + // Verify the category was added and is displayed as a Chip + expect(find.byKey(const Key('Category 1')), findsOneWidget); + + // Step 2: Trigger the onDeleted callback by specifically finding the delete icon + final deleteButtonFinder = find.descendant( + of: find.byKey(const Key('Category 1')), + matching: find.byIcon( + Icons.cancel, + ), // Adjust to match the delete icon used in the Chip + ); + + expect( + deleteButtonFinder, + findsOneWidget, + ); // Ensure there's exactly one match + await tester.tap(deleteButtonFinder); + await tester.pumpAndSettle(); + + // Step 3: Verify the category was removed + expect(find.byKey(const Key('Category 1')), findsNothing); + }); + + testWidgets('TextFormField displays error message when input is empty', + (WidgetTester tester) async { + // Build the widget + await tester.pumpWidget(createEditAgendaItemScreen()); + await tester.pumpAndSettle(); + + // Find the TextFormField + final textFormField = find.byKey(const Key('edit_event_agenda_duration')); + expect(textFormField, findsOneWidget); + + // Simulate user entering empty text + await tester.enterText(textFormField, ''); + await tester.pumpAndSettle(); + + // Since we're not using a Form, we need to manually trigger the validator + // by accessing the FormFieldState and calling validate() + final formFieldState = + tester.state>(textFormField); + formFieldState.validate(); + await tester.pumpAndSettle(); + + // Check for the error message + expect(find.text('Please enter a duration'), findsOneWidget); + }); + + testWidgets('Agenda Item Title Validation', (WidgetTester tester) async { + // Title validation rules: + // - Must not be empty + // - Must contain at least one letter (A-Z or a-z) + await tester.pumpWidget(createEditAgendaItemScreen()); + await tester.pumpAndSettle(); + + // Locate the TextFormField using its Key + final titleField = find.byKey(const Key('edit_event_agenda_tf1')); + expect(titleField, findsOneWidget); + + // Simulate user entering empty text and trigger validation + await tester.enterText(titleField, ''); + await tester.pumpAndSettle(); + final emptyFieldState = tester.state>(titleField); + emptyFieldState.validate(); + await tester.pumpAndSettle(); + + // Verify the error message for an empty title + expect(find.text('Title must not be left blank.'), findsOneWidget); + + // Simulate user entering an invalid title (numbers only) and trigger validation + await tester.enterText(titleField, '12345'); + await tester.pumpAndSettle(); + final invalidFieldState = + tester.state>(titleField); + invalidFieldState.validate(); + await tester.pumpAndSettle(); + + // Verify the error message for an invalid title + expect(find.text('Invalid Title'), findsOneWidget); + + // Simulate user entering a valid title and trigger validation + await tester.enterText(titleField, 'Team Meeting'); + await tester.pumpAndSettle(); + final validFieldState = tester.state>(titleField); + validFieldState.validate(); + await tester.pumpAndSettle(); + + // Verify no error message is shown for valid title + expect(find.text('Invalid Title'), findsNothing); + expect(find.text('Title must not be left blank.'), findsNothing); + }); + + testWidgets('Agenda Item Description Validation', + (WidgetTester tester) async { + // Description validation rules: + // - Must not be empty + // - Must contain valid characters (add specific rules here) + await tester.pumpWidget(createEditAgendaItemScreen()); + await tester.pumpAndSettle(); + + // Locate the TextFormField using its Key + final descriptionField = find.byKey(const Key('edit_event_agenda_tf2')); + expect(descriptionField, findsOneWidget); + + // Simulate user entering an invalid description and trigger validation + await tester.enterText(descriptionField, ''); + await tester.pumpAndSettle(); + final emptyFieldState = + tester.state>(descriptionField); + emptyFieldState.validate(); + await tester.pumpAndSettle(); + + // Verify the error message for an empty description + expect(find.text('Description must not be left blank.'), findsOneWidget); + + // Simulate user entering an invalid title and trigger validation + await tester.enterText(descriptionField, '12345'); + await tester.pumpAndSettle(); + final invalidFieldState = + tester.state>(descriptionField); + invalidFieldState.validate(); + await tester.pumpAndSettle(); + + // Verify the error message for an invalid description + expect(find.text('Invalid Description'), findsOneWidget); + + // Test valid description + await tester.enterText(descriptionField, 'Valid Description'); + await tester.pumpAndSettle(); + final validFieldState = + tester.state>(descriptionField); + validFieldState.validate(); + await tester.pumpAndSettle(); + expect(find.text('Invalid Description'), findsNothing); + }); }); }