Share the quick steppers and the date-time pair

This commit is contained in:
Roland Geider
2026-08-07 22:05:48 +02:00
parent d6de435c6b
commit 62e06d63a4
7 changed files with 312 additions and 191 deletions

View File

@@ -177,4 +177,38 @@ void main() {
expect(find.text('1/5/2021'), findsOneWidget);
});
});
group('DateTimeInputWidget', () {
testWidgets('reports the whole moment, keeping the half picked before', (
WidgetTester tester,
) async {
// The forms collect what they are told without rebuilding, so the
// second pick must not be applied to the moment they started with
DateTime? reported;
await tester.pumpWidget(
wrap(
DateTimeInputWidget(
value: DateTime(2021, 1, 1, 15, 30),
onChanged: (value) => reported = value,
),
),
);
await tester.pumpAndSettle();
// Straight through the two halves, whose own pickers are covered above
tester.widget<DateInputWidget>(find.byType(DateInputWidget)).onChanged(DateTime(2021, 1, 5));
await tester.pumpAndSettle();
expect(reported, DateTime(2021, 1, 5, 15, 30));
tester
.widget<TimeInputWidget>(find.byType(TimeInputWidget))
.onChanged(const TimeOfDay(hour: 9, minute: 45));
await tester.pumpAndSettle();
// The day of the first pick, the time of the second
expect(reported, DateTime(2021, 1, 5, 9, 45));
expect(find.text('1/5/2021'), findsOneWidget);
expect(find.text('9:45 AM'), findsOneWidget);
});
});
}

View File

@@ -142,4 +142,87 @@ void main() {
expect(formKey.currentState!.validate(), isTrue);
});
group('steppers', () {
Widget wrapStepped({
num? value = 80,
String locale = 'en',
num? min,
num? max,
required ValueChanged<num?> onChanged,
}) => wrap(
DecimalInputWidget(
value: value,
labelText: 'X',
min: min,
max: max,
steppers: const [1, 0.1],
onChanged: onChanged,
),
locale: locale,
);
testWidgets('a field without them has no buttons', (WidgetTester tester) async {
await tester.pumpWidget(
wrap(DecimalInputWidget(value: 80, labelText: 'X', onChanged: (_) {})),
);
await tester.pumpAndSettle();
expect(find.byType(IconButton), findsNothing);
});
testWidgets('each button steps the field by its size and reports it', (
WidgetTester tester,
) async {
num? captured;
await tester.pumpWidget(wrapStepped(onChanged: (value) => captured = value));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('stepper-minus-0')));
await tester.pumpAndSettle();
expect(find.text('79'), findsOneWidget);
expect(captured, 79);
await tester.tap(find.byKey(const Key('stepper-plus-1')));
await tester.pumpAndSettle();
expect(find.text('79.1'), findsOneWidget);
expect(captured, 79.1);
});
testWidgets('steps in the format of the active locale', (WidgetTester tester) async {
await tester.pumpWidget(wrapStepped(locale: 'de', onChanged: (_) {}));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('stepper-minus-1')));
await tester.pumpAndSettle();
expect(find.text('79,9'), findsOneWidget);
});
testWidgets('a step out of the valid range is ignored', (WidgetTester tester) async {
num? captured;
await tester.pumpWidget(
wrapStepped(value: 80, min: 80, max: 80.5, onChanged: (value) => captured = value),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('stepper-minus-0')));
await tester.tap(find.byKey(const Key('stepper-plus-0')));
await tester.pumpAndSettle();
expect(find.text('80'), findsOneWidget);
expect(captured, isNull);
});
testWidgets('an empty field has nothing to step', (WidgetTester tester) async {
await tester.pumpWidget(wrapStepped(value: null, onChanged: (_) {}));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('stepper-plus-0')));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.text('1'), findsNothing);
});
});
}

View File

@@ -95,16 +95,16 @@ void main() {
await tester.pumpWidget(createWeightForm(entry: testWeightEntry1));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('quickMinus')));
await tester.tap(find.byKey(const Key('stepper-minus-0')));
expect(find.text('79'), findsOneWidget);
await tester.tap(find.byKey(const Key('quickMinusSmall')));
await tester.tap(find.byKey(const Key('stepper-minus-1')));
expect(find.text('78.9'), findsOneWidget);
await tester.tap(find.byKey(const Key('quickPlus')));
await tester.tap(find.byKey(const Key('stepper-plus-0')));
expect(find.text('79.9'), findsOneWidget);
await tester.tap(find.byKey(const Key('quickPlusSmall')));
await tester.tap(find.byKey(const Key('stepper-plus-1')));
expect(find.text('80'), findsOneWidget);
});
@@ -117,7 +117,7 @@ void main() {
expect(find.text('shiba inu'), findsNothing);
// Quick-change still works on the now-empty field
await tester.tap(find.byKey(const Key('quickMinus')));
await tester.tap(find.byKey(const Key('stepper-minus-0')));
await tester.pumpAndSettle();
expect(find.text('shiba inu'), findsNothing);
});