From 6f18aafbab47b9c60a0e5db23dda05aa639e7bd1 Mon Sep 17 00:00:00 2001 From: Yeikel Uriarte Arteaga Date: Tue, 19 Mar 2024 11:15:03 -0400 Subject: [PATCH] fix(ly-form): change submit transform event to droppable --- packages/lyform/lib/src/ly_form/ly_form.dart | 8 ++- .../lyform/test/src/ly_form/ly_form_test.dart | 63 ++++++++++++++++++- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/packages/lyform/lib/src/ly_form/ly_form.dart b/packages/lyform/lib/src/ly_form/ly_form.dart index 9e9b8a8..670d7ce 100644 --- a/packages/lyform/lib/src/ly_form/ly_form.dart +++ b/packages/lyform/lib/src/ly_form/ly_form.dart @@ -36,7 +36,7 @@ abstract class LyForm extends Bloc> await Future.delayed(Duration.zero); await onSubmitEvent(emit); }, - transformer: sequential(), + transformer: droppable(), ); on( @@ -118,7 +118,8 @@ abstract class LyForm extends Bloc> /// Called when the form is submitted. Future onSubmitEvent(Emitter> emit) async { - validate(); + await validate(); + if (!isValid()) { emit(invalid()); } else { @@ -191,10 +192,11 @@ abstract class LyForm extends Bloc> } /// Validate all inputs and return [true] if they all are valids. - void validate() { + FutureOr validate() async { for (final input in _inputs) { if (input.validator != null && !input.isPure) { input.validate(); + await Future.delayed(Duration.zero); } } } diff --git a/packages/lyform/test/src/ly_form/ly_form_test.dart b/packages/lyform/test/src/ly_form/ly_form_test.dart index 616380e..34eb323 100644 --- a/packages/lyform/test/src/ly_form/ly_form_test.dart +++ b/packages/lyform/test/src/ly_form/ly_form_test.dart @@ -299,8 +299,16 @@ void main() { ..name.dirty('ly') ..submit(), skip: 1, - wait: const Duration(seconds: 1), + wait: const Duration(seconds: 2), expect: () => const [ + LyFormValidState([ + LyInputState( + value: 'ly', + lastNotNullValue: 'ly', + pureValue: '', + debugName: 'name', + ), + ]), LyFormLoadingState([ LyInputState( value: 'ly', @@ -309,6 +317,39 @@ void main() { debugName: 'name', ), ]), + LyFormSuccessState( + 'Name save success!', + [ + LyInputState( + value: 'ly', + lastNotNullValue: 'ly', + pureValue: '', + debugName: 'name', + ), + ], + ), + LyFormPureState([ + LyInputState( + value: 'ly', + lastNotNullValue: 'ly', + pureValue: 'ly', + debugName: 'name', + ), + ]), + ], + ); + + blocTest( + 'check when submit event was called and is dropped when is more than one', + build: () => TestForm(), + act: (form) => form + ..name.dirty('ly') + ..submit() + ..submit() + ..submit(), + skip: 1, + wait: const Duration(seconds: 2), + expect: () => const [ LyFormValidState([ LyInputState( value: 'ly', @@ -317,10 +358,18 @@ void main() { debugName: 'name', ), ]), + LyFormLoadingState([ + LyInputState( + value: 'ly', + lastNotNullValue: 'ly', + pureValue: '', + debugName: 'name', + ), + ]), LyFormSuccessState( 'Name save success!', [ - LyInputState( + LyInputState( value: 'ly', lastNotNullValue: 'ly', pureValue: '', @@ -328,6 +377,16 @@ void main() { ), ], ), + LyFormPureState( + [ + LyInputState( + value: 'ly', + lastNotNullValue: 'ly', + pureValue: 'ly', + debugName: 'name', + ), + ], + ), ], );