Commit 656205a

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-11-04 09:19:06
add tests
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent bda38a8
lib/screens/subsettings/enter_timeformat.dart → lib/components/dialoges/enter_timeformat.dart
@@ -6,11 +6,16 @@ import 'package:intl/intl.dart';
 
 /// Dialoge that explains the time format and pops the context with either null or a time format string.
 class EnterTimeFormatDialoge extends StatefulWidget {
-  const EnterTimeFormatDialoge({super.key, required this.initialValue});
+  const EnterTimeFormatDialoge({super.key, required this.initialValue, this.previewTime});
 
   /// Text that is initially in time format field.
   final String initialValue;
 
+  /// Timestamp used to generate time format preview.
+  ///
+  /// When previewTime is null [DateTime.now] will be used.
+  final DateTime? previewTime;
+
   @override
   State<EnterTimeFormatDialoge> createState() => _EnterTimeFormatDialogeState();
 }
@@ -68,7 +73,7 @@ class _EnterTimeFormatDialogeState extends State<EnterTimeFormatDialoge> {
                 physics: const NeverScrollableScrollPhysics(),
                 data: localizations.enterTimeFormatDesc
               ),
-              Text(DateFormat(timeFormatFieldController.text).format(DateTime.now())),
+              Text(DateFormat(timeFormatFieldController.text).format(widget.previewTime ?? DateTime.now())),
               Padding(
                 padding: const EdgeInsets.all(12),
                 child: TextField(
lib/components/input_dialoge.dart → lib/components/dialoges/input_dialoge.dart
File renamed without changes
lib/screens/subsettings/graph_markings.dart
@@ -1,5 +1,5 @@
 import 'package:blood_pressure_app/components/color_picker.dart';
-import 'package:blood_pressure_app/components/input_dialoge.dart';
+import 'package:blood_pressure_app/components/dialoges/input_dialoge.dart';
 import 'package:blood_pressure_app/model/horizontal_graph_line.dart';
 import 'package:blood_pressure_app/model/storage/settings_store.dart';
 import 'package:flutter/material.dart';
lib/screens/settings.dart
@@ -1,14 +1,14 @@
 import 'dart:io';
 
 import 'package:blood_pressure_app/components/consistent_future_builder.dart';
-import 'package:blood_pressure_app/components/input_dialoge.dart';
+import 'package:blood_pressure_app/components/dialoges/enter_timeformat.dart';
+import 'package:blood_pressure_app/components/dialoges/input_dialoge.dart';
 import 'package:blood_pressure_app/components/settings_widgets.dart';
 import 'package:blood_pressure_app/model/blood_pressure.dart';
 import 'package:blood_pressure_app/model/iso_lang_names.dart';
 import 'package:blood_pressure_app/model/storage/settings_store.dart';
 import 'package:blood_pressure_app/platform_integration/platform_client.dart';
 import 'package:blood_pressure_app/screens/subsettings/delete_data.dart';
-import 'package:blood_pressure_app/screens/subsettings/enter_timeformat.dart';
 import 'package:blood_pressure_app/screens/subsettings/export_import_screen.dart';
 import 'package:blood_pressure_app/screens/subsettings/graph_markings.dart';
 import 'package:blood_pressure_app/screens/subsettings/version.dart';
test/ui/components/enter_timeformat_dialoge_test.dart
@@ -0,0 +1,145 @@
+import 'package:blood_pressure_app/components/dialoges/enter_timeformat.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_gen/gen_l10n/app_localizations.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+  group('EnterTimeFormatDialoge', () {
+    testWidgets('should initialize without errors', (widgetTester) async {
+      await widgetTester.pumpWidget(const MaterialApp(
+        localizationsDelegates: [AppLocalizations.delegate,],
+        locale: Locale('en'),
+        home: EnterTimeFormatDialoge(initialValue: 'yyyy-MM-dd HH:mm',)
+      ));
+      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
+    });
+    testWidgets('should prefill time format', (widgetTester) async {
+      await widgetTester.pumpWidget(const MaterialApp(
+          localizationsDelegates: [AppLocalizations.delegate,],
+          locale: Locale('en'),
+          home: EnterTimeFormatDialoge(initialValue: 'yyyy-MM-dd HH:mm',)
+      ));
+      final textField = find.byType(TextField);
+      expect(textField, findsOneWidget);
+      expect(find.descendant(of: textField, matching: find.text('yyyy-MM-dd HH:mm')), findsOneWidget);
+    });
+    testWidgets('should show preview', (widgetTester) async {
+      await widgetTester.pumpWidget(MaterialApp(
+          localizationsDelegates: const [AppLocalizations.delegate,],
+          locale: const Locale('en'),
+          home: EnterTimeFormatDialoge(
+            initialValue: 'yyyy-MM-dd HH:mm',
+            previewTime: DateTime(2023, 7, 23, 8, 20),
+          )
+      ));
+      expect(find.text('2023-07-23 08:20'), findsOneWidget);
+      
+      // other time formats
+      expect(find.byType(TextField), findsOneWidget);
+      await widgetTester.enterText(find.byType(TextField), 'QQQQ + LLLL');
+      await widgetTester.pumpAndSettle();
+      expect(find.text('3rd quarter + July'), findsOneWidget);
+    });
+    testWidgets('should close page on button pressed', (widgetTester) async {
+      await widgetTester.pumpWidget(const MaterialApp(
+          localizationsDelegates: [AppLocalizations.delegate,],
+          locale: Locale('en'),
+          home: EnterTimeFormatDialoge(
+            initialValue: 'yyyy-MM-dd HH:mm',
+          )
+      ));
+
+      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
+      expect(find.byIcon(Icons.close), findsOneWidget);
+      await widgetTester.tap(find.byIcon(Icons.close));
+      await widgetTester.pumpAndSettle();
+      expect(find.byType(EnterTimeFormatDialoge), findsNothing);
+    });
+    testWidgets('should not allow saving empty time formats', (widgetTester) async {
+      await widgetTester.pumpWidget(const MaterialApp(
+          localizationsDelegates: [AppLocalizations.delegate,],
+          locale: Locale('en'),
+          home: EnterTimeFormatDialoge(
+            initialValue: 'yyyy-MM-dd HH:mm',
+          )
+      ));
+
+      await widgetTester.enterText(find.byType(TextField), '');
+      await widgetTester.pumpAndSettle();
+      expect(find.text('Please enter a value'), findsOneWidget);
+
+      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
+      await widgetTester.tap(find.text('SAVE'));
+      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
+    });
+  });
+
+  group('showTimeFormatPickerDialoge', () {
+    testWidgets('should return null on close', (widgetTester) async {
+      String? result = 'notnull';
+      await widgetTester.pumpWidget(MaterialApp(
+        localizationsDelegates: const [AppLocalizations.delegate,],
+        locale: const Locale('en'),
+        home: Builder(
+          builder: (BuildContext context) => TextButton(onPressed: () async {
+            result = await showTimeFormatPickerDialoge(context, 'yyyy-MM-dd HH:mm');
+          }, child: const Text('TEST')),
+        ),
+      ));
+
+      await widgetTester.tap(find.text('TEST'));
+      await widgetTester.pumpAndSettle();
+
+      expect(find.byIcon(Icons.close), findsOneWidget);
+      await widgetTester.tap(find.byIcon(Icons.close));
+      await widgetTester.pumpAndSettle();
+
+      expect(result, null);
+    });
+    testWidgets('should return value on save', (widgetTester) async {
+      String? result;
+      await widgetTester.pumpWidget(MaterialApp(
+        localizationsDelegates: const [AppLocalizations.delegate,],
+        locale: const Locale('en'),
+        home: Builder(
+          builder: (BuildContext context) => TextButton(onPressed: () async {
+            result = await showTimeFormatPickerDialoge(context, 'yyyy-MM-dd HH:mm');
+          }, child: const Text('TEST')),
+        ),
+      ));
+
+      await widgetTester.tap(find.text('TEST'));
+      await widgetTester.pumpAndSettle();
+
+      expect(find.text('SAVE'), findsOneWidget);
+      await widgetTester.tap(find.text('SAVE'));
+      await widgetTester.pumpAndSettle();
+
+      expect(result, 'yyyy-MM-dd HH:mm');
+    });
+    testWidgets('should return modified value on save', (widgetTester) async {
+      String? result;
+      await widgetTester.pumpWidget(MaterialApp(
+        localizationsDelegates: const [AppLocalizations.delegate,],
+        locale: const Locale('en'),
+        home: Builder(
+          builder: (BuildContext context) => TextButton(onPressed: () async {
+            result = await showTimeFormatPickerDialoge(context, 'yyyy-MM-dd HH:mm');
+          }, child: const Text('TEST')),
+        ),
+      ));
+
+      await widgetTester.tap(find.text('TEST'));
+      await widgetTester.pumpAndSettle();
+
+      await widgetTester.enterText(find.byType(TextField), 'test text!');
+      await widgetTester.pumpAndSettle();
+
+      expect(find.text('SAVE'), findsOneWidget);
+      await widgetTester.tap(find.text('SAVE'));
+      await widgetTester.pumpAndSettle();
+
+      expect(result, 'test text!');
+    });
+  });
+}
\ No newline at end of file
test/ui/navigation_test.dart
@@ -1,3 +1,4 @@
+import 'package:blood_pressure_app/components/dialoges/enter_timeformat.dart';
 import 'package:blood_pressure_app/main.dart';
 import 'package:blood_pressure_app/model/blood_pressure.dart';
 import 'package:blood_pressure_app/model/ram_only_implementations.dart';
@@ -9,7 +10,6 @@ import 'package:blood_pressure_app/model/storage/settings_store.dart';
 import 'package:blood_pressure_app/screens/add_measurement.dart';
 import 'package:blood_pressure_app/screens/settings.dart';
 import 'package:blood_pressure_app/screens/statistics.dart';
-import 'package:blood_pressure_app/screens/subsettings/enter_timeformat.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_test/flutter_test.dart';
 import 'package:provider/provider.dart';