Skip to content

Commit 70ec280

Browse files
committed
Tests
1 parent f063d81 commit 70ec280

File tree

4 files changed

+167
-6
lines changed

4 files changed

+167
-6
lines changed

test/src/core/log_validator_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,31 @@ void main() {
7373
// Act & Assert
7474
expect(() => validator.validate(value), prints('$customErrorMessage\n'));
7575
});
76+
77+
// Additional test to cover debugPrint(errorText);
78+
test(
79+
'should log the default errorText if valueCandidate is null and errorText is not provided',
80+
() {
81+
// Arrange
82+
const LogValidator<String> validator = LogValidator<String>();
83+
const String? value = null;
84+
85+
// Act & Assert
86+
expect(() => validator.validate(value),
87+
prints('${FormBuilderLocalizations.current.requiredErrorText}\n'));
88+
});
89+
90+
test(
91+
'should log the custom errorText if valueCandidate is null and errorText is provided',
92+
() {
93+
// Arrange
94+
final LogValidator<String> validator = LogValidator<String>(
95+
errorText: customErrorMessage,
96+
);
97+
const String? value = null;
98+
99+
// Act & Assert
100+
expect(() => validator.validate(value), prints('$customErrorMessage\n'));
101+
});
76102
});
77103
}

test/src/datetime/date_time_validator_test.dart

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void main() {
3232
result,
3333
equals(FormBuilderLocalizations.current.dateStringErrorText),
3434
);
35-
expect(result, validator.errorText);
3635
});
3736

3837
test('should return the custom error message when the value is null', () {
@@ -92,7 +91,6 @@ void main() {
9291
result,
9392
equals(FormBuilderLocalizations.current.dateStringErrorText),
9493
);
95-
expect(result, validator.errorText);
9694
});
9795

9896
test(
@@ -127,7 +125,10 @@ void main() {
127125
expect(result, equals(customErrorMessage));
128126
});
129127

130-
test('should return the default error message when the value is null', () {
128+
// Additional tests to cover return errorText;
129+
test(
130+
'should return the default error message when the value is an invalid DateTime',
131+
() {
131132
// Arrange
132133
const DateTimeValidator validator = DateTimeValidator();
133134
const DateTime? value = null;
@@ -137,10 +138,39 @@ void main() {
137138

138139
// Assert
139140
expect(
140-
result,
141-
equals(FormBuilderLocalizations.current.dateStringErrorText),
141+
result, equals(FormBuilderLocalizations.current.dateStringErrorText));
142+
});
143+
144+
test(
145+
'should return the custom error message when the value is an invalid DateTime',
146+
() {
147+
// Arrange
148+
final DateTimeValidator validator = DateTimeValidator(
149+
errorText: customErrorMessage,
150+
);
151+
const DateTime? value = null;
152+
153+
// Act
154+
final String? result = validator.validate(value);
155+
156+
// Assert
157+
expect(result, equals(customErrorMessage));
158+
});
159+
160+
test(
161+
'should return the custom error message when the value is an empty string',
162+
() {
163+
// Arrange
164+
final DateTimeValidator validator = DateTimeValidator(
165+
errorText: customErrorMessage,
142166
);
143-
expect(result, validator.errorText);
167+
const String value = '';
168+
169+
// Act
170+
final String? result = validator.validate(DateTime.tryParse(value));
171+
172+
// Assert
173+
expect(result, equals(customErrorMessage));
144174
});
145175
});
146176
}

test/src/form_field_validator_extensions_test.dart

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,96 @@ void main() {
381381
expect(validator(null), isNotNull);
382382
expect(validator(' '), isNotNull);
383383
});
384+
385+
test('FormFieldValidatorExtensions.transform', () {
386+
// Arrange
387+
final FormFieldValidator<String> validator =
388+
FormBuilderValidators.transform<String>(
389+
(String? value) => value?.trim() ?? '',
390+
FormBuilderValidators.required(),
391+
);
392+
393+
// Act & Assert
394+
// Pass
395+
expect(validator(' trimmed '), isNull);
396+
397+
// Fail
398+
expect(validator(' '), isNotNull);
399+
expect(validator(null), isNotNull);
400+
expect(validator(''), isNotNull);
401+
402+
final FormFieldValidator<String> validatorWithErrorMessage =
403+
FormBuilderValidators.transform<String>(
404+
(String? value) => value?.trim() ?? '',
405+
FormBuilderValidators.required(errorText: customErrorMessage),
406+
);
407+
408+
// Pass
409+
expect(validatorWithErrorMessage(' trimmed '), isNull);
410+
411+
// Fail
412+
expect(validatorWithErrorMessage(' '), customErrorMessage);
413+
});
414+
415+
test('FormFieldValidatorExtensions.skipWhen with custom condition', () {
416+
// Arrange
417+
final FormFieldValidator<String> validator =
418+
FormBuilderValidators.skipWhen<String>(
419+
(String? value) => value == 'skip',
420+
FormBuilderValidators.required(),
421+
);
422+
423+
// Act & Assert
424+
// Pass
425+
expect(validator('skip'), isNull);
426+
427+
// Fail
428+
expect(validator(''), isNotNull);
429+
expect(validator(null), isNotNull);
430+
431+
final FormFieldValidator<String> validatorWithErrorMessage =
432+
FormBuilderValidators.skipWhen<String>(
433+
(String? value) => value == 'skip',
434+
FormBuilderValidators.required(errorText: customErrorMessage),
435+
);
436+
437+
// Pass
438+
expect(validatorWithErrorMessage('skip'), isNull);
439+
440+
// Fail
441+
expect(validatorWithErrorMessage(''), customErrorMessage);
442+
});
443+
444+
test('FormFieldValidatorExtensions.log with custom logger', () {
445+
// Arrange
446+
String? logMessage;
447+
final FormFieldValidator<String> validator =
448+
FormBuilderValidators.log<String>(
449+
log: (String? value) {
450+
logMessage = 'Custom Log: $value';
451+
return '';
452+
},
453+
);
454+
455+
// Act
456+
validator('test');
457+
458+
// Assert
459+
expect(logMessage, 'Custom Log: test');
460+
461+
// Act
462+
logMessage = null;
463+
validator(null);
464+
465+
// Assert
466+
expect(logMessage, 'Custom Log: null');
467+
468+
// Act
469+
logMessage = null;
470+
validator('');
471+
472+
// Assert
473+
expect(logMessage, 'Custom Log: ');
474+
});
384475
});
385476
}

test/src/identity/passport_number_validator_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,19 @@ void main() {
152152
equals(customErrorMessage),
153153
);
154154
});
155+
156+
test(
157+
'should return the error message when the value does not match the regex and is not in whitelist or blacklist',
158+
() {
159+
// Arrange
160+
final PassportNumberValidator validator = PassportNumberValidator(
161+
passportNumberWhitelist: passportNumberWhitelist,
162+
passportNumberBlacklist: passportNumberBlacklist,
163+
);
164+
165+
// Act & Assert
166+
expect(validator.validate('NotInWhitelistOrBlacklist'),
167+
equals(validator.errorText));
168+
});
155169
});
156170
}

0 commit comments

Comments
 (0)