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

Unittest for lib/views/after_auth_screens/profile/edit_profile_page.dart #2686

Merged
Show file tree
Hide file tree
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
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
});
});
}
Loading