-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWidget.test.tsx
More file actions
657 lines (550 loc) · 16.7 KB
/
Widget.test.tsx
File metadata and controls
657 lines (550 loc) · 16.7 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// react/lib/tests/components/Widget.test.tsx
import { render, screen, waitFor, cleanup } from '@testing-library/react'
import { act } from 'react'
import userEvent from '@testing-library/user-event'
import { WidgetContainer as Widget } from '../../components/Widget/WidgetContainer'
import { TEST_ADDRESSES } from '../util/constants'
import copyToClipboard from 'copy-to-clipboard'
import type { Currency, Transaction } from '../../util'
import { isFiat } from '../../util';
import config from '../../paybutton-config.json'
jest.mock('copy-to-clipboard', () => jest.fn())
jest.mock('../../util', () => {
const real = jest.requireActual('../../util')
return {
__esModule: true,
...real,
getFiatPrice: jest.fn(async (currency: string, to: string) => {
if (isFiat(currency)) {
return 100
}
return null
}),
setupChronikWebSocket: jest.fn().mockResolvedValue(undefined),
setupAltpaymentSocket: jest.fn().mockResolvedValue(undefined),
getAddressBalance: jest.fn().mockResolvedValue(0),
openCashtabPayment: jest.fn(),
createPayment: jest.fn(async () => '00112233445566778899aabbccddeeff'),
}
})
const CRYPTO_CASES: { label: string; currency: Currency; to: string }[] = [
{ label: 'XEC', currency: 'XEC' as Currency, to: TEST_ADDRESSES.ecash },
{ label: 'BCH', currency: 'BCH' as Currency, to: TEST_ADDRESSES.bitcoincash },
]
const FIAT_CASES: { label: string; currency: Currency; price: number, to: string }[] = [
{ label: 'USD', currency: 'USD' as Currency, price: 10, to: TEST_ADDRESSES.ecash },
{ label: 'CAD', currency: 'CAD' as Currency, price: 20, to: TEST_ADDRESSES.ecash},
]
beforeEach(() => {
jest.clearAllMocks()
cleanup()
})
describe('Widget – standalone paymentId (crypto)', () => {
test.each(CRYPTO_CASES)(
'%s – first render triggers createPayment(amount)',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
(createPayment as jest.Mock).mockResolvedValue('pid-crypto-1')
render(
<Widget
to={to}
amount={5}
currency={currency}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledWith(5, to, config.apiBaseUrl)
})
}
)
test.each(CRYPTO_CASES)(
'%s – amount change triggers new paymentId',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock)
.mockResolvedValueOnce('pid-crypto-1')
.mockResolvedValueOnce('pid-crypto-2')
const { rerender } = render(
<Widget
to={to}
amount={5}
currency={currency}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
rerender(
<Widget
to={to}
amount={8}
currency={currency}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(2)
})
expect((createPayment as jest.Mock).mock.calls[1][0]).toBe(8)
}
)
test.each(CRYPTO_CASES)(
'%s – same amount across rerender does NOT regenerate',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock).mockResolvedValue('pid-crypto')
const { rerender } = render(
<Widget
to={to}
amount={5}
currency={currency}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
rerender(
<Widget
to={to}
amount={5}
currency={currency}
/>
)
// still only the first call
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
}
)
test.each(CRYPTO_CASES)(
'%s – no paymentId when disablePaymentId=true',
async ({ currency, to }) => {
render(
<Widget
to={to}
amount={5}
currency={currency}
disablePaymentId={true}
/>
)
// WIP wait for loading to go away
const { createPayment } = require('../../util');
await waitFor(() => {
expect(createPayment).not.toHaveBeenCalled()
})
}
)
test.each(CRYPTO_CASES)(
'%s – no paymentId when isChild=true',
async ({ currency, to }) => {
render(
<Widget
to={to}
amount={5}
currency={currency}
isChild={true}
/>
)
// WIP wait for loading to go away
const { createPayment } = require('../../util');
await waitFor(() => {
expect(createPayment).not.toHaveBeenCalled()
})
}
)
test.each(CRYPTO_CASES)(
'%s – undefined amount passes undefined to createPayment',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock).mockResolvedValue('pid-crypto-undef')
render(
<Widget
to={to}
currency={currency}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
expect(createPayment).toHaveBeenCalledWith(undefined, to, config.apiBaseUrl)
}
)
})
describe('Widget – standalone paymentId (fiat)', () => {
test.each(FIAT_CASES)(
'%s – uses internal conversion (amount / price) for paymentId',
async ({ currency, price }) => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock).mockResolvedValue('pid-fiat-1')
const amount = 50
const to = TEST_ADDRESSES.ecash
render(
<Widget
to={to}
amount={amount}
currency={currency}
price={price}
/>
)
const expectedCrypto = amount / price
await waitFor(() => {
expect(createPayment).toHaveBeenCalledWith(expectedCrypto, to, config.apiBaseUrl)
})
}
)
test.each(FIAT_CASES)(
'%s – new converted amount regenerates paymentId',
async ({ currency, price }) => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock)
.mockResolvedValueOnce('pid-fiat-1')
.mockResolvedValueOnce('pid-fiat-2')
const to = TEST_ADDRESSES.ecash
const { rerender } = render(
<Widget
to={to}
amount={50}
currency={currency}
price={price}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
rerender(
<Widget
to={to}
amount={90}
currency={currency}
price={price}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(2)
})
const lastCall = (createPayment as jest.Mock).mock.calls[1]
expect(lastCall[0]).toBe(90 / price)
expect(lastCall[1]).toBe(to)
}
)
})
describe('Widget – editable amount input (crypto)', () => {
test.each(CRYPTO_CASES)(
'%s – editing amount to a new value regenerates paymentId',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
const user = userEvent.setup()
render(
<Widget
to={to}
amount={5}
currency={currency}
editable={true}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
;(createPayment as jest.Mock).mockResolvedValueOnce('aa112233445566778899aabbccddeeff')
const input = screen.getByLabelText(/edit amount/i)
await user.clear(input)
await user.type(input, '8')
await user.keyboard('{Enter}')
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(2)
})
const lastCall = (createPayment as jest.Mock).mock.calls[1]
expect(lastCall[0]).toBe(8)
expect(lastCall[1]).toBe(to)
}
)
test.each(CRYPTO_CASES)(
'%s – editing amount to SAME value does NOT regenerate paymentId',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock).mockResolvedValue('pid-edit-same')
const user = userEvent.setup()
render(
<Widget
to={to}
amount={5}
currency={currency}
editable={true}
/>
)
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
const input = screen.getByLabelText(/edit amount/i)
await user.clear(input)
await user.type(input, '5')
await user.keyboard('{Enter}')
// applyDraftAmount should not fire (isSameAmount=true), so no new call
await waitFor(() => {
expect(createPayment).toHaveBeenCalledTimes(1)
})
}
)
})
describe('Widget – QR copy interaction', () => {
test.each([...CRYPTO_CASES, ...FIAT_CASES])(
'clicking QR copies full URL for %s address',
async ({ currency, to, label }) => {
await act(async () => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock).mockResolvedValue(`pid-qr-${label}`)
;(copyToClipboard as jest.Mock).mockReturnValue(true)
render(
<Widget
to={to}
amount={10}
currency={currency}
/>
)
})
const user = userEvent.setup()
await waitFor(() => {
expect(screen.queryByText(/Click to copy/i)).toBeTruthy()
})
const qrBox = screen.getByTestId('qr-click-area')
await user.click(qrBox)
expect(copyToClipboard).toHaveBeenCalledTimes(1)
const copied = (copyToClipboard as jest.Mock).mock.calls[0][0] as string
if (currency === 'XEC') {
expect(copied).toContain('ecash:')
} else if (currency === 'BCH') {
expect(copied).toContain('bitcoincash:')
}
}
)
})
describe('Widget – loading behaviour (WIP)', () => {
const CASES = [...CRYPTO_CASES, ...FIAT_CASES]
const WITH_AMOUNTS = CASES.flatMap(base => [
{ ...base, amount: 3 },
{ ...base, amount: undefined },
])
test.each(WITH_AMOUNTS)(
'handleQrCodeClick does nothing while component is loading (%s, amount=%s)',
async ({ currency, to, amount }) => {
const { createPayment } = require('../../util')
;(createPayment as jest.Mock).mockImplementation(
() =>
new Promise(resolve =>
setTimeout(() => resolve('some-payment-id'), 1000),
),
)
render(<Widget to={to} amount={amount} currency={currency} />)
await waitFor(() => {
expect(screen.getByText(/loading/i)).toBeTruthy()
})
const qr = await screen.findByTestId('qr-click-area')
const user = userEvent.setup()
await waitFor(() => {
expect(screen.getByText(/loading/i)).toBeTruthy()
})
expect(copyToClipboard).not.toHaveBeenCalled()
await user.click(qr)
expect(copyToClipboard).not.toHaveBeenCalled()
},
)
test.each(WITH_AMOUNTS)(
'widget button is disabled while component is loading', async ({ currency, to, amount }) => {
const { createPayment } = require('../../util');
;(createPayment as jest.Mock).mockImplementation(() =>
new Promise(resolve => setTimeout(() => resolve('some-payment-id'), 1000)),
)
render(
<Widget
to={to}
currency={currency}
amount={amount}
/>,
)
await waitFor(() => expect(screen.getByText(/loading/i)).toBeTruthy())
const btn = screen.getByRole('button', { name: /send with.*wallet/i })
await waitFor(() => {
expect(screen.getByText(/loading/i)).toBeTruthy()
})
expect(btn.hasAttribute('disabled')).toBeTruthy()
})
})
describe('Widget – hideSendButton', () => {
test.each(CRYPTO_CASES)(
'%s – send button is hidden when hideSendButton=true',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
(createPayment as jest.Mock).mockResolvedValue('pid-hide-test')
render(
<Widget
to={to}
amount={5}
currency={currency}
hideSendButton={true}
/>
)
await waitFor(() => {
expect(screen.queryByText(/loading/i)).toBeNull()
})
// The send button should not be present
const sendButton = screen.queryByRole('button', { name: /send with.*wallet/i })
expect(sendButton).toBeNull()
}
)
test.each(CRYPTO_CASES)(
'%s – send button is visible when hideSendButton=false',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
(createPayment as jest.Mock).mockResolvedValue('pid-show-test')
render(
<Widget
to={to}
amount={5}
currency={currency}
hideSendButton={false}
/>
)
await waitFor(() => {
expect(screen.queryByText(/loading/i)).toBeNull()
})
// The send button should be present
const sendButton = await screen.findByRole('button', { name: /send with.*wallet/i })
expect(sendButton).toBeTruthy()
}
)
test.each(CRYPTO_CASES)(
'%s – send button is visible by default (hideSendButton undefined)',
async ({ currency, to }) => {
const { createPayment } = require('../../util');
(createPayment as jest.Mock).mockResolvedValue('pid-default-test')
render(
<Widget
to={to}
amount={5}
currency={currency}
/>
)
await waitFor(() => {
expect(screen.queryByText(/loading/i)).toBeNull()
})
// The send button should be present by default
const sendButton = await screen.findByRole('button', { name: /send with.*wallet/i })
expect(sendButton).toBeTruthy()
}
)
})
describe('Widget – payment finalization states', () => {
test('XEC shows pending spinner on detection and runs success on finalization', async () => {
const onSuccess = jest.fn()
const onTransaction = jest.fn()
const setNewTxs = jest.fn()
const xecMempoolTx: Transaction = {
hash: 'xec-mempool-hash',
amount: '1',
paymentId: '',
message: '',
rawMessage: '',
timestamp: 1,
address: TEST_ADDRESSES.ecash,
confirmed: false,
txStatus: 'mempool',
}
const xecFinalizedTx: Transaction = {
...xecMempoolTx,
hash: 'xec-finalized-hash',
confirmed: true,
txStatus: 'finalized',
}
const { rerender } = render(
<Widget
to={TEST_ADDRESSES.ecash}
currency={'XEC'}
disablePaymentId={true}
sound={false}
onSuccess={onSuccess}
onTransaction={onTransaction}
setNewTxs={setNewTxs}
/>
)
await waitFor(() => {
expect(screen.queryByText(/loading/i)).toBeNull()
})
rerender(
<Widget
to={TEST_ADDRESSES.ecash}
currency={'XEC'}
disablePaymentId={true}
sound={false}
onSuccess={onSuccess}
onTransaction={onTransaction}
setNewTxs={setNewTxs}
newTxs={[xecMempoolTx]}
/>
)
await waitFor(() => {
expect(onTransaction).toHaveBeenCalled()
})
expect(onSuccess).not.toHaveBeenCalled()
expect(screen.queryByText(/waiting for finalization/i)).toBeNull()
rerender(
<Widget
to={TEST_ADDRESSES.ecash}
currency={'XEC'}
disablePaymentId={true}
sound={false}
onSuccess={onSuccess}
onTransaction={onTransaction}
setNewTxs={setNewTxs}
newTxs={[xecFinalizedTx]}
/>
)
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
})
await waitFor(() => {
expect(screen.getByText(/thank you!/i)).toBeTruthy()
})
})
test('BCH keeps immediate success on first detected transaction', async () => {
const onSuccess = jest.fn()
const setNewTxs = jest.fn()
const bchDetectedTx: Transaction = {
hash: 'bch-detected-hash',
amount: '1',
paymentId: '',
message: '',
rawMessage: '',
timestamp: 1,
address: TEST_ADDRESSES.bitcoincash,
confirmed: false,
txStatus: 'mempool',
}
const { rerender } = render(
<Widget
to={TEST_ADDRESSES.bitcoincash}
currency={'BCH'}
disablePaymentId={true}
sound={false}
onSuccess={onSuccess}
setNewTxs={setNewTxs}
/>
)
await waitFor(() => {
expect(screen.queryByText(/loading/i)).toBeNull()
})
rerender(
<Widget
to={TEST_ADDRESSES.bitcoincash}
currency={'BCH'}
disablePaymentId={true}
sound={false}
onSuccess={onSuccess}
setNewTxs={setNewTxs}
newTxs={[bchDetectedTx]}
/>
)
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
})
expect(screen.queryByText(/waiting for finalization/i)).toBeNull()
})
})