cpd/test/widget_testing/listview_test.dart

84 lines
3.2 KiB
Dart

import 'package:cpd/database/todo_db.dart';
import 'package:cpd/widgets/edithabit_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:cpd/database/habit.dart';
import 'package:cpd/widgets/listview.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
void main() {
final todoDb = TodoDB();
setUpAll(() {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
});
testWidgets('MyListView displays and interacts correctly', (WidgetTester tester) async {
// Mock Habit data
final habits = [
Habit(id: 1, title: 'Habit 1', subtitle: 'Description 1', icon: Icons.star, isComplete: false),
Habit(id: 2, title: 'Habit 2', subtitle: 'Description 2', icon: Icons.star_border, isComplete: true),
];
// Build MyListView widget
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: MyListView(
habits: habits,
todoDB: todoDb,
fetchTodos: () {},
updateCounter: (int count) {},
),
),
));
// Verify the list view is displayed with the correct number of items
expect(find.byType(ListView), findsOneWidget);
expect(find.byType(ListTile), findsNWidgets(habits.length));
// Verify each habit is displayed correctly
for (final habit in habits) {
expect(find.text(habit.title), findsOneWidget);
expect(find.text(habit.subtitle), findsOneWidget);
expect(find.byIcon(habit.icon), findsOneWidget);
}
// Interact with the first habit checkbox
final firstHabitCheckbox = find.byType(Checkbox).at(0);
expect(firstHabitCheckbox, findsOneWidget);
expect((tester.widget(firstHabitCheckbox) as Checkbox).value, habits[0].isComplete);
await tester.tap(firstHabitCheckbox);
expect((tester.widget(firstHabitCheckbox) as Checkbox).value, habits[0].isComplete);
await tester.pump();
// Taps on the second checkbox to see if it was checked
final secondHabitCheckbox = find.byType(Checkbox).at(1);
expect(secondHabitCheckbox, findsOneWidget);
expect((tester.widget(secondHabitCheckbox) as Checkbox).value, habits[1].isComplete);
await tester.tap(secondHabitCheckbox);
expect((tester.widget(secondHabitCheckbox) as Checkbox).value, habits[1].isComplete);
await tester.pump();
// Swipe left to delete
final firstHabitListTile = find.byType(ListTile).at(0);
await tester.drag(firstHabitListTile, const Offset(-500.0, 0.0)); // Swipe left
await tester.pumpAndSettle(); // Wait for the swipe action to complete
expect(find.text("${habits[0].title} was dismissed"), findsOneWidget);
await tester.pumpAndSettle();
//Swipe right to edit
await tester.drag(firstHabitListTile, const Offset(500.0, 0.0)); // Swipe right
await tester.pumpAndSettle(); // Wait for the swipe action to complete
// Verify that the EditHabitDialog is displayed
expect(find.byType(EditHabitDialog), findsOneWidget);
//close the EditHabitDialog as it was already tested in edithabit_dialog_test.dart
final cancelButtonFinder = find.widgetWithText(TextButton, 'Cancel');
expect(cancelButtonFinder, findsOneWidget);
await tester.tap(cancelButtonFinder);
await tester.pumpAndSettle();
});
}