cpd/test/integration_testing/listview_features_test.dart

126 lines
4.1 KiB
Dart
Raw Normal View History

2024-05-30 19:43:56 +02:00
import 'package:cpd/database/db_interface.dart';
import 'package:cpd/widgets/edithabit_dialog.dart';
2024-05-30 19:43:56 +02:00
import 'package:cpd/widgets/listview.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:cpd/database/habit.dart';
import 'package:cpd/database/todo_interface.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import '../unit_testing/habitcounter_test.mocks.dart';
@GenerateMocks([ToDoInterface, HabitDbInterface])
void main() {
setUpAll(() {
// Initialisierung von sqflite_ffi
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
});
group('MyListView Tests', () {
final mockToDoInterface = MockToDoInterface();
testWidgets('Deletes a habit when swiped to the left', (WidgetTester tester) async {
// Arrange
final habits = [
Habit(id: 1, title: 'Test Habit 1', isComplete: true, subtitle: 'Subtitle 1', icon: Icons.check),
Habit(id: 2, title: 'Test Habit 2', isComplete: false, subtitle: 'Subtitle 2', icon: Icons.check),
];
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits);
when(mockToDoInterface.delete(any)).thenAnswer((_) async {});
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: MyListView(
habits: habits,
todoDB: mockToDoInterface,
fetchTodos: () {},
updateCounter: (int count) {},
2024-05-30 19:43:56 +02:00
),
),
));
await tester.pumpAndSettle(); // Warte, bis das FutureBuilder abgeschlossen ist
// Act
final habit = find.byType(ListTile).at(0);
expect(habit, findsOneWidget);
await tester.drag(habit, const Offset(-500.0, 0.0)); // Swipe left
await tester.pumpAndSettle();
// Assert
verify(mockToDoInterface.delete(1)).called(1);
});
testWidgets('Edits a habit when swiped to the right', (WidgetTester tester) async {
// Arrange
final habits = [
Habit(id: 1, title: 'Test Habit 1', isComplete: true, subtitle: 'Subtitle 1', icon: Icons.check),
Habit(id: 2, title: 'Test Habit 2', isComplete: false, subtitle: 'Subtitle 2', icon: Icons.check),
];
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits);
when(mockToDoInterface.update(id: 1, title: 'Test Habit 1', subtitle: 'Subtitle changed', icon: Icons.add)).thenAnswer((_) async => 1);
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: MyListView(
habits: habits,
todoDB: mockToDoInterface,
fetchTodos: () {},
updateCounter: (int count) {},
2024-05-30 19:43:56 +02:00
),
),
));
await tester.pumpAndSettle(); // Warte, bis das FutureBuilder abgeschlossen ist
// Act
final habit = find.byType(ListTile).at(0);
expect(habit, findsOneWidget);
await tester.drag(habit, const Offset(500.0, 0.0));
await tester.pumpAndSettle();
// Assert
expect(find.byType(EditHabitDialog), findsOneWidget);
});
testWidgets('Checkbox toggles completion status', (WidgetTester tester) async {
// Arrange
final habits = [
Habit(id: 1, title: 'Test Habit 1', isComplete: false, subtitle: 'Subtitle 1', icon: Icons.check),
];
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits);
when(mockToDoInterface.updateCompletionStatus(1, true)).thenAnswer((_) async => null);
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: MyListView(
habits: habits,
todoDB: mockToDoInterface,
fetchTodos: () {},
updateCounter: (int count) {},
2024-05-30 19:43:56 +02:00
),
),
));
await tester.pumpAndSettle(); // Warte, bis das FutureBuilder abgeschlossen ist
// Act
final checkboxFinder = find.byType(Checkbox).at(0);
expect(checkboxFinder, findsOneWidget);
await tester.tap(checkboxFinder);
await tester.pumpAndSettle();
// Assert
verify(mockToDoInterface.updateCompletionStatus(1, true)).called(1);
});
});
}