cpd_2022_zi/test/unit_tests/date_service_test.dart

125 lines
4.4 KiB
Dart
Raw Permalink Normal View History

2023-03-06 21:54:32 +01:00
import 'package:flutter/material.dart';
2023-03-06 21:04:35 +01:00
import 'package:flutter_test/flutter_test.dart';
2023-03-06 21:54:32 +01:00
import 'package:shared_preferences/shared_preferences.dart';
2023-03-06 21:04:35 +01:00
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;
2023-03-06 21:04:35 +01:00
import 'package:timezone/timezone.dart';
2023-03-06 22:15:29 +01:00
import '../mock/settings_mock.dart';
2023-03-06 21:04:35 +01:00
2023-03-06 21:54:32 +01:00
void main() async {
tz.initializeTimeZones();
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences.setMockInitialValues(mockSettings);
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', () {
2023-03-06 21:54:32 +01:00
List<TZDateTime> tzDateTimes = createTZDateTimes(['Mong'], 10, 0);
expect(tzDateTimes, []);
});
test('returns list of valid dates', () {
List<TZDateTime> tzDateTimes = createTZDateTimes(['Montag'], 10, 0);
expect(tzDateTimes.length, greaterThan(0));
});
2023-03-06 21:54:32 +01:00
test('returns all the same days as today in next 6 weeks at 23:59', () {
List<String> selectedDays = [
weekDays.keys.elementAt(DateTime.now().weekday - 1)
];
List<TZDateTime> expected = [];
DateTime now = DateTime.now();
final Duration offset = now.timeZoneOffset;
final DateTime date =
DateTime(now.year, now.month, now.day, 23, 59, 0, 0, 0);
for (int i = 0; i <= trainingTime; i = i + 7) {
expected.add(
TZDateTime.local(date.year, date.month, date.day, 23, 59, 0, 0, 0)
.add(Duration(days: i))
// subtract offset since TZDateTime uses the UTC Timezone
.subtract(offset));
}
List<TZDateTime> result = createTZDateTimes(selectedDays, 23, 59);
expect(result, expected);
});
});
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.timer);
expect(duration, isNotNull);
});
});
2023-03-06 21:04:35 +01:00
});
group('Helper functions', () {
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'];
2023-03-06 21:54:32 +01:00
const selectedHours = 12;
const selectedMinutes = 30;
final result =
2023-03-06 21:54:32 +01:00
createTZDateTimes(selectedDays, selectedHours, selectedMinutes);
expect(result, isA<List<TZDateTime>>());
});
2023-03-06 21:04:35 +01:00
});
}