cpd/test/unit_testing/habitcounter_test.dart

100 lines
3.3 KiB
Dart
Raw Permalink Normal View History

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(() {
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),
];
when(mockToDoInterface.fetchAll()).thenAnswer((_) async => habits);
await tester.pumpWidget(
MaterialApp(
home: FutureBuilder<List<Habit>>(
future: mockToDoInterface.fetchAll(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
return Text('You have ${snapshot.data!.length} habits for today');
} else {
return const Text('No habits');
}
},
),
),
);
await tester.pumpAndSettle();
expect(find.text('You have ${habits.length} habits for today'), findsOneWidget);
expect(find.text('No habits'), findsNothing);
verify(mockToDoInterface.fetchAll()).called(1);
verifyNoMoreInteractions(mockToDoInterface);
});
});
}