diff --git a/test/formz_test.dart b/test/formz_test.dart index cc07b02..5f9e50c 100644 --- a/test/formz_test.dart +++ b/test/formz_test.dart @@ -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 { + const _DelayedNameInput.dirty({ + required this.delay, + String value = '', + }) : super.dirty(value); + + final Duration delay; + + @override + FutureOr validator(String value) async { + await Future.delayed(delay); + return value.isEmpty ? NameInputError.empty : null; + } +} + void main() { group('Formz', () { group('FormzMixin', () { @@ -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'), @@ -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()]), @@ -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', () {