107 lines
3.8 KiB
Dart
107 lines
3.8 KiB
Dart
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';
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
|
|
import 'habitcounter_test.mocks.dart';
|
|
|
|
@GenerateMocks([HabitDbInterface, ToDoInterface])
|
|
void main() {
|
|
setUpAll(() {
|
|
// Initialisierung von sqflite_ffi
|
|
sqfliteFfiInit();
|
|
databaseFactory = databaseFactoryFfi;
|
|
});
|
|
|
|
group('MyHomePage', ()
|
|
{
|
|
test('habitcounter returns correct count', () {
|
|
List<Habit> 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),
|
|
Habit(id: 3,
|
|
title: 'Habit 3',
|
|
subtitle: 'Description 3',
|
|
iconCodePoint: Icons.favorite.codePoint,
|
|
iconFontFamily: Icons.favorite.fontFamily ?? 'MaterialIcons',
|
|
isComplete: true),
|
|
];
|
|
|
|
final myHomePageState = MyHomePageState();
|
|
final habitCount = myHomePageState.countHabits(habits);
|
|
expect(habitCount, 3);
|
|
});
|
|
|
|
testWidgets(
|
|
'displays correct habit count text', (WidgetTester tester) async {
|
|
final mockToDoInterface = MockToDoInterface();
|
|
|
|
List<Habit> 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),
|
|
Habit(id: 3,
|
|
title: 'Habit 3',
|
|
subtitle: 'Description 3',
|
|
iconCodePoint: Icons.favorite.codePoint,
|
|
iconFontFamily: Icons.favorite.fontFamily ?? 'MaterialIcons',
|
|
isComplete: true),
|
|
];
|
|
|
|
// Mocking the fetchAll method of mockToDoInterface
|
|
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits);
|
|
|
|
// 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');
|
|
}
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
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);
|
|
|
|
// Überprüfe, ob der Text "No habits" angezeigt wird, wenn keine Daten verfügbar sind
|
|
expect(find.text('No habits'), findsNothing);
|
|
|
|
verify(mockToDoInterface.fetchAll()).called(1);
|
|
verifyNoMoreInteractions(mockToDoInterface);
|
|
});
|
|
});
|
|
} |