Commit 8128f99

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-10-10 20:48:10
add tests for color picker
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent ad2e734
Changed files (2)
lib
test
lib/components/color_picker.dart
@@ -3,11 +3,13 @@ import 'dart:async';
 import 'package:flutter/material.dart';
 import 'package:flutter_gen/gen_l10n/app_localizations.dart';
 
+/// A list of colors in circles where one can be selected at a time.
 class ColorPicker extends StatefulWidget {
+  /// Create a widget to select one color from a list.
   const ColorPicker({super.key,
+    required this.onColorSelected,
     this.availableColors,
     this.initialColor,
-    required this.onColorSelected,
     this.showTransparentColor = true,
     this.circleSize = 50
   });
test/ui/components/color_picker_test.dart
@@ -0,0 +1,41 @@
+import 'package:blood_pressure_app/components/color_picker.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+  group('ColorPicker', () {
+    testWidgets('should initialize without errors', (tester) async {
+      await tester.pumpWidget(_materialApp(ColorPicker(onColorSelected: (color) {})));
+      await tester.pumpWidget(_materialApp(ColorPicker(availableColors: const [], onColorSelected: (color) {})));
+      await tester.pumpWidget(_materialApp(ColorPicker(showTransparentColor: false, onColorSelected: (color) {})));
+      await tester.pumpWidget(_materialApp(ColorPicker(circleSize: 15, onColorSelected: (color) {})));
+      await tester.pumpWidget(_materialApp(ColorPicker(availableColors: const [], initialColor: Colors.red, onColorSelected: (color) {})));
+    });
+    testWidgets('should report correct picked color', (tester) async {
+      int onColorSelectedCallCount = 0;
+      await tester.pumpWidget(_materialApp(ColorPicker(onColorSelected: (color) {
+        expect(color, Colors.blue);
+        onColorSelectedCallCount += 1;
+      })));
+
+      final containers = find.byType(Container).evaluate();
+      final blueColor = containers.where((element) { // find widgets with color blue
+        final widget = (element.widget as Container);
+        final decoration = widget.decoration;
+        if (decoration != null && decoration is BoxDecoration) {
+          return decoration.color == Colors.blue;
+        }
+        return false;
+      });
+      expect(blueColor.length, 1);
+      await tester.tap(find.byWidget(blueColor.first.widget));
+      expect(onColorSelectedCallCount, 1);
+    });
+  });
+}
+
+Widget _materialApp(Widget child) {
+  return MaterialApp(
+    home: Scaffold(body: child),
+  );
+}
\ No newline at end of file