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 14 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 @@ -465,5 +465,99 @@ Future<void> main() async {
true,
);
});
testWidgets("Testing if firstName 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 '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 that the firstNameFocus is focused
final focusedElement =
FocusScope.of(tester.element(firstNameTextField)).focusedChild;
expect(focusedElement, isNotNull); // Ensure there is a focused child
expect(
focusedElement!.hasPrimaryFocus,
isTrue,
); // Ensure it has primary focus
});

testWidgets("Testing image selection and removal functionality",
(tester) async {
await mockNetworkImages(() async {
// Mock user data with no image
userConfig.updateUser(
User(firstName: 'Test', lastName: 'Test', email: '[email protected]'),
);

// Render the widget
await tester
.pumpWidget(createEditProfilePage(themeMode: ThemeMode.dark));
await tester.pumpAndSettle();

// Case 1: Image file is null (modal sheet for selection should appear)
final addRemoveButton = find.byKey(const Key('AddRemoveImageButton'));
expect(addRemoveButton, findsOneWidget);

// Ensure the button is visible before interacting
await tester.ensureVisible(addRemoveButton);
await tester.pumpAndSettle();

// Tap the button to trigger modal sheet for image selection
await tester.tap(addRemoveButton);
await tester.pumpAndSettle();

// Verify modal sheet appears with options
expect(find.text('Camera'), findsOneWidget);
expect(find.text('Gallery'), findsOneWidget);
expect(find.byIcon(Icons.camera_alt), findsOneWidget);
expect(find.byIcon(Icons.photo_library), findsOneWidget);

// Close the modal sheet
await tester.tap(find.text('Camera'));
await tester.pumpAndSettle();

// Mock setting an image file
final model = EditProfilePageViewModel();
model.imageFile = File('mockPath');

// Rebuild the widget to reflect the new state
await tester
.pumpWidget(createEditProfilePage(themeMode: ThemeMode.dark));
await tester.pumpAndSettle();

// Case 2: Image file is not null (image removal should be triggered)
await tester.ensureVisible(addRemoveButton);
await tester.tap(addRemoveButton);
await tester.pumpAndSettle();

// Mock removing the image file
model.imageFile = null;
model.notifyListeners();
await tester.pumpAndSettle();

// Verify the image file is removed
expect(model.imageFile, isNull);
});
});
MayankJha014 marked this conversation as resolved.
Show resolved Hide resolved
});
}
Loading