@@ -122,3 +122,126 @@ describe('TransactionUtil.getTransactionDescriptions', () => {
122
122
expect ( desc [ 0 ] ) . toBe ( 'success' )
123
123
} )
124
124
} )
125
+
126
+ describe ( 'TransactionUtil.mergeTransfers' , ( ) => {
127
+ const createToken1Transfer = ( quantity = '100' ) => ( {
128
+ fungible_info : {
129
+ name : 'Token1' ,
130
+ symbol : 'TOK1' ,
131
+ icon : { url : 'token1-url' }
132
+ } ,
133
+ direction : 'in' as const ,
134
+ quantity : { numeric : quantity }
135
+ } )
136
+
137
+ const createToken2Transfer = ( quantity = '200' ) => ( {
138
+ fungible_info : {
139
+ name : 'Token2' ,
140
+ symbol : 'TOK2' ,
141
+ icon : { url : 'token2-url' }
142
+ } ,
143
+ direction : 'in' as const ,
144
+ quantity : { numeric : quantity }
145
+ } )
146
+
147
+ it ( 'returns empty array for empty input' , ( ) => {
148
+ const result = TransactionUtil . mergeTransfers ( [ ] )
149
+ expect ( result ) . toEqual ( [ ] )
150
+ } )
151
+
152
+ it ( 'returns single transfer unchanged' , ( ) => {
153
+ const token1Transfer = createToken1Transfer ( )
154
+ const result = TransactionUtil . mergeTransfers ( [ token1Transfer ] )
155
+ expect ( result ) . toEqual ( [ token1Transfer ] )
156
+ } )
157
+
158
+ it ( 'returns multiple transfers with different names unchanged' , ( ) => {
159
+ const token1Transfer = createToken1Transfer ( )
160
+ const token2Transfer = createToken2Transfer ( )
161
+ const result = TransactionUtil . mergeTransfers ( [ token1Transfer , token2Transfer ] )
162
+ expect ( result ) . toEqual ( [ token1Transfer , token2Transfer ] )
163
+ } )
164
+
165
+ it ( 'merges transfers with same token name' , ( ) => {
166
+ const token1Transfer = createToken1Transfer ( '100' )
167
+ const token1Transfer2 = createToken1Transfer ( '50' )
168
+ const result = TransactionUtil . mergeTransfers ( [ token1Transfer , token1Transfer2 ] )
169
+ expect ( result . length ) . toBe ( 1 )
170
+ expect ( result [ 0 ] ?. fungible_info ?. name ) . toBe ( 'Token1' )
171
+ expect ( result [ 0 ] ?. quantity . numeric ) . toBe ( '150' )
172
+ } )
173
+
174
+ it ( 'merges multiple transfers with mixed same and different names' , ( ) => {
175
+ const token1Transfer = createToken1Transfer ( '100' )
176
+ const token2Transfer = createToken2Transfer ( '200' )
177
+ const token1Transfer2 = createToken1Transfer ( '50' )
178
+ const result = TransactionUtil . mergeTransfers ( [ token1Transfer , token2Transfer , token1Transfer2 ] )
179
+ expect ( result . length ) . toBe ( 2 )
180
+
181
+ // Find the merged Token1 transfer
182
+ const token1Merged = result . find ( t => t ?. fungible_info ?. name === 'Token1' )
183
+ expect ( token1Merged ?. quantity . numeric ) . toBe ( '150' ) // '100' + '50'
184
+
185
+ // Token2 should remain unchanged
186
+ const token2Result = result . find ( t => t ?. fungible_info ?. name === 'Token2' )
187
+ expect ( token2Result ?. quantity . numeric ) . toBe ( '200' )
188
+ } )
189
+
190
+ it ( 'handles transfers with undefined fungible_info gracefully' , ( ) => {
191
+ const token1Transfer = createToken1Transfer ( )
192
+ const transferWithoutFungible = {
193
+ direction : 'in' as const ,
194
+ quantity : { numeric : '100' }
195
+ } as any
196
+
197
+ const result = TransactionUtil . mergeTransfers ( [ token1Transfer , transferWithoutFungible ] )
198
+ expect ( result . length ) . toBe ( 2 )
199
+ expect ( result ) . toContain ( token1Transfer )
200
+ expect ( result ) . toContain ( transferWithoutFungible )
201
+ } )
202
+
203
+ it ( 'does not merge transfers with undefined fungible_info name' , ( ) => {
204
+ const transferWithoutName = {
205
+ fungible_info : {
206
+ symbol : 'TOK1' ,
207
+ icon : { url : 'token1-url' }
208
+ // name is undefined
209
+ } ,
210
+ direction : 'in' as const ,
211
+ quantity : { numeric : '100' }
212
+ }
213
+
214
+ const anotherTransferWithoutName = {
215
+ fungible_info : {
216
+ symbol : 'TOK1' ,
217
+ icon : { url : 'token1-url' }
218
+ // name is undefined
219
+ } ,
220
+ direction : 'in' as const ,
221
+ quantity : { numeric : '50' }
222
+ }
223
+
224
+ const result = TransactionUtil . mergeTransfers ( [ transferWithoutName , anotherTransferWithoutName ] )
225
+ expect ( result . length ) . toBe ( 2 ) // Should not merge because names are undefined
226
+ expect ( result [ 0 ] ?. quantity . numeric ) . toBe ( '100' )
227
+ expect ( result [ 1 ] ?. quantity . numeric ) . toBe ( '50' )
228
+ } )
229
+
230
+ it ( 'does not merge transfer with undefined name with named transfer' , ( ) => {
231
+ const namedTransfer = createToken1Transfer ( '100' )
232
+ const transferWithoutName = {
233
+ fungible_info : {
234
+ symbol : 'TOK1' ,
235
+ icon : { url : 'token1-url' }
236
+ // name is undefined
237
+ } ,
238
+ direction : 'in' as const ,
239
+ quantity : { numeric : '50' }
240
+ }
241
+
242
+ const result = TransactionUtil . mergeTransfers ( [ namedTransfer , transferWithoutName ] )
243
+ expect ( result . length ) . toBe ( 2 ) // Should not merge because one has undefined name
244
+ expect ( result . find ( t => t ?. fungible_info ?. name === 'Token1' ) ?. quantity . numeric ) . toBe ( '100' )
245
+ expect ( result . find ( t => ! t ?. fungible_info ?. name ) ?. quantity . numeric ) . toBe ( '50' )
246
+ } )
247
+ } )
0 commit comments