@@ -772,6 +772,95 @@ describe('useEmailSubscription', () => {
772772 } )
773773 } )
774774
775+ describe ( 'Feedback lifecycle — clearing on typing' , ( ) => {
776+ test ( 'clears success message when user types after successful submit' , async ( ) => {
777+ mockUpdateSubscriptions . mockResolvedValue ( { } )
778+ const { result} = renderHook ( ( ) => useEmailSubscription ( { tag : 'email_capture' } ) , {
779+ wrapper : createWrapper ( )
780+ } )
781+
782+ act ( ( ) => {
783+ result . current . form . setValue ( 'email' , 'test@example.com' )
784+ } )
785+
786+ await act ( async ( ) => {
787+ await result . current . onSubmit ( )
788+ } )
789+
790+ await waitFor ( ( ) => {
791+ expect ( result . current . successMessage ) . toBe ( 'Thanks for subscribing!' )
792+ } )
793+
794+ // User starts typing a new email — success message should clear
795+ act ( ( ) => {
796+ result . current . form . setValue ( 'email' , 'n' )
797+ } )
798+
799+ await waitFor ( ( ) => {
800+ expect ( result . current . successMessage ) . toBeNull ( )
801+ } )
802+ } )
803+
804+ test ( 'does not show validation error when typing partial email after successful submit' , async ( ) => {
805+ mockUpdateSubscriptions . mockResolvedValue ( { } )
806+ const { result} = renderHook ( ( ) => useEmailSubscription ( { tag : 'email_capture' } ) , {
807+ wrapper : createWrapper ( )
808+ } )
809+
810+ act ( ( ) => {
811+ result . current . form . setValue ( 'email' , 'test@example.com' )
812+ } )
813+
814+ await act ( async ( ) => {
815+ await result . current . onSubmit ( )
816+ } )
817+
818+ await waitFor ( ( ) => {
819+ expect ( result . current . successMessage ) . toBe ( 'Thanks for subscribing!' )
820+ } )
821+
822+ // Type a partial (invalid) email — should NOT trigger validation error
823+ act ( ( ) => {
824+ result . current . form . setValue ( 'email' , 'new-use' )
825+ } )
826+
827+ await waitFor ( ( ) => {
828+ expect ( result . current . errors . email ) . toBeUndefined ( )
829+ expect ( result . current . successMessage ) . toBeNull ( )
830+ } )
831+ } )
832+
833+ test ( 'clears error message when user types after failed submit' , async ( ) => {
834+ mockUpdateSubscriptions . mockRejectedValue ( new Error ( 'API Error' ) )
835+ const { result} = renderHook ( ( ) => useEmailSubscription ( { tag : 'email_capture' } ) , {
836+ wrapper : createWrapper ( )
837+ } )
838+
839+ act ( ( ) => {
840+ result . current . form . setValue ( 'email' , 'test@example.com' )
841+ } )
842+
843+ await act ( async ( ) => {
844+ await result . current . onSubmit ( )
845+ } )
846+
847+ await waitFor ( ( ) => {
848+ expect ( result . current . errors . email ?. message ) . toBe (
849+ "We couldn't process the subscription. Try again."
850+ )
851+ } )
852+
853+ // User starts correcting — error should clear
854+ act ( ( ) => {
855+ result . current . form . setValue ( 'email' , 'test@example.co' )
856+ } )
857+
858+ await waitFor ( ( ) => {
859+ expect ( result . current . errors . email ) . toBeUndefined ( )
860+ } )
861+ } )
862+ } )
863+
775864 describe ( 'Edge cases' , ( ) => {
776865 test ( 'handles undefined subscriptions data' , ( ) => {
777866 const mockRefetch = jest . fn ( ) . mockResolvedValue ( { data : undefined } )
0 commit comments