265 lines
6.9 KiB
Dart
265 lines
6.9 KiB
Dart
import 'package:bharat_erp/core/utils/validators.dart';
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
|
||
void main() {
|
||
group('email', () {
|
||
test('accepts valid addresses', () {
|
||
expect(Validators.email('user@example.com'), isNull);
|
||
expect(Validators.email('user.name+tag@company.co.in'), isNull);
|
||
});
|
||
|
||
test('rejects invalid addresses', () {
|
||
expect(Validators.email('not-an-email'), isNotNull);
|
||
expect(Validators.email('user@'), isNotNull);
|
||
});
|
||
|
||
test('optional allows empty', () {
|
||
expect(Validators.optionalEmail(''), isNull);
|
||
expect(Validators.optionalEmail('bad'), isNotNull);
|
||
});
|
||
});
|
||
|
||
group('accountNumber', () {
|
||
test('accepts 9–18 digits', () {
|
||
expect(Validators.accountNumber('123456789'), isNull);
|
||
expect(Validators.accountNumber('123456789012345678'), isNull);
|
||
});
|
||
|
||
test('rejects invalid lengths and non-digits', () {
|
||
expect(Validators.accountNumber('12345'), isNotNull);
|
||
expect(Validators.accountNumber('1234567890123456789'), isNotNull);
|
||
expect(Validators.accountNumber('12345ABC'), isNotNull);
|
||
});
|
||
});
|
||
|
||
group('ifsc', () {
|
||
test('accepts valid IFSC', () {
|
||
expect(Validators.ifsc('SBIN0001234'), isNull);
|
||
expect(Validators.ifsc('sbin0001234'), isNull);
|
||
});
|
||
|
||
test('rejects invalid IFSC', () {
|
||
expect(Validators.ifsc('SBIN001234'), isNotNull);
|
||
expect(Validators.ifsc('SBIN00012345'), isNotNull);
|
||
expect(Validators.ifsc('SB1N0001234'), isNotNull);
|
||
});
|
||
});
|
||
|
||
group('pincode', () {
|
||
test('accepts valid 6-digit pincode', () {
|
||
expect(Validators.pincode('560001'), isNull);
|
||
});
|
||
|
||
test('rejects invalid pincode', () {
|
||
expect(Validators.pincode('056001'), isNotNull);
|
||
expect(Validators.pincode('56001'), isNotNull);
|
||
expect(Validators.pincode('5600011'), isNotNull);
|
||
});
|
||
|
||
test('optional allows empty', () {
|
||
expect(Validators.optionalPincode(''), isNull);
|
||
});
|
||
});
|
||
|
||
group('forFieldKey', () {
|
||
test('routes email and pincode keys', () {
|
||
expect(
|
||
Validators.forFieldKey('email', 'a@b.com', required: true),
|
||
isNull,
|
||
);
|
||
expect(
|
||
Validators.forFieldKey('pincode', '560001', required: false),
|
||
isNull,
|
||
);
|
||
expect(
|
||
Validators.forFieldKey('ifsc', 'HDFC0001234', required: true),
|
||
isNull,
|
||
);
|
||
});
|
||
|
||
test('validates master name characters', () {
|
||
expect(
|
||
Validators.forFieldKey('name', 'Widget A-1', required: true),
|
||
isNull,
|
||
);
|
||
expect(
|
||
Validators.forFieldKey('name', 'Part (OEM) & Co.', required: true),
|
||
isNull,
|
||
);
|
||
expect(
|
||
Validators.forFieldKey('item_name', 'Item_01/Test', required: true),
|
||
isNull,
|
||
);
|
||
expect(
|
||
Validators.forFieldKey('name', 'Invalid@Name', required: true),
|
||
isNotNull,
|
||
);
|
||
});
|
||
});
|
||
|
||
group('uniqueMasterName', () {
|
||
test('rejects duplicate names case-insensitively', () {
|
||
expect(
|
||
Validators.uniqueMasterName(
|
||
'Widget A',
|
||
nameKey: 'name',
|
||
existingRecords: const [
|
||
{'id': '1', 'name': 'widget a'},
|
||
],
|
||
fieldName: 'Name',
|
||
),
|
||
isNotNull,
|
||
);
|
||
});
|
||
|
||
test('allows same name when editing current record', () {
|
||
expect(
|
||
Validators.uniqueMasterName(
|
||
'Widget A',
|
||
nameKey: 'name',
|
||
existingRecords: const [
|
||
{'id': '1', 'name': 'Widget A'},
|
||
],
|
||
currentRecordId: '1',
|
||
fieldName: 'Name',
|
||
),
|
||
isNull,
|
||
);
|
||
});
|
||
});
|
||
|
||
group('masterCode', () {
|
||
test('accepts valid code characters', () {
|
||
expect(
|
||
Validators.forFieldKey('code', 'ABC-12_test/item', required: true),
|
||
isNull,
|
||
);
|
||
expect(
|
||
Validators.forFieldKey('item_code', 'SKU_01/A', required: true),
|
||
isNull,
|
||
);
|
||
});
|
||
|
||
test('rejects invalid code characters', () {
|
||
expect(
|
||
Validators.forFieldKey('code', 'CODE@123', required: true),
|
||
isNotNull,
|
||
);
|
||
expect(
|
||
Validators.forFieldKey('code', 'CODE 123', required: true),
|
||
isNotNull,
|
||
);
|
||
});
|
||
});
|
||
|
||
group('uniqueMasterCode', () {
|
||
test('rejects duplicate codes case-insensitively', () {
|
||
expect(
|
||
Validators.uniqueMasterCode(
|
||
'UOM-01',
|
||
codeKey: 'code',
|
||
existingRecords: const [
|
||
{'id': '1', 'code': 'uom-01'},
|
||
],
|
||
fieldName: 'Code',
|
||
),
|
||
isNotNull,
|
||
);
|
||
});
|
||
|
||
test('allows same code when editing current record', () {
|
||
expect(
|
||
Validators.uniqueMasterCode(
|
||
'UOM-01',
|
||
codeKey: 'code',
|
||
existingRecords: const [
|
||
{'id': '1', 'code': 'UOM-01'},
|
||
],
|
||
currentRecordId: '1',
|
||
fieldName: 'Code',
|
||
),
|
||
isNull,
|
||
);
|
||
});
|
||
});
|
||
|
||
group('hsnCode', () {
|
||
test('accepts 4–8 digit codes', () {
|
||
expect(Validators.hsnCode('3402'), isNull);
|
||
expect(Validators.hsnCode('34029099'), isNull);
|
||
});
|
||
|
||
test('rejects non-digit or wrong length codes', () {
|
||
expect(Validators.hsnCode('340'), isNotNull);
|
||
expect(Validators.hsnCode('340290991'), isNotNull);
|
||
expect(Validators.hsnCode('HSN3402'), isNotNull);
|
||
});
|
||
|
||
test('rejects duplicate HSN codes', () {
|
||
expect(
|
||
Validators.uniqueHsnCode(
|
||
'34029099',
|
||
existingRecords: const [
|
||
{'id': '1', 'code': '34029099'},
|
||
],
|
||
),
|
||
isNotNull,
|
||
);
|
||
});
|
||
});
|
||
|
||
group('password', () {
|
||
test('accepts strong passwords', () {
|
||
expect(Validators.password('Abcdef1!'), isNull);
|
||
expect(Validators.password('Secure@Pass9'), isNull);
|
||
});
|
||
|
||
test('requires non-empty value', () {
|
||
expect(Validators.password(null), 'Password is required');
|
||
expect(Validators.password(''), 'Password is required');
|
||
});
|
||
|
||
test('enforces minimum length', () {
|
||
expect(
|
||
Validators.password('Ab1!xyz'),
|
||
'Password must be at least 8 characters',
|
||
);
|
||
});
|
||
|
||
test('requires uppercase letter', () {
|
||
expect(
|
||
Validators.password('abcdef1!'),
|
||
contains('uppercase'),
|
||
);
|
||
});
|
||
|
||
test('requires lowercase letter', () {
|
||
expect(
|
||
Validators.password('ABCDEF1!'),
|
||
contains('lowercase'),
|
||
);
|
||
});
|
||
|
||
test('requires numeric digit', () {
|
||
expect(
|
||
Validators.password('Abcdefg!'),
|
||
contains('numeric digit'),
|
||
);
|
||
});
|
||
|
||
test('requires special character', () {
|
||
expect(
|
||
Validators.password('Abcdefg1'),
|
||
contains('special character'),
|
||
);
|
||
});
|
||
|
||
test('optionalPassword allows empty', () {
|
||
expect(Validators.optionalPassword(null), isNull);
|
||
expect(Validators.optionalPassword(''), isNull);
|
||
expect(Validators.optionalPassword('Abcdefg1'), isNotNull);
|
||
expect(Validators.optionalPassword('Abcdef1!'), isNull);
|
||
});
|
||
});
|
||
}
|