-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathtransactionsTableController.test.ts
More file actions
375 lines (323 loc) · 10 KB
/
transactionsTableController.test.ts
File metadata and controls
375 lines (323 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import { testAddress } from '__mocks__';
import { account, testToken } from '__mocks__/data';
import { getPersistedTokenDetails } from 'apiCalls/tokens/getPersistedTokenDetails';
import { AssetType } from 'types/account.types';
import { TransactionServerStatusesEnum } from 'types/enums.types';
import {
ServerTransactionType,
TokenArgumentType,
TransactionActionsEnum,
TransactionActionType,
TransactionDirectionEnum,
TransferTypeEnum
} from 'types/serverTransactions.types';
import { NftEnumType } from 'types/tokens.types';
import { timeAgo } from 'utils/operations/timeRemaining';
import { getShardText } from 'utils/transactions/getInterpretedTransaction/helpers/getShardText';
import { TransactionsTableController } from '../TransactionsTableController';
jest.mock('apiCalls/tokens/getPersistedTokenDetails', () => ({
getPersistedTokenDetails: jest.fn()
}));
jest.mock('utils/operations/timeRemaining', () => ({
...jest.requireActual('utils/operations/timeRemaining'),
timeAgo: jest.fn()
}));
const mockTransactionBase: ServerTransactionType = {
txHash: 'tx1',
tokenIdentifier: testToken.identifier,
sender: testAddress,
receiver: account.address,
senderShard: 0,
receiverShard: 0,
value: '1000000000000000000',
data: '',
timestamp: Math.floor(Date.now() / 1000) - 300,
action: {
name: TransactionActionsEnum.transfer,
description: 'Token transfer',
category: 'esdtNft',
arguments: {}
} as TransactionActionType,
gasLimit: 50_000,
gasPrice: 1_000_000_000,
gasUsed: 45_000,
miniBlockHash: 'miniBlockHash123',
nonce: 1,
round: 1,
signature: 'signature123',
status: TransactionServerStatusesEnum.success,
price: 0,
type: TransferTypeEnum.Transaction,
fee: '1000000000',
inTransit: false,
results: [],
operations: [],
logs: {
id: 'log1',
address: 'erd1qqq...contract',
events: []
},
scamInfo: undefined,
pendingResults: false,
receipt: {
value: '0',
sender: '',
data: ''
},
senderAssets: {
name: 'Sender',
description: 'Test sender account'
} as AssetType,
receiverAssets: {
name: 'Receiver',
description: 'Test receiver account'
} as AssetType
};
const mockParams = {
address: 'erd1qqq...test',
egldLabel: 'EGLD',
explorerAddress: 'https://explorer.example.com',
transactions: [mockTransactionBase]
};
describe('TransactionsTableController', () => {
beforeEach(() => {
(getPersistedTokenDetails as jest.Mock).mockResolvedValue({
assets: {
lockedAccounts: {}
}
});
});
it('should handle locked accounts', async () => {
(getPersistedTokenDetails as jest.Mock).mockResolvedValue({
assets: {
lockedAccounts: {
senderLocked: testAddress,
receiverLocked: account.address
}
}
});
const [result] =
await TransactionsTableController.processTransactions(mockParams);
expect(result.sender.isTokenLocked).toBe(true);
expect(result.receiver.isTokenLocked).toBe(true);
});
it('should format transaction values correctly', async () => {
const [result] =
await TransactionsTableController.processTransactions(mockParams);
expect(result.value.valueInteger).toBe('1');
expect(result.value.valueDecimal).toBe('.00');
expect(result.value.egldLabel).toBe('EGLD');
});
it('should generate proper shard text', async () => {
const [result] =
await TransactionsTableController.processTransactions(mockParams);
expect(result.sender.shard).toBe(getShardText(0));
expect(result.receiver.shard).toBe(getShardText(0));
});
it('should detect contract addresses', async () => {
const contractAddress =
'erd1qqqqqqqqqqqqqpgqv9gxgq8nurz754spjfck6rdwlg9etpcp0n4sjg2dhc';
const transactions = [
{
...mockTransactionBase,
sender: contractAddress,
receiver: contractAddress
}
];
const [result] = await TransactionsTableController.processTransactions({
...mockParams,
transactions
});
expect(result.sender.isContract).toBe(true);
expect(result.receiver.isContract).toBe(true);
});
it('should handle concurrent transaction processing', async () => {
const transactions = Array(10)
.fill(null)
.map((_, i) => ({
...mockTransactionBase,
txHash: `tx${i}`,
tokenIdentifier: `TOKEN-${i}`
}));
(getPersistedTokenDetails as jest.Mock).mockImplementation(() =>
Promise.resolve({
assets: {
lockedAccounts: {
senderLocked: testAddress
}
}
})
);
const results = await TransactionsTableController.processTransactions({
...mockParams,
transactions
});
expect(results).toHaveLength(10);
expect(results.every((tx) => tx.sender.isTokenLocked)).toBe(true);
});
it('should handle network failures when fetching token details', async () => {
(getPersistedTokenDetails as jest.Mock).mockRejectedValue('Network error');
await expect(
TransactionsTableController.processTransactions(mockParams)
).resolves.not.toThrow();
const [result] =
await TransactionsTableController.processTransactions(mockParams);
expect(result.sender.isTokenLocked).toBe(false);
});
it('should handle metachain shard IDs', async () => {
const METACHAIN_SHARD_ID = 4294967295;
const transactions = [
{
...mockTransactionBase,
senderShard: METACHAIN_SHARD_ID,
receiverShard: METACHAIN_SHARD_ID
}
];
const [result] = await TransactionsTableController.processTransactions({
...mockParams,
transactions
});
expect(result.sender.shard).toBe('Metachain');
expect(result.receiver.shard).toBe('Metachain');
});
it('should handle zero-value transactions', async () => {
const transactions = [
{
...mockTransactionBase,
value: '0'
}
];
const [result] = await TransactionsTableController.processTransactions({
...mockParams,
transactions
});
expect(result.value.valueInteger).toBe('0');
expect(result.value.valueDecimal).toBe('.00');
});
it('should handle different NFT types', async () => {
const mockTokenArgument: TokenArgumentType = {
type: NftEnumType.NonFungibleESDT,
name: 'Elrond Apes',
ticker: 'EAPES',
collection: 'EAPES-123456',
identifier: 'EAPES-123456-01',
token: 'EAPES-123456-01',
decimals: 0,
value: '1',
providerName: 'ElrondApes',
providerAvatar: 'https://example.com/elrondapes-avatar.png',
svgUrl: 'https://example.com/eapes/01.svg',
valueUSD: '250.00'
};
const transactions: ServerTransactionType[] = [
{
...mockTransactionBase,
action: {
...mockTransactionBase.action,
arguments: {
token: mockTokenArgument
}
} as TransactionActionType
}
];
const [result] = await TransactionsTableController.processTransactions({
...mockParams,
transactions
});
expect(result.value.badge).toBeDefined();
});
it('should handle malformed token details response', async () => {
(getPersistedTokenDetails as jest.Mock).mockResolvedValue({
invalid: 'response'
});
const [result] =
await TransactionsTableController.processTransactions(mockParams);
expect(result.sender.isTokenLocked).toBe(false);
});
it('should handle time-sensitive transactions near midnight', async () => {
const timeAgoMock = {
timeAgo: '13 days',
tooltip: 'Feb 06, 2025 00:00:00 AM UTC'
};
(timeAgo as jest.Mock).mockReturnValue(timeAgoMock.timeAgo);
const nearMidnightTransaction = {
...mockTransactionBase,
timestamp: 1738800000 // Exact midnight in some timezone
};
const [result] = await TransactionsTableController.processTransactions({
...mockParams,
transactions: [nearMidnightTransaction]
});
expect(result.age).toEqual(timeAgoMock);
});
it('should handle missing token ID', async () => {
const transactions = [
{
...mockTransactionBase,
tokenIdentifier: undefined
}
];
const [result] = await TransactionsTableController.processTransactions({
...mockParams,
transactions
});
expect(result.sender.isTokenLocked).toBe(false);
expect(result.receiver.isTokenLocked).toBe(false);
});
it('should handle empty locked accounts', async () => {
(getPersistedTokenDetails as jest.Mock).mockResolvedValue({
assets: { lockedAccounts: null }
});
const [result] =
await TransactionsTableController.processTransactions(mockParams);
expect(result.sender.isTokenLocked).toBe(false);
expect(result.receiver.isTokenLocked).toBe(false);
});
it('should handle different transaction directions', async () => {
const testCases = [
{
sender: 'erd1qqq...test',
receiver: 'erd1qqq...other',
expectedDirection: TransactionDirectionEnum.OUT
},
{
sender: 'erd1qqq...other',
receiver: 'erd1qqq...test',
expectedDirection: TransactionDirectionEnum.IN
},
{
sender: 'erd1qqq...test',
receiver: 'erd1qqq...test',
expectedDirection: TransactionDirectionEnum.SELF
}
];
for (const { sender, receiver, expectedDirection } of testCases) {
const transactions: ServerTransactionType[] = [
{
...mockTransactionBase,
sender,
receiver
}
];
const [result] = await TransactionsTableController.processTransactions({
...mockParams,
address: 'erd1qqq...test',
transactions
});
expect(result.direction).toBe(expectedDirection);
}
});
it('should handle MetaESDT tokens', async () => {
(getPersistedTokenDetails as jest.Mock).mockResolvedValue({
assets: {
lockedAccounts: {},
token: {
type: NftEnumType.MetaESDT
}
}
});
const [result] =
await TransactionsTableController.processTransactions(mockParams);
expect(result.value.badge).toBeUndefined();
});
});