cpd/test/widget_testing/listview_test.dart

77 lines
2.9 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 {
final habits = [
Habit(id: 1, title: 'Habit 1', subtitle: 'Description 1', iconCodePoint: Icons.star.codePoint,
iconFontFamily: Icons.star.fontFamily ?? 'MaterialIcons', isComplete: false),
Habit(id: 2, title: 'Habit 2', subtitle: 'Description 2', iconCodePoint: Icons.star_border.codePoint,
iconFontFamily: Icons.star_border.fontFamily ?? 'MaterialIcons', isComplete: true),
];
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: MyListView(
habits: habits,
todoDB: todoDb,
fetchTodos: () {},
updateCounter: (int count) {},
),
),
));
expect(find.byType(ListView), findsOneWidget);
expect(find.byType(ListTile), findsNWidgets(habits.length));
for (final habit in habits) {
expect(find.text(habit.title), findsOneWidget);
expect(find.text(habit.subtitle), findsOneWidget);
expect(find.byIcon(habit.icon), findsOneWidget);
}
final firstHabitCheckbox = find.byType(Checkbox).at(0);
expect(firstHabitCheckbox, findsOneWidget);
expect((tester.widget(firstHabitCheckbox) as Checkbox).value, habits[0].isComplete);
await tester.tap(firstHabitCheckbox);
await tester.pumpAndSettle();
expect((tester.widget(firstHabitCheckbox) as Checkbox).value, habits[0].isComplete);
final secondHabitCheckbox = find.byType(Checkbox).at(1);
expect(secondHabitCheckbox, findsOneWidget);
expect((tester.widget(secondHabitCheckbox) as Checkbox).value, habits[1].isComplete);
await tester.tap(secondHabitCheckbox);
await tester.pumpAndSettle();
expect((tester.widget(secondHabitCheckbox) as Checkbox).value, habits[1].isComplete);
final firstHabitListTile = find.byType(ListTile).at(0);
await tester.drag(firstHabitListTile, const Offset(-500.0, 0.0));
await tester.pumpAndSettle();
expect(find.text("${habits[0].title} was dismissed"), findsOneWidget);
await tester.drag(firstHabitListTile, const Offset(500.0, 0.0));
await tester.pumpAndSettle();
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();
});
}