forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQRCodeScanner.test.tsx
More file actions
98 lines (83 loc) · 2.75 KB
/
Copy pathQRCodeScanner.test.tsx
File metadata and controls
98 lines (83 loc) · 2.75 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
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi, afterEach } from 'vitest';
import { QRCodeScanner } from './QRCodeScanner';
import jsQR from 'jsqr';
import { buildStellarPaymentURI } from '../../utils/stellar/paymentUri';
vi.mock('jsqr', () => ({
default: vi.fn(),
}));
const DESTINATION = 'GDQP2KPQGKIHYJGXNUIYOMHARUARCA6NSWVE2YQYCVY75HL7P5G4U2DI';
describe('QRCodeScanner', () => {
beforeEach(() => {
vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
callback(0);
return 1;
});
vi.stubGlobal('cancelAnimationFrame', vi.fn());
Object.defineProperty(HTMLMediaElement.prototype, 'play', {
writable: true,
value: vi.fn().mockResolvedValue(undefined),
});
Object.defineProperty(HTMLVideoElement.prototype, 'readyState', {
configurable: true,
get: () => 4,
});
Object.defineProperty(HTMLVideoElement.prototype, 'videoWidth', {
configurable: true,
get: () => 200,
});
Object.defineProperty(HTMLVideoElement.prototype, 'videoHeight', {
configurable: true,
get: () => 200,
});
Object.defineProperty(HTMLCanvasElement.prototype, 'getContext', {
configurable: true,
value: vi.fn(() => ({
drawImage: vi.fn(),
getImageData: vi.fn(() => ({
data: new Uint8ClampedArray(200 * 200 * 4),
width: 200,
height: 200,
})),
})),
});
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
it('shows an error when camera API is unavailable', async () => {
Object.defineProperty(navigator, 'mediaDevices', {
configurable: true,
value: undefined,
});
render(<QRCodeScanner isOpen onClose={vi.fn()} onConfirm={vi.fn()} />);
await waitFor(() => {
expect(screen.getByText(/camera api is not available/i)).toBeInTheDocument();
});
});
it('detects a valid QR and confirms payment', async () => {
Object.defineProperty(navigator, 'mediaDevices', {
configurable: true,
value: {
getUserMedia: vi.fn().mockResolvedValue({
getTracks: () => [{ stop: vi.fn() }],
}),
},
});
const uri = buildStellarPaymentURI({
destination: DESTINATION,
amount: 9.9,
memo: 'split_007',
memoType: 'text',
});
vi.mocked(jsQR).mockReturnValue({ data: uri } as any);
const onConfirm = vi.fn();
render(<QRCodeScanner isOpen onClose={vi.fn()} onConfirm={onConfirm} />);
await waitFor(() => {
expect(screen.getByText(/payment request detected/i)).toBeInTheDocument();
});
fireEvent.click(screen.getByText('Confirm and Pay'));
expect(onConfirm).toHaveBeenCalledTimes(1);
});
});