92 lines
3.0 KiB
Dart
92 lines
3.0 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:nearle_pos/core/utils/formatters.dart';
|
|
import 'package:nearle_pos/core/utils/validators.dart';
|
|
|
|
void main() {
|
|
group('Validators.mobile', () {
|
|
test('accepts a valid Indian number', () {
|
|
expect(Validators.mobile('9876543210'), isNull);
|
|
expect(Validators.mobile('6000000000'), isNull);
|
|
});
|
|
|
|
test('rejects the wrong length', () {
|
|
expect(Validators.mobile(''), isNotNull);
|
|
expect(Validators.mobile('98765'), isNotNull);
|
|
expect(Validators.mobile('98765432101'), isNotNull);
|
|
});
|
|
|
|
test('rejects a leading digit below 6', () {
|
|
expect(Validators.mobile('1234567890'), isNotNull);
|
|
expect(Validators.mobile('5876543210'), isNotNull);
|
|
});
|
|
|
|
test('ignores separators', () {
|
|
expect(Validators.mobile('98765 43210'), isNull);
|
|
expect(Validators.mobile('98765-43210'), isNull);
|
|
});
|
|
});
|
|
|
|
group('Validators.emailOptional', () {
|
|
test('treats empty as valid, since email is optional', () {
|
|
expect(Validators.emailOptional(''), isNull);
|
|
expect(Validators.emailOptional(null), isNull);
|
|
});
|
|
|
|
test('accepts a well-formed address', () {
|
|
expect(Validators.emailOptional('a.b+tag@example.co.in'), isNull);
|
|
});
|
|
|
|
test('rejects a malformed address', () {
|
|
expect(Validators.emailOptional('not-an-email'), isNotNull);
|
|
expect(Validators.emailOptional('missing@tld'), isNotNull);
|
|
});
|
|
});
|
|
|
|
group('Validators.name', () {
|
|
test('requires at least two characters', () {
|
|
expect(Validators.name('Jo'), isNull);
|
|
expect(Validators.name('J'), isNotNull);
|
|
expect(Validators.name(' '), isNotNull);
|
|
});
|
|
});
|
|
|
|
group('Validators.isLikelyBarcode', () {
|
|
test('accepts a long digit string', () {
|
|
expect(Validators.isLikelyBarcode('8901234500011'), isTrue);
|
|
});
|
|
|
|
test('rejects short input and anything with letters', () {
|
|
expect(Validators.isLikelyBarcode('12345'), isFalse);
|
|
expect(Validators.isLikelyBarcode('milk'), isFalse);
|
|
expect(Validators.isLikelyBarcode('ABC1234567'), isFalse);
|
|
});
|
|
});
|
|
|
|
group('Formatters', () {
|
|
test('groups a mobile number into 5 + 5', () {
|
|
expect(Formatters.mobile('9876543210'), '98765 43210');
|
|
});
|
|
|
|
test('masks all but the last four digits', () {
|
|
expect(Formatters.maskedMobile('9876543210'), endsWith('3210'));
|
|
expect(Formatters.maskedMobile('9876543210'), isNot(contains('98765')));
|
|
});
|
|
|
|
test('derives initials', () {
|
|
expect(Formatters.initials('Abhishek'), 'A');
|
|
expect(Formatters.initials('Meena Lakshmi'), 'ML');
|
|
expect(Formatters.initials(' '), '?');
|
|
});
|
|
|
|
test('builds a sequential invoice number', () {
|
|
final n = Formatters.invoiceNumber(42, DateTime(2026, 7, 28));
|
|
expect(n, 'INV-2607-00042');
|
|
});
|
|
|
|
test('renders a percentage without noise decimals', () {
|
|
expect(Formatters.percent(0.05), '5%');
|
|
expect(Formatters.percent(0.185), '18.5%');
|
|
});
|
|
});
|
|
}
|