Commit fba9b3d

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-11-24 18:14:10
review
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 903cdf5
Changed files (2)
lib
components
test
lib/components/dialoges/input_dialoge.dart
@@ -32,7 +32,6 @@ class InputDialoge extends StatefulWidget {
   /// and pressing of the submit button will be ignored.
   ///
   /// It is still possible to cancel a dialoge in case the validator fails.
-  /// TODO: test
   final String? Function(String)? validator;
 
   @override
@@ -106,6 +105,9 @@ Future<String?> showInputDialoge(BuildContext context, {String? hintText, String
   showDialog<String?>(context: context, builder: (context) =>
       InputDialoge(hintText: hintText, initialValue: initialValue,));
 
+/// Creates a dialoge that only allows int and double inputs.
+///
+/// Variables behave similar to [showInputDialoge].
 Future<double?> showNumberInputDialoge(BuildContext context, {String? hintText, num? initialValue}) async {
   final result = await showDialog<String?>(context: context, builder: (context) =>
     InputDialoge(
@@ -123,8 +125,7 @@ Future<double?> showNumberInputDialoge(BuildContext context, {String? hintText,
       },
     ));
 
-  if (result == null) return null;
-  double? value = double.tryParse(result);
-  value ??= int.tryParse(result)?.toDouble();
+  double? value = double.tryParse(result ?? '');
+  value ??= int.tryParse(result ?? '')?.toDouble();
   return value;
 }
\ No newline at end of file
test/ui/components/input_dialoge_test.dart
@@ -33,6 +33,48 @@ void main() {
       expect(find.text('initial text'), findsOneWidget);
       expect(find.text('test hint'), findsNWidgets(2));
     });
+    testWidgets('should show validator errors', (widgetTester) async {
+      await widgetTester.pumpWidget(MaterialApp(
+          localizationsDelegates: const [AppLocalizations.delegate,], locale: const Locale('en'),
+          home: InputDialoge(
+            initialValue: 'initial text',
+            validator: (_) => 'test error',
+          )
+      ));
+      final localizations = await AppLocalizations.delegate.load(const Locale('en'));
+
+      expect(find.text(localizations.btnConfirm), findsOneWidget);
+      await widgetTester.tap(find.text(localizations.btnConfirm));
+      await widgetTester.pumpAndSettle();
+
+      expect(find.byType(InputDialoge), findsOneWidget);
+      expect(find.text('test error'), findsOneWidget);
+    });
+    testWidgets('should send current text to validator', (widgetTester) async {
+      int validatorCalls = 0;
+      await widgetTester.pumpWidget(MaterialApp(
+          localizationsDelegates: const [AppLocalizations.delegate,], locale: const Locale('en'),
+          home: InputDialoge(
+            initialValue: 'initial text',
+            validator: (value) {
+              expect(value, 'initial text');
+              validatorCalls += 1;
+              return null;
+            },
+          )
+      ));
+      final localizations = await AppLocalizations.delegate.load(const Locale('en'));
+
+      expect(validatorCalls, 0);
+
+      expect(find.text(localizations.btnConfirm), findsOneWidget);
+      await widgetTester.tap(find.text(localizations.btnConfirm));
+      await widgetTester.pumpAndSettle();
+
+      expect(validatorCalls, 1);
+
+      expect(find.byType(InputDialoge), findsNothing);
+    });
   });
   group('showInputDialoge', () {
     testWidgets('should start with input focused', (widgetTester) async {