diff --git a/lib/views/after_auth_screens/profile/edit_profile_page.dart b/lib/views/after_auth_screens/profile/edit_profile_page.dart index 96965dc30..8dcd77fdf 100644 --- a/lib/views/after_auth_screens/profile/edit_profile_page.dart +++ b/lib/views/after_auth_screens/profile/edit_profile_page.dart @@ -180,6 +180,7 @@ class _EditProfilePageState extends State { Flexible( // Text field for first name with value text of user's last name. child: TextFormField( + key: const Key('LastNameTextField'), controller: model.lastNameTextController, focusNode: model.lastNameFocus, keyboardType: TextInputType.name, diff --git a/test/views/after_auth_screens/events/create_custom_recurring_event_test.dart b/test/views/after_auth_screens/events/create_custom_recurring_event_test.dart index fbf53eb88..f8ed3c3c5 100644 --- a/test/views/after_auth_screens/events/create_custom_recurring_event_test.dart +++ b/test/views/after_auth_screens/events/create_custom_recurring_event_test.dart @@ -187,6 +187,20 @@ void main() { final isLastOccurrence = RecurrenceUtils.isLastOccurenceOfWeekDay(date); expect(isLastOccurrence, false); }); + + test( + 'isLastOccurenceOfWeekDay iterates through multiple days to find last occurrence', + () { + final date = DateTime(2022, 8, 29); // Last Monday of August 2022 + final isLastOccurrence = RecurrenceUtils.isLastOccurenceOfWeekDay(date); + expect(isLastOccurrence, true); // 29th August is the last Monday + }); + + test('isLastOccurenceOfWeekDay iterates and adjusts last occurrence', () { + final date = DateTime(2022, 3, 28); // Last Monday of March 2022 + final isLastOccurrence = RecurrenceUtils.isLastOccurenceOfWeekDay(date); + expect(isLastOccurrence, true); // 28th March is the last Monday + }); }); group('Test custom recurrence page.', () { testWidgets('Appbar is being rendered as expected.', (tester) async { @@ -413,25 +427,53 @@ void main() { }); }); - testWidgets('CustomWeekDaySelector', (tester) async { - final widget = createCustomRecurrenceScreen( - theme: TalawaTheme.darkTheme, + testWidgets( + 'CustomWeekDaySelector handles selecting and deselecting weekdays', + (tester) async { + final mockModel = CreateEventViewModel(); + mockModel.weekDays = {}; // Start with an empty set of selected weekdays. + + // Create the widget with the mock model + final widget = MaterialApp( + home: Scaffold( + body: CustomWeekDaySelector(model: mockModel), + ), ); + await tester.pumpWidget(widget); await tester.pump(); + // Verify the CustomWeekDaySelector widget is present expect(find.byType(CustomWeekDaySelector), findsOne); + // Tap "M" (Monday) to select it await tester.tap(find.text("M")); await tester.pumpAndSettle(); + + // Verify that "M" is selected and added to the model + expect(mockModel.weekDays, contains(WeekDays.monday)); + + // Tap "W" (Wednesday) to select it await tester.tap(find.text("W")); await tester.pumpAndSettle(); - expect(find.text("S"), findsNWidgets(2)); - expect(find.text("M"), findsNWidgets(1)); - expect(find.text("T"), findsNWidgets(2)); - expect(find.text("W"), findsNWidgets(1)); - expect(find.text("F"), findsNWidgets(1)); + // Verify that "W" is selected and added to the model + expect(mockModel.weekDays, contains(WeekDays.wednesday)); + + // Verify that unrelated weekdays remain unselected + expect(mockModel.weekDays, isNot(contains(WeekDays.sunday))); + expect(mockModel.weekDays, isNot(contains(WeekDays.friday))); + + // Tap "M" again to deselect it + await tester.tap(find.text("M")); + await tester.pumpAndSettle(); + + // Verify that "M" is removed from the model + expect(mockModel.weekDays, isNot(contains(WeekDays.monday))); + + // Final state checks + expect(mockModel.weekDays, contains(WeekDays.wednesday)); + expect(mockModel.weekDays.length, 1); }); testWidgets('EventEndOptions', (tester) async { diff --git a/test/widget_tests/after_auth_screens/profile/edit_profile_page_test.dart b/test/widget_tests/after_auth_screens/profile/edit_profile_page_test.dart index 652995996..79826304e 100644 --- a/test/widget_tests/after_auth_screens/profile/edit_profile_page_test.dart +++ b/test/widget_tests/after_auth_screens/profile/edit_profile_page_test.dart @@ -448,22 +448,75 @@ Future main() async { }); // Testing onPressed for firstName - testWidgets("Testing if firstName text field gets focus", (tester) async { + testWidgets("Testing if firstName text field gets focus on onPressed", + (tester) async { userConfig.updateUser( User(firstName: 'Test', lastName: 'Test', email: 'test@test.com'), ); + + // Render the widget await tester.pumpWidget(createEditProfilePage(themeMode: ThemeMode.dark)); await tester.pumpAndSettle(); - await tester.tap(find.byKey(const Key('FirstNameTextField'))); + + // Find the 'First Name' text field and its suffix icon + final firstNameTextField = find.byKey(const Key('FirstNameTextField')); + final editIconButton = find.descendant( + of: firstNameTextField, + matching: find.byIcon(Icons.edit), + ); + + // Ensure the text field and icon exist + expect(firstNameTextField, findsOneWidget); + expect(editIconButton, findsOneWidget); + + // Tap on the edit icon button to trigger focus + await tester.tap(editIconButton); await tester.pumpAndSettle(); - // Verify the focus + // Verify that the lastNameFocus is focused + final focusedElement = + FocusScope.of(tester.element(firstNameTextField)).focusedChild; + expect(focusedElement, isNotNull); // Ensure there is a focused child expect( - FocusScope.of(tester.element(find.byType(EditProfilePage))) - .focusedChild! - .hasPrimaryFocus, - true, + focusedElement!.hasPrimaryFocus, + isTrue, + ); // Ensure it h + }); + + testWidgets("Testing if lastName text field gets focus on onPressed", + (tester) async { + // Mock or set up user data + userConfig.updateUser( + User(firstName: 'Test', lastName: 'Test', email: 'test@test.com'), ); + + // Render the widget + await tester.pumpWidget(createEditProfilePage(themeMode: ThemeMode.dark)); + await tester.pumpAndSettle(); + + // Find the 'Last Name' text field and its suffix icon + final lastNameTextField = find.byKey(const Key('LastNameTextField')); + final editIconButton = find.descendant( + of: lastNameTextField, + matching: find.byIcon(Icons.edit), + ); + + // Ensure the text field and icon exist + expect(lastNameTextField, findsOneWidget); + expect(editIconButton, findsOneWidget); + + // Tap on the edit icon button to trigger focus + await tester.tap(editIconButton); + await tester.pumpAndSettle(); + + // Verify that the lastNameFocus is focused + final focusedElement = + FocusScope.of(tester.element(lastNameTextField)).focusedChild; + expect(focusedElement, isNotNull); // Ensure there is a focused child + expect( + focusedElement!.hasPrimaryFocus, + isTrue, + ); // Ensure it has primary focus }); }); }