-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcreateToastsFromTransactions.test.ts
More file actions
249 lines (219 loc) · 7.41 KB
/
createToastsFromTransactions.test.ts
File metadata and controls
249 lines (219 loc) · 7.41 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
import {
testAddress,
testNetwork,
mockPendingTransaction,
server,
rest,
mockStore
} from '__mocks__';
import { TRANSACTIONS_ENDPOINT } from 'apiCalls/endpoints';
import { StoreType } from 'store/store.types';
import {
IconNamesEnum,
TransactionServerStatusesEnum
} from 'types/enums.types';
import { createToastsFromTransactions } from '../createToastsFromTransactions';
import { mockTransactionSession } from './mocks/transactions';
const successTransactionHash =
'txSuccess§§§7cb81c945decd70678cebc5b8b919f8e3c8411f2b19ebdd6ay';
const existingTransactionHash =
'txExisting§§§7cb81c945decd70678cebc5b8b919f8e3c8411f2b19ebdd6ay';
const createMockToast = (toastId: string, fixedNow: number) => {
return {
toastId,
startTime: fixedNow,
endTime: fixedNow + 1000
};
};
const getSessionIds = (fixedNow: number) =>
({
PENDING: fixedNow.toString(), // August 1, 2025 midnight
SUCCESS: (fixedNow + 1 * 60 * 1000).toString(), // 1 minute later
EXISTING: (fixedNow + 2 * 60 * 1000).toString(), // 2 minutes later
MISSING: (fixedNow + 3 * 60 * 1000).toString() // 3 minutes later
}) as const;
const createMockSession = (
status: TransactionServerStatusesEnum,
transactionHash: string
) => ({
...mockTransactionSession,
status,
transactions: [{ ...mockPendingTransaction, hash: transactionHash, status }]
});
describe('createToastsFromTransactions', () => {
const fixedNowDate = new Date('2025-08-01T00:00:00'); // fixed date at August 1, 2025
const fixedNow = fixedNowDate.getTime();
const SESSION_IDS = getSessionIds(fixedNow);
beforeEach(() => {
// mock Date.now()
jest.spyOn(Date, 'now').mockReturnValue(fixedNow);
});
afterEach(() => {
jest.spyOn(Date, 'now').mockRestore();
server.resetHandlers();
});
const mockTransactionsEndpoint = (
hashes: string[],
mockData: Array<typeof mockPendingTransaction>
) => {
const endpoint = `${testNetwork.apiAddress}/${TRANSACTIONS_ENDPOINT}`;
return rest.get(endpoint, (req, res, ctx) => {
return res(ctx.status(200), ctx.json(mockData));
});
};
it('should return empty arrays for processing and completed transactions when no transactions exist', async () => {
const result = await createToastsFromTransactions({ store: mockStore });
expect(result).toEqual({
pendingTransactionToasts: [],
completedTransactionToasts: []
});
});
it('should correctly classify transactions as processing or completed based on their status', async () => {
server.use(
mockTransactionsEndpoint(
[mockPendingTransaction.hash],
[{ ...mockPendingTransaction, status: 'pending' }]
)
);
const store: StoreType = {
...mockStore,
transactions: {
[SESSION_IDS.PENDING]: createMockSession(
TransactionServerStatusesEnum.pending,
mockPendingTransaction.hash
),
[SESSION_IDS.SUCCESS]: createMockSession(
TransactionServerStatusesEnum.success,
successTransactionHash
)
},
toasts: {
transactionToasts: [
createMockToast(SESSION_IDS.PENDING, fixedNow),
createMockToast(SESSION_IDS.SUCCESS, fixedNow)
],
customToasts: []
}
};
const result = await createToastsFromTransactions({ store });
const commonData = {
asset: { icon: IconNamesEnum.hourglass },
interactor: testAddress,
directionLabel: 'From',
action: { name: 'Received xEGLD', description: undefined },
amount: '0 xEGLD',
hash: mockPendingTransaction.hash,
status: 'pending',
timestamp: undefined,
interactorAsset: undefined
};
expect(result).toEqual({
pendingTransactionToasts: [
{
toastDataState: {
id: SESSION_IDS.PENDING,
icon: IconNamesEnum.hourglass,
hasCloseButton: false,
title: 'Processing Self transaction',
iconClassName: 'warning'
},
processedTransactionsStatus: '0 / 1 transactions processed',
transactionProgressState: {
endTime: fixedNow + 1000,
startTime: fixedNow
},
toastId: SESSION_IDS.PENDING,
transactions: [
{
...commonData,
link: `${testNetwork.explorerAddress}/transactions/${mockPendingTransaction.hash}`
}
]
}
],
completedTransactionToasts: [
{
toastDataState: {
id: SESSION_IDS.SUCCESS,
icon: IconNamesEnum.check,
hasCloseButton: true,
title: 'Self transaction successful',
iconClassName: 'success'
},
processedTransactionsStatus: 'Transaction processed',
transactionProgressState: null,
toastId: SESSION_IDS.SUCCESS,
transactions: [
{
action: { name: 'Received xEGLD', description: undefined },
amount: '0 xEGLD',
asset: null,
directionLabel: 'From',
hash: successTransactionHash,
interactor: testAddress,
interactorAsset: undefined,
link: `${testNetwork.explorerAddress}/transactions/${successTransactionHash}`,
status: TransactionServerStatusesEnum.success,
timestamp: undefined
}
]
}
]
});
});
});
describe('createToastsFromTransactions existing completed transactions', () => {
beforeEach(() => {
jest.mock('../createTransactionToast', () => ({
createTransactionToast: jest.fn().mockImplementation(({ toastId }) => ({
toastId
}))
}));
});
afterEach(() => {
jest.clearAllMocks();
});
it('should preserve existing completed transactions and not create duplicates when same transaction is processed again', async () => {
const EXISTING_SESSION_ID = 'existing-session-id';
const fixedNow = new Date('2025-08-01T00:00:00').getTime();
const toastList = {
transactionToasts: [createMockToast(EXISTING_SESSION_ID, fixedNow)],
customToasts: []
};
const transactionsSessions = {
[EXISTING_SESSION_ID]: createMockSession(
TransactionServerStatusesEnum.success,
existingTransactionHash
)
};
const store: StoreType = {
...mockStore,
toasts: toastList,
transactions: transactionsSessions
};
const { createTransactionToast } = require('../createTransactionToast');
const result = await createToastsFromTransactions({
store
});
expect(result.completedTransactionToasts).toHaveLength(1);
expect(createTransactionToast).not.toHaveBeenCalled();
});
it('should safely handle transactions with missing session data without creating toasts', async () => {
const { createTransactionToast } = require('../createTransactionToast');
const fixedNow = new Date('2025-08-01T00:00:00').getTime();
const SESSION_IDS = getSessionIds(fixedNow);
const toastList = {
transactionToasts: [createMockToast(SESSION_IDS.MISSING, fixedNow)],
customToasts: []
};
const store: StoreType = {
...mockStore,
toasts: toastList,
transactions: {}
};
const result = await createToastsFromTransactions({ store });
expect(result.pendingTransactionToasts).toHaveLength(0);
expect(result.completedTransactionToasts).toHaveLength(0);
expect(createTransactionToast).not.toHaveBeenCalled();
});
});