Skip to content

Commit

Permalink
test: updated formz_test.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
alestiago committed Nov 29, 2023
1 parent f92079b commit cb7457f
Showing 1 changed file with 55 additions and 19 deletions.
74 changes: 55 additions & 19 deletions test/formz_test.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
// ignore_for_file: prefer_const_constructors
import 'dart:async';

import 'package:formz/formz.dart';
import 'package:test/test.dart';

import 'helpers/helpers.dart';

/// Similar to [NameInput], but takes a [delay] before validating.
class _DelayedNameInput extends FormzInput<String, NameInputError> {
const _DelayedNameInput.dirty({
required this.delay,
String value = '',
}) : super.dirty(value);

final Duration delay;

@override
FutureOr<NameInputError?> validator(String value) async {
await Future<void>.delayed(delay);
return value.isEmpty ? NameInputError.empty : null;
}
}

void main() {
group('Formz', () {
group('FormzMixin', () {
Expand Down Expand Up @@ -243,7 +261,7 @@ void main() {
);
});

test('returns valid for multiple valid inputs', () async {
test('returns valid for multiple valid sync inputs', () async {
await expectLater(
Formz.validate([
NameInput.dirty(value: 'jen'),
Expand All @@ -254,6 +272,29 @@ void main() {
);
});

test(
'returns valid for multiple valid async inputs',
() async {
await expectLater(
Formz.validate([
_DelayedNameInput.dirty(
delay: Duration(seconds: 2),
value: 'joe',
),
_DelayedNameInput.dirty(
delay: Duration(seconds: 2),
value: 'bob',
),
_DelayedNameInput.dirty(
delay: Duration(seconds: 2),
value: 'alex',
),
]),
completion(isTrue),
);
},
);

test('returns invalid for a pure/invalid input', () async {
await expectLater(
Formz.validate([NameInput.pure()]),
Expand Down Expand Up @@ -290,24 +331,19 @@ void main() {
);
});

test('if one input is invalid should not call validate for the others',
() async {
final formValid = NameInputErrorCacheMixin.dirty(value: 'John');
final formInvalidA = NameInputErrorCacheMixin.dirty();
final formInvalidB = NameInputErrorCacheMixin.dirty();
await expectLater(
Formz.validate([
formValid,
formInvalidA,
formInvalidB,
]),
completion(isFalse),
);

expect(formValid.validatorCalls, 1);
expect(formInvalidA.validatorCalls, 1);
expect(formInvalidB.validatorCalls, 0);
});
test(
'returns invalid as soon as an input is invalid',
() async {
await expectLater(
Formz.validate([
_DelayedNameInput.dirty(delay: Duration(seconds: 1)),
_DelayedNameInput.dirty(delay: Duration(minutes: 5)),
]),
completion(isFalse),
);
},
timeout: Timeout(Duration(seconds: 10)),
);
});

group('FormzSubmissionStatusX', () {
Expand Down

0 comments on commit cb7457f

Please sign in to comment.