@@ -30,6 +30,7 @@ describe('TransactionButton', () => {
30
30
( useShowCallsStatus as Mock ) . mockReturnValue ( {
31
31
showCallsStatus : vi . fn ( ) ,
32
32
} ) ;
33
+ vi . clearAllMocks ( ) ;
33
34
} ) ;
34
35
35
36
it ( 'renders correctly' , ( ) => {
@@ -74,6 +75,71 @@ describe('TransactionButton', () => {
74
75
expect ( text ) . toBeInTheDocument ( ) ;
75
76
} ) ;
76
77
78
+ it ( 'renders custom error text when error exists' , ( ) => {
79
+ const mockErrorFunc = vi . fn ( ) ;
80
+ ( useTransactionContext as Mock ) . mockReturnValue ( {
81
+ lifecycleStatus : { statusName : 'init' , statusData : null } ,
82
+ errorMessage : 'blah blah' ,
83
+ customStates : {
84
+ error : {
85
+ text : 'oops' ,
86
+ onClick : mockErrorFunc ,
87
+ } ,
88
+ } ,
89
+ isLoading : false ,
90
+ address : '123' ,
91
+ transactions : [ { } ] ,
92
+ } ) ;
93
+ render ( < TransactionButton text = "Transact" /> ) ;
94
+ const text = screen . getByText ( 'oops' ) ;
95
+ expect ( text ) . toBeInTheDocument ( ) ;
96
+ const button = screen . getByTestId ( 'ockTransactionButton_Button' ) ;
97
+ fireEvent . click ( button ) ;
98
+ expect ( mockErrorFunc ) . toHaveBeenCalled ( ) ;
99
+ } ) ;
100
+
101
+ it ( 'should call custom error handler when error exists' , ( ) => {
102
+ const mockErrorFunc = vi . fn ( ) ;
103
+ ( useTransactionContext as Mock ) . mockReturnValue ( {
104
+ lifecycleStatus : { statusName : 'init' , statusData : null } ,
105
+ errorMessage : 'blah blah' ,
106
+ customStates : {
107
+ error : {
108
+ text : 'oops' ,
109
+ onClick : mockErrorFunc ,
110
+ } ,
111
+ } ,
112
+ isLoading : false ,
113
+ address : '123' ,
114
+ transactions : [ { } ] ,
115
+ } ) ;
116
+ render ( < TransactionButton text = "Transact" /> ) ;
117
+ const button = screen . getByTestId ( 'ockTransactionButton_Button' ) ;
118
+ fireEvent . click ( button ) ;
119
+ expect ( mockErrorFunc ) . toHaveBeenCalled ( ) ;
120
+ } ) ;
121
+
122
+ it ( 'should recall onSubmit when error exists and no custom handler provided' , ( ) => {
123
+ const mockOnSubmit = vi . fn ( ) ;
124
+ ( useTransactionContext as Mock ) . mockReturnValue ( {
125
+ lifecycleStatus : { statusName : 'init' , statusData : null } ,
126
+ errorMessage : 'blah blah' ,
127
+ customStates : {
128
+ error : {
129
+ text : 'oops' ,
130
+ } ,
131
+ } ,
132
+ isLoading : false ,
133
+ address : '123' ,
134
+ transactions : [ { } ] ,
135
+ onSubmit : mockOnSubmit ,
136
+ } ) ;
137
+ render ( < TransactionButton text = "Transact" /> ) ;
138
+ const button = screen . getByTestId ( 'ockTransactionButton_Button' ) ;
139
+ fireEvent . click ( button ) ;
140
+ expect ( mockOnSubmit ) . toHaveBeenCalled ( ) ;
141
+ } ) ;
142
+
77
143
it ( 'should have disabled attribute when isDisabled is true' , ( ) => {
78
144
const { getByRole } = render (
79
145
< TransactionButton disabled = { true } text = "Submit" /> ,
@@ -116,6 +182,44 @@ describe('TransactionButton', () => {
116
182
expect ( showCallsStatus ) . toHaveBeenCalledWith ( { id : '456' } ) ;
117
183
} ) ;
118
184
185
+ it ( 'should render custom success text when it exists' , ( ) => {
186
+ const showCallsStatus = vi . fn ( ) ;
187
+ ( useShowCallsStatus as Mock ) . mockReturnValue ( { showCallsStatus } ) ;
188
+ ( useTransactionContext as Mock ) . mockReturnValue ( {
189
+ lifecycleStatus : { statusName : 'init' , statusData : null } ,
190
+ receipt : '123' ,
191
+ transactionId : '456' ,
192
+ customStates : {
193
+ success : {
194
+ text : 'yay' ,
195
+ } ,
196
+ } ,
197
+ } ) ;
198
+ render ( < TransactionButton text = "Transact" /> ) ;
199
+ const button = screen . getByText ( 'yay' ) ;
200
+ fireEvent . click ( button ) ;
201
+ expect ( showCallsStatus ) . toHaveBeenCalledWith ( { id : '456' } ) ;
202
+ } ) ;
203
+
204
+ it ( 'should call custom success handler when it exists' , ( ) => {
205
+ const mockSuccessHandler = vi . fn ( ) ;
206
+ ( useTransactionContext as Mock ) . mockReturnValue ( {
207
+ lifecycleStatus : { statusName : 'init' , statusData : null } ,
208
+ receipt : '123' ,
209
+ transactionId : '456' ,
210
+ customStates : {
211
+ success : {
212
+ text : 'yay' ,
213
+ onClick : mockSuccessHandler ,
214
+ } ,
215
+ } ,
216
+ } ) ;
217
+ render ( < TransactionButton text = "Transact" /> ) ;
218
+ const button = screen . getByText ( 'yay' ) ;
219
+ fireEvent . click ( button ) ;
220
+ expect ( mockSuccessHandler ) . toHaveBeenCalledWith ( '123' ) ;
221
+ } ) ;
222
+
119
223
it ( 'should enable button when not in progress, not missing props, and not waiting for receipt' , ( ) => {
120
224
( useTransactionContext as Mock ) . mockReturnValue ( {
121
225
isLoading : false ,
0 commit comments