cpd_2022_zi/test/unit_tests/date_service_test.dart

119 lines
3.8 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:smoke_cess_app/services/date_service.dart';
import 'package:smoke_cess_app/services/pages_service.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart';
void main() {
group('Helpers', () {
group('isSameDay', () {
test('returns true when dates are the same day', () {
DateTime dateA = DateTime.now();
DateTime dateB = DateTime(dateA.year, dateA.month, dateA.day, 10, 0);
expect(isSameDay(dateA, dateB), true);
});
test('returns false when dates are different days', () {
DateTime dateA = DateTime.now();
DateTime dateB = DateTime(dateA.year, dateA.month, dateA.day + 1, 10, 0);
expect(isSameDay(dateA, dateB), false);
});
test('returns false when one date is null', () {
DateTime? dateA = DateTime.now();
DateTime? dateB;
expect(isSameDay(dateA, dateB), false);
});
});
group('createTZDateTimes', () {
test('returns empty list when selectedDays is null', () {
List<TZDateTime> tzDateTimes =
createTZDateTimes(null, 10, 0);
expect(tzDateTimes, []);
});
test('returns empty list when selectedHours is null', () {
List<TZDateTime> tzDateTimes =
createTZDateTimes(['Montag'], null, 0);
expect(tzDateTimes, []);
});
test('returns empty list when selectedMinutes is null', () {
List<TZDateTime> tzDateTimes =
createTZDateTimes(['Montag'], 10, null);
expect(tzDateTimes, []);
});
test('returns empty list when no valid dates are found', () {
List<TZDateTime> tzDateTimes =
createTZDateTimes(['Montag'], 10, 0);
expect(tzDateTimes, []);
});
test('returns list of valid dates', () {
List<TZDateTime> tzDateTimes =
createTZDateTimes(['Montag'], 10, 0);
expect(tzDateTimes.length, greaterThan(0));
});
});
group('getTimeTill', () {
test('returns time till next mood', () async {
Duration duration = await getTimeTill(Pages.mood);
expect(duration, isNotNull);
});
test('returns time till next sleep', () async {
Duration duration = await getTimeTill(Pages.sleep);
expect(duration, isNotNull);
});
//?
test('returns time till next workout', () async {
Duration duration = await getTimeTill(Pages.relapse);
expect(duration, isNotNull);
});
//?
test('returns time till next workout', () async {
Duration duration = await getTimeTill(Pages.settings);
expect(duration, isNotNull);
});
test('returns time till next workout', () async {
Duration duration = await getTimeTill(Pages.timer);
expect(duration, isNotNull);
});
});
});
group('Helper functions', () {
setUp(() async {
tz.initializeTimeZones();
});
test('isSameDay returns true if two dates are the same day', () {
final date1 = DateTime(2022, 3, 6);
final date2 = DateTime(2022, 3, 6, 15, 30);
final result = isSameDay(date1, date2);
expect(result, true);
});
test('isSameDay returns false if two dates are not the same day', () {
final date1 = DateTime(2022, 3, 6);
final date2 = DateTime(2022, 3, 7);
final result = isSameDay(date1, date2);
expect(result, false);
});
test('createTZDateTimes returns a list of TZDateTime objects', () async {
final selectedDays = ['Montag', 'Dienstag'];
final selectedHours = 12;
final selectedMinutes = 30;
final result = await createTZDateTimes(
selectedDays, selectedHours, selectedMinutes);
expect(result, isA<List<TZDateTime>>());
});
});
}