Skip to content

Commit

Permalink
Merge pull request #1141 from nrallakis/main
Browse files Browse the repository at this point in the history
Add widget tests for FormBuilderRangeSlider #1090
  • Loading branch information
deandreamatias authored Nov 5, 2022
2 parents 57cc1c8 + 663e39d commit 47acd8b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/src/fields/form_builder_range_slider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ class FormBuilderRangeSlider extends FormBuilderField<RangeValues> {
}) : super(builder: (FormFieldState<RangeValues?> field) {
final state = field as _FormBuilderRangeSliderState;
final effectiveNumberFormat = numberFormat ?? NumberFormat.compact();

if (initialValue == null) {
field.setValue(RangeValues(min, min));
}
return InputDecorator(
decoration: state.decoration,
child: Container(
Expand All @@ -145,7 +147,7 @@ class FormBuilderRangeSlider extends FormBuilderField<RangeValues> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RangeSlider(
values: field.value ?? RangeValues(min, min),
values: field.value!,
min: min,
max: max,
divisions: divisions,
Expand Down
62 changes: 62 additions & 0 deletions test/form_builder_range_slider_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_test/flutter_test.dart';

import 'form_builder_tester.dart';

void main() {
group('FormBuilderRangeSlider --', () {
testWidgets('basic', (WidgetTester tester) async {
const widgetName = 'formBuilderRangeSlider';
final testWidget = FormBuilderRangeSlider(
name: widgetName,
min: 10.0,
max: 20.0,
);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

expect(formSave(), isTrue);
expect(formValue<RangeValues?>(widgetName),
equals(const RangeValues(10.0, 10.0)));

// Inspired by https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/range_slider_test.dart
// Tap at the center of the slider.
final Offset topLeft =
tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0);
final Offset bottomRight =
tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0);
final Offset rightTarget = topLeft + (bottomRight - topLeft) * 0.5;
await tester.tapAt(rightTarget);

expect(formSave(), isTrue);
expect(formValue<RangeValues>(widgetName), const RangeValues(10.0, 15.0));
});

testWidgets('initial value', (WidgetTester tester) async {
const widgetName = 'formBuilderRangeSlider';
final testWidget = FormBuilderRangeSlider(
name: widgetName,
min: 10.0,
max: 20.0,
initialValue: const RangeValues(14.0, 18.0),
);
await tester.pumpWidget(buildTestableFieldWidget(testWidget));

expect(formSave(), isTrue);
expect(formValue<RangeValues?>(widgetName),
equals(const RangeValues(14.0, 18.0)));

// Inspired by https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/range_slider_test.dart
// Tap a small offset after the start of the slider.
final Offset topLeft =
tester.getTopLeft(find.byType(RangeSlider)).translate(24, 0);
final Offset bottomRight =
tester.getBottomRight(find.byType(RangeSlider)).translate(-24, 0);
final Offset leftTarget = topLeft + (bottomRight - topLeft) * 0.1;
await tester.tapAt(leftTarget);

expect(formSave(), isTrue);
expect(formValue<RangeValues>(widgetName), const RangeValues(11.0, 18.0));
});
});
}

0 comments on commit 47acd8b

Please sign in to comment.