Skip to content

Commit 2338cc8

Browse files
committed
Add test cases for the min value formatter
1 parent a7fd095 commit 2338cc8

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:stelaris/util/formatter/min_value_fomatter.dart';
4+
5+
void main() {
6+
testWidgets('TextField with MinValueFormatter behaves correctly',
7+
(tester) async {
8+
final controller = TextEditingController();
9+
10+
await tester.pumpWidget(
11+
MaterialApp(
12+
home: Scaffold(
13+
body: TextField(
14+
controller: controller,
15+
inputFormatters: [MinValueFormatter(1)],
16+
),
17+
),
18+
),
19+
);
20+
21+
// Enter "05"
22+
await tester.enterText(find.byType(TextField), '05');
23+
expect(controller.text, '5');
24+
25+
// Enter "0"
26+
await tester.enterText(find.byType(TextField), '0');
27+
expect(controller.text, '1'); // snapped to min
28+
29+
// Enter "21"
30+
await tester.enterText(find.byType(TextField), '21');
31+
expect(controller.text, '21');
32+
});
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:flutter/services.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:stelaris/util/formatter/min_value_fomatter.dart';
4+
5+
void main() {
6+
group('MinValueFormatter', () {
7+
const min = 1;
8+
final formatter = MinValueFormatter(min);
9+
10+
TextEditingValue format(String oldText, String newText, {int? cursor}) {
11+
return formatter.formatEditUpdate(
12+
TextEditingValue(
13+
text: oldText,
14+
selection: TextSelection.collapsed(offset: oldText.length),
15+
),
16+
TextEditingValue(
17+
text: newText,
18+
selection: TextSelection.collapsed(offset: cursor ?? newText.length),
19+
),
20+
);
21+
}
22+
23+
test('empty input resets to min', () {
24+
final result = format('2', '');
25+
expect(result.text, '1');
26+
expect(result.selection.baseOffset, '1'.length);
27+
});
28+
29+
test('leading zero removed', () {
30+
final result = format('', '01');
31+
expect(result.text, '1');
32+
expect(result.selection.baseOffset, 1);
33+
});
34+
35+
test('below min resets to min', () {
36+
final result = format('', '0');
37+
expect(result.text, '1');
38+
expect(result.selection.baseOffset, 1);
39+
});
40+
41+
test('valid number stays as is', () {
42+
final result = format('', '21');
43+
expect(result.text, '21');
44+
expect(result.selection.baseOffset, 2);
45+
});
46+
47+
test('multiple leading zeros collapse', () {
48+
final result = format('', '0007');
49+
expect(result.text, '7');
50+
expect(result.selection.baseOffset, 1);
51+
});
52+
});
53+
}

0 commit comments

Comments
 (0)