2024-05-28 21:59:44 +02:00
|
|
|
import 'package:cpd/database/db_interface.dart';
|
|
|
|
import 'package:cpd/database/todo_interface.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:cpd/database/habit.dart';
|
|
|
|
import 'package:cpd/pages/homepage.dart';
|
|
|
|
import 'package:mockito/annotations.dart';
|
|
|
|
import 'package:mockito/mockito.dart';
|
2024-05-30 14:28:21 +02:00
|
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
|
|
|
2024-05-28 21:59:44 +02:00
|
|
|
import 'habitcounter_test.mocks.dart';
|
|
|
|
|
|
|
|
@GenerateMocks([HabitDbInterface, ToDoInterface])
|
|
|
|
void main() {
|
2024-05-30 14:28:21 +02:00
|
|
|
setUpAll(() {
|
|
|
|
// Initialisierung von sqflite_ffi
|
|
|
|
sqfliteFfiInit();
|
|
|
|
databaseFactory = databaseFactoryFfi;
|
|
|
|
});
|
2024-05-28 21:59:44 +02:00
|
|
|
|
2024-05-30 14:28:21 +02:00
|
|
|
group('MyHomePage', ()
|
|
|
|
{
|
|
|
|
test('habitcounter returns correct count', () {
|
2024-05-28 21:59:44 +02:00
|
|
|
List<Habit> habits = [
|
2024-05-30 14:28:21 +02:00
|
|
|
Habit(id: 1,
|
|
|
|
title: 'Habit 1',
|
|
|
|
subtitle: 'Description 1',
|
2024-06-09 17:45:17 +02:00
|
|
|
iconCodePoint: Icons.star.codePoint,
|
|
|
|
iconFontFamily: Icons.star.fontFamily ?? 'MaterialIcons',
|
2024-05-30 14:28:21 +02:00
|
|
|
isComplete: false),
|
|
|
|
Habit(id: 2,
|
|
|
|
title: 'Habit 2',
|
|
|
|
subtitle: 'Description 2',
|
2024-06-09 17:45:17 +02:00
|
|
|
iconCodePoint: Icons.star_border.codePoint,
|
|
|
|
iconFontFamily: Icons.star_border.fontFamily ?? 'MaterialIcons',
|
2024-05-30 14:28:21 +02:00
|
|
|
isComplete: true),
|
|
|
|
Habit(id: 3,
|
|
|
|
title: 'Habit 3',
|
|
|
|
subtitle: 'Description 3',
|
2024-06-09 17:45:17 +02:00
|
|
|
iconCodePoint: Icons.favorite.codePoint,
|
|
|
|
iconFontFamily: Icons.favorite.fontFamily ?? 'MaterialIcons',
|
2024-05-30 14:28:21 +02:00
|
|
|
isComplete: true),
|
2024-05-28 21:59:44 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
final myHomePageState = MyHomePageState();
|
|
|
|
final habitCount = myHomePageState.countHabits(habits);
|
|
|
|
expect(habitCount, 3);
|
|
|
|
});
|
|
|
|
|
2024-05-30 14:28:21 +02:00
|
|
|
testWidgets(
|
|
|
|
'displays correct habit count text', (WidgetTester tester) async {
|
2024-05-28 21:59:44 +02:00
|
|
|
final mockToDoInterface = MockToDoInterface();
|
|
|
|
|
|
|
|
List<Habit> habits = [
|
2024-05-30 14:28:21 +02:00
|
|
|
Habit(id: 1,
|
|
|
|
title: 'Habit 1',
|
|
|
|
subtitle: 'Description 1',
|
2024-06-09 17:45:17 +02:00
|
|
|
iconCodePoint: Icons.star.codePoint,
|
|
|
|
iconFontFamily: Icons.star.fontFamily ?? 'MaterialIcons',
|
2024-05-30 14:28:21 +02:00
|
|
|
isComplete: false),
|
|
|
|
Habit(id: 2,
|
|
|
|
title: 'Habit 2',
|
|
|
|
subtitle: 'Description 2',
|
2024-06-09 17:45:17 +02:00
|
|
|
iconCodePoint: Icons.star_border.codePoint,
|
|
|
|
iconFontFamily: Icons.star_border.fontFamily ?? 'MaterialIcons',
|
2024-05-30 14:28:21 +02:00
|
|
|
isComplete: true),
|
|
|
|
Habit(id: 3,
|
|
|
|
title: 'Habit 3',
|
|
|
|
subtitle: 'Description 3',
|
2024-06-09 17:45:17 +02:00
|
|
|
iconCodePoint: Icons.favorite.codePoint,
|
|
|
|
iconFontFamily: Icons.favorite.fontFamily ?? 'MaterialIcons',
|
2024-05-30 14:28:21 +02:00
|
|
|
isComplete: true),
|
2024-05-28 21:59:44 +02:00
|
|
|
];
|
|
|
|
|
2024-05-30 14:28:21 +02:00
|
|
|
// Mocking the fetchAll method of mockToDoInterface
|
2024-05-28 21:59:44 +02:00
|
|
|
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits);
|
|
|
|
|
2024-05-30 14:28:21 +02:00
|
|
|
// Pumpe das Widget mit MaterialApp um den FutureBuilder zu umgeben
|
|
|
|
await tester.pumpWidget(
|
|
|
|
MaterialApp(
|
|
|
|
home: FutureBuilder<List<Habit>>(
|
|
|
|
future: mockToDoInterface.fetchAll(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
|
|
|
|
// Data is available, return a widget with the correct text
|
|
|
|
return Text('You have ${snapshot.data!.length} habits for today');
|
|
|
|
} else {
|
|
|
|
// Data is not available or in loading state, return a widget with 'No habits' text
|
|
|
|
return const Text('No habits');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2024-05-28 21:59:44 +02:00
|
|
|
|
2024-05-30 14:28:21 +02:00
|
|
|
await tester.pumpAndSettle();
|
|
|
|
// Überprüfe, ob der Text "You have x habits for today" angezeigt wird
|
|
|
|
expect(find.text('You have ${habits.length} habits for today'), findsOneWidget);
|
2024-05-28 21:59:44 +02:00
|
|
|
|
2024-05-30 14:28:21 +02:00
|
|
|
// Überprüfe, ob der Text "No habits" angezeigt wird, wenn keine Daten verfügbar sind
|
|
|
|
expect(find.text('No habits'), findsNothing);
|
2024-05-28 21:59:44 +02:00
|
|
|
|
|
|
|
verify(mockToDoInterface.fetchAll()).called(1);
|
|
|
|
verifyNoMoreInteractions(mockToDoInterface);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|