mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-06 07:54:02 +01:00
67 lines
2.0 KiB
Dart
67 lines
2.0 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:wger/core/validators.dart';
|
|
import 'package:wger/l10n/generated/app_localizations_en.dart';
|
|
|
|
void main() {
|
|
final i18n = AppLocalizationsEn();
|
|
|
|
test('Required field empty returns error', () {
|
|
final result = validateUrl('', i18n, required: true);
|
|
expect(result, i18n.enterValue);
|
|
});
|
|
|
|
test('Optional field empty returns no error', () {
|
|
final result = validateUrl('', i18n, required: false);
|
|
expect(result, isNull);
|
|
});
|
|
|
|
test('Invalid URL without http/https returns error', () {
|
|
final result = validateUrl('www.example.com', i18n);
|
|
expect(result, i18n.invalidUrl);
|
|
});
|
|
|
|
test('Invalid URL with wrong protocol returns error', () {
|
|
final result = validateUrl('gopher://', i18n);
|
|
expect(result, i18n.invalidUrl);
|
|
});
|
|
|
|
test('Valid http URL returns no error', () {
|
|
final result = validateUrl('http://example.com', i18n);
|
|
expect(result, isNull);
|
|
});
|
|
|
|
test('Valid https URL returns no error', () {
|
|
final result = validateUrl('https://example.com', i18n);
|
|
expect(result, isNull);
|
|
});
|
|
|
|
test('Scheme-only URL with empty host returns error', () {
|
|
// Uri.parse('http://') succeeds with an empty host; the validator must
|
|
// still reject it.
|
|
expect(validateUrl('http://', i18n), i18n.invalidUrl);
|
|
expect(validateUrl('https://', i18n), i18n.invalidUrl);
|
|
});
|
|
|
|
group('validateDateRange', () {
|
|
test('returns null when end is null', () {
|
|
expect(validateDateRange(DateTime(2024, 1, 1), null, i18n), isNull);
|
|
});
|
|
|
|
test('returns null when end is after start', () {
|
|
expect(validateDateRange(DateTime(2024, 1, 1), DateTime(2024, 2, 1), i18n), isNull);
|
|
});
|
|
|
|
test('returns null when end equals start', () {
|
|
final date = DateTime(2024, 1, 1);
|
|
expect(validateDateRange(date, date, i18n), isNull);
|
|
});
|
|
|
|
test('returns an error when end is before start', () {
|
|
expect(
|
|
validateDateRange(DateTime(2024, 2, 1), DateTime(2024, 1, 1), i18n),
|
|
i18n.endBeforeStart,
|
|
);
|
|
});
|
|
});
|
|
}
|