main
 1import 'package:blood_pressure_app/features/settings/enter_timeformat_dialoge.dart';
 2import 'package:flutter/material.dart';
 3import 'package:flutter_test/flutter_test.dart';
 4
 5import '../../util.dart';
 6
 7void main() {
 8  group('EnterTimeFormatDialoge', () {
 9    testWidgets('should initialize without errors', (tester) async {
10      await tester.pumpWidget(materialApp(const EnterTimeFormatDialoge(initialValue: 'yyyy-MM-dd HH:mm',)));
11      expect(tester.takeException(), isNull);
12      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
13    });
14    testWidgets('should prefill time format', (tester) async {
15      await tester.pumpWidget(materialApp( const EnterTimeFormatDialoge(initialValue: 'yyyy-MM-dd HH:mm',)));
16      final textField = find.byType(TextField);
17      expect(textField, findsOneWidget);
18      expect(find.descendant(of: textField, matching: find.text('yyyy-MM-dd HH:mm')), findsOneWidget);
19    });
20    testWidgets('should show preview', (tester) async {
21      await tester.pumpWidget(materialApp(EnterTimeFormatDialoge(
22          initialValue: 'yyyy-MM-dd HH:mm',
23          previewTime: DateTime(2023, 7, 23, 8, 20),
24        ),
25      ),);
26      expect(find.text('2023-07-23 08:20'), findsOneWidget);
27      
28      // other time formats
29      expect(find.byType(TextField), findsOneWidget);
30      await tester.enterText(find.byType(TextField), 'QQQQ + LLLL');
31      await tester.pumpAndSettle();
32      expect(find.text('3rd quarter + July'), findsOneWidget);
33    });
34    testWidgets('should close page on close button pressed', (tester) async {
35      await tester.pumpWidget(materialApp(const EnterTimeFormatDialoge(initialValue: 'yyyy-MM-dd HH:mm',)));
36
37      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
38      expect(find.byIcon(Icons.close), findsOneWidget);
39      await tester.tap(find.byIcon(Icons.close));
40      await tester.pumpAndSettle();
41      expect(find.byType(EnterTimeFormatDialoge), findsNothing);
42    });
43    testWidgets('should not allow saving empty time formats', (tester) async {
44      await tester.pumpWidget(materialApp(const EnterTimeFormatDialoge(initialValue: 'yyyy-MM-dd HH:mm',)));
45
46      await tester.enterText(find.byType(TextField), '');
47      await tester.pumpAndSettle();
48      expect(find.text('Please enter a value'), findsOneWidget);
49
50      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
51      await tester.tap(find.text('SAVE'));
52      expect(find.byType(EnterTimeFormatDialoge), findsOneWidget);
53    });
54  });
55
56  group('showTimeFormatPickerDialoge', () {
57    testWidgets('should return null on close', (tester) async {
58      String? result = 'notnull';
59      await loadDialoge(tester,
60              (context) async => result = await showTimeFormatPickerDialoge(context, 'yyyy-MM-dd HH:mm', false),);
61
62      expect(find.byIcon(Icons.close), findsOneWidget);
63      await tester.tap(find.byIcon(Icons.close));
64      await tester.pumpAndSettle();
65
66      expect(result, null);
67    });
68    testWidgets('should return value on save', (tester) async {
69      String? result;
70      await loadDialoge(tester,
71              (context) async => result = await showTimeFormatPickerDialoge(context, 'yyyy-MM-dd HH:mm', false),);
72
73      expect(find.text('SAVE'), findsOneWidget);
74      await tester.tap(find.text('SAVE'));
75      await tester.pumpAndSettle();
76
77      expect(result, 'yyyy-MM-dd HH:mm');
78    });
79    testWidgets('should return modified value on save', (tester) async {
80      String? result;
81      await loadDialoge(tester,
82              (context) async => result = await showTimeFormatPickerDialoge(context, 'yyyy-MM-dd HH:mm', false),);
83
84      await tester.enterText(find.byType(TextField), 'test text!');
85      await tester.pumpAndSettle();
86
87      expect(find.text('SAVE'), findsOneWidget);
88      await tester.tap(find.text('SAVE'));
89      await tester.pumpAndSettle();
90
91      expect(result, 'test text!');
92    });
93  });
94}