cpd/test/addhabit_popup_test.dart

73 lines
2.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:cpd/widgets/addhabit_popup.dart';
import 'package:cpd/pages/iconpage.dart';
void main() {
testWidgets('AddHabitPopup displays and submits a new habit', (WidgetTester tester) async {
bool submitted = false;
String submittedTitle = '';
String submittedSubtitle = '';
IconData submittedIcon = Icons.favorite;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (_) => AddHabitPopup(
onSubmit: (title, subtitle, icon) {
submitted = true;
submittedTitle = title;
submittedSubtitle = subtitle;
submittedIcon = icon;
},
),
);
},
child: const Text('Show AddHabitPopup'),
),
),
),
),
);
// Open the dialog
await tester.tap(find.text('Show AddHabitPopup'));
await tester.pumpAndSettle();
// Verify the AddHabitPopup is displayed
expect(find.text('Add task'), findsOneWidget);
// Enter title and subtitle
await tester.enterText(find.byType(TextFormField).at(0), 'New Habit');
await tester.enterText(find.byType(TextFormField).at(1), 'Description of the habit');
// Simulate selecting a new icon from the IconPage
IconData newIcon = Icons.star;
// Tap the 'Select Icon' button
await tester.tap(find.text('Select Icon'));
await tester.pumpAndSettle();
// Call the onIconSelected callback directly to simulate icon selection
(tester.firstWidget(find.byType(IconPage)) as IconPage).onIconSelected(newIcon);
await tester.pumpAndSettle();
// Verify the new icon is displayed
expect(find.byIcon(newIcon), findsOneWidget);
// Submit the form by pressing the Enter key
//await tester.testTextInput.receiveAction(TextInputAction.done);
//await tester.pumpAndSettle();
// Verify that the onSubmit callback is called
//expect(submitted, isTrue);
//expect(submittedTitle, 'New Habit');
//expect(submittedSubtitle, 'Description of the habit');
//expect(submittedIcon, newIcon);
});
}