Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/src/models/form_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ class FormBuilder {
} else if (value is double) {
return FormControl<double>(value: value, validators: validators);
} else if (value is DateTime) {
return FormControl<DateTime>(value: value);
return FormControl<DateTime>(value: value, validators: validators);
} else if (value is TimeOfDay) {
return FormControl<TimeOfDay>(value: value);
return FormControl<TimeOfDay>(value: value, validators: validators);
}

return FormControl<dynamic>(value: value, validators: validators);
Expand Down
57 changes: 57 additions & 0 deletions test/src/models/form_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,35 @@ void main() {
);
});


test('Build a group with default datetime value and a validator as array', () {
// Given: a form group builder creation
final requiredValidator = Validators.required;
final value = DateTime.now();
final form = fb.group({
'control': [value, requiredValidator],
});

// Expect a form group created
expect(
form.control('control'),
isA<FormControl<DateTime>>(),
reason:
'${form.control('control').runtimeType} is not instance of FormControl<DateTime>',
);
Comment on lines +424 to +429
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For type checks in tests, it's more idiomatic to use the isA<T>() matcher. It provides better error messages on failure, especially if the control is null.

Suggested change
expect(
form.control('control') is FormControl<DateTime>,
true,
reason:
'${form.control('control').runtimeType} is not instance of FormControl<DateTime>',
);
expect(form.control('control'), isA<FormControl<DateTime>>());

expect(
form.control('control').value,
value,
reason: 'control default value not set',
);
expect(
form.control('control').validators,
[requiredValidator],
reason: 'not set required validator'
);
Comment on lines +435 to +439
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

You can simplify these two assertions into a single one by comparing the validators list directly. This is more concise and checks both the length and the content of the list.

Suggested change
expect(
form.control('control').validators.length,
1,
reason: 'incorrect validators length'
);
expect(
form.control('control').validators[0],
requiredValidator,
reason: 'not set required validator'
);
expect(form.control('control').validators, [requiredValidator]);

});


test('Build a group with default TimeOfDay value as array', () {
// Given: a form group builder creation
final value = TimeOfDay.now();
Expand All @@ -432,6 +461,34 @@ void main() {
);
});


test('Build a group with default TimeOfDay value and a validator as array', () {
// Given: a form group builder creation
final requiredValidator = Validators.required;
final value = TimeOfDay.now();
final form = fb.group({
'control': [value, requiredValidator],
});

// Expect a form group created
expect(
form.control('control'),
isA<FormControl<TimeOfDay>>(),
reason:
'${form.control('control').runtimeType} is not instance of FormControl<TimeOfDay>',
);
Comment on lines +474 to +479
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For type checks in tests, it's more idiomatic to use the isA<T>() matcher. It provides better error messages on failure, especially if the control is null.

Suggested change
expect(
form.control('control') is FormControl<TimeOfDay>,
true,
reason:
'${form.control('control').runtimeType} is not instance of FormControl<TimeOfDay>',
);
expect(form.control('control'), isA<FormControl<TimeOfDay>>());

expect(
form.control('control').value,
value,
reason: 'control default value not set',
);
expect(
form.control('control').validators,
[requiredValidator],
reason: 'not set required validator'
);
Comment on lines +485 to +489
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

You can simplify these two assertions into a single one by comparing the validators list directly. This is more concise and checks both the length and the content of the list.

Suggested change
expect(
form.control('control').validators.length,
1,
reason: 'incorrect validators length'
);
expect(
form.control('control').validators[0],
requiredValidator,
reason: 'not set required validator'
);
expect(form.control('control').validators, [requiredValidator]);

});

test('Build a state with ', () {
// Given: a state creation
final state = fb.state(value: 'name', disabled: true);
Expand Down