Skip to content

Commit

Permalink
Unittest for lib/views/after_auth_screens/profile/edit_profile_page.d…
Browse files Browse the repository at this point in the history
…art (#2686)

* added

* adding const in it

* fixing linting issue

* fixing this issue

* inc code coverage

* revert some changes

* adding remove image coverage

* increasing recursive dialog test

* revert package.lock

* test change

* Revert pubspec.lock to match upstream/develop-postgres

* code coverga inc

* add lastname in edit_profile

* added trailing comma

* cahnge last name focus way

* testing image selection

* revert some change

* revert image modal fix

* increase coverage for firstName

---------

Co-authored-by: Mayankpulse333 <[email protected]>
  • Loading branch information
MayankJha014 and Mayankpulse333 authored Jan 4, 2025
1 parent bb59156 commit 1e0e627
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class _EditProfilePageState extends State<EditProfilePage> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,22 +448,75 @@ Future<void> 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: '[email protected]'),
);

// 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: '[email protected]'),
);

// 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
});
});
}

0 comments on commit 1e0e627

Please sign in to comment.