forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraCapture.spec.tsx
More file actions
399 lines (322 loc) · 12.2 KB
/
Copy pathCameraCapture.spec.tsx
File metadata and controls
399 lines (322 loc) · 12.2 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
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { CameraCapture } from './CameraCapture';
// Mock the permission and compression utilities
vi.mock('../utils/cameraPermissions', () => ({
requestCameraPermission: vi.fn(),
stopCameraStream: vi.fn(),
checkCameraPermission: vi.fn(),
getUserFriendlyErrorMessage: vi.fn((error) => error?.message || 'Camera error'),
}));
vi.mock('../utils/imageCompression', () => ({
compressImage: vi.fn(),
blobToFile: vi.fn((blob, name) => new File([blob], name, { type: 'image/jpeg' })),
formatFileSize: vi.fn((bytes) => `${bytes} bytes`),
isValidImageType: vi.fn(() => true),
}));
import * as cameraPermissions from '../../utils/cameraPermissions';
import * as imageCompression from '../../utils/imageCompression';
describe('CameraCapture Component', () => {
let mockStream: MediaStream;
beforeEach(() => {
// Create mock MediaStream
mockStream = {
getTracks: vi.fn(() => [
{
stop: vi.fn(),
},
]),
} as any;
// Setup default mocks
vi.mocked(cameraPermissions.requestCameraPermission).mockResolvedValue(
mockStream
);
vi.mocked(cameraPermissions.checkCameraPermission).mockResolvedValue(
'prompt'
);
vi.mocked(cameraPermissions.stopCameraStream).mockImplementation(() => {});
});
afterEach(() => {
vi.clearAllMocks();
});
describe('Camera Access', () => {
it('should request camera permission on mount', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(
cameraPermissions.requestCameraPermission
).toHaveBeenCalledWith(
expect.objectContaining({
video: expect.any(Object),
audio: false,
})
);
});
});
it('should show requesting state while accessing camera', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
expect(screen.getByText(/requesting camera access/i)).toBeInTheDocument();
});
it('should show active state when camera is ready', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
const video = screen.getByRole('img', { hidden: true }) ||
document.querySelector('video');
expect(video).toBeInTheDocument();
});
});
it('should handle permission denied error', async () => {
vi.mocked(cameraPermissions.checkCameraPermission).mockResolvedValue(
'denied'
);
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByText(/permission was previously denied/i)).toBeInTheDocument();
});
});
it('should handle camera not found error', async () => {
const error = new Error('Camera not found');
(error as any).permissionError = {
type: 'not-found',
message: 'No camera device found',
};
vi.mocked(cameraPermissions.requestCameraPermission).mockRejectedValue(
error
);
const mockOnCapture = vi.fn();
const mockOnError = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} onError={mockOnError} />);
await waitFor(() => {
expect(screen.getByText(/camera error/i)).toBeInTheDocument();
});
expect(mockOnError).toHaveBeenCalledWith(error);
});
});
describe('Camera Controls', () => {
it('should render capture button', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByLabelText(/take photo/i)).toBeInTheDocument();
});
});
it('should render camera switch button', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByLabelText(/switch camera/i)).toBeInTheDocument();
});
});
it('should render file upload option', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByLabelText(/upload image file/i)).toBeInTheDocument();
});
});
});
describe('Image Capture', () => {
it('should capture image when capture button is clicked', async () => {
const mockOnCapture = vi.fn();
const { container } = render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByLabelText(/take photo/i)).toBeInTheDocument();
});
// Mock canvas context
const canvasEl = container.querySelector('canvas');
expect(canvasEl).toBeInTheDocument();
const captureButton = screen.getByLabelText(/take photo/i);
fireEvent.click(captureButton);
// Give time for image processing
await waitFor(() => {
expect(screen.getByText(/retake/i)).toBeInTheDocument();
});
});
it('should show preview after capture', async () => {
const mockOnCapture = vi.fn();
const { container } = render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/take photo/i));
});
await waitFor(() => {
const images = container.querySelectorAll('img');
expect(images.length).toBeGreaterThan(0);
});
});
});
describe('Image Compression', () => {
it('should compress image on confirm', async () => {
const mockBlob = new Blob(['test'], { type: 'image/jpeg' });
vi.mocked(imageCompression.compressImage).mockResolvedValue(mockBlob);
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
// Capture image first
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/take photo/i));
});
// Confirm capture
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/confirm and upload photo/i));
});
await waitFor(() => {
expect(imageCompression.compressImage).toHaveBeenCalled();
});
});
it('should retry compression with lower quality if file too large', async () => {
const largeBlob = new Blob(
[new ArrayBuffer(6 * 1024 * 1024)],
{ type: 'image/jpeg' }
); // 6MB
const smallBlob = new Blob(['test'], { type: 'image/jpeg' });
vi.mocked(imageCompression.compressImage)
.mockResolvedValueOnce(largeBlob) // First attempt returns large blob
.mockResolvedValueOnce(smallBlob); // Retry with lower quality
const mockOnCapture = vi.fn();
render(
<CameraCapture
onCapture={mockOnCapture}
maxFileSize={5 * 1024 * 1024}
/>
);
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/take photo/i));
});
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/confirm and upload photo/i));
});
await waitFor(() => {
expect(imageCompression.compressImage).toHaveBeenCalledTimes(2);
});
});
it('should call onCapture with compressed file', async () => {
const mockBlob = new Blob(['compressed'], { type: 'image/jpeg' });
vi.mocked(imageCompression.compressImage).mockResolvedValue(mockBlob);
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/take photo/i));
});
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/confirm and upload photo/i));
});
await waitFor(() => {
expect(mockOnCapture).toHaveBeenCalledWith(expect.any(File));
});
});
});
describe('Retake Functionality', () => {
it('should return to camera view on retake', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
// Capture image
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/take photo/i));
});
// Retake
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/retake photo/i));
});
// Should be back to requesting or ready state
await waitFor(() => {
expect(screen.queryByLabelText(/retake photo/i)).not.toBeInTheDocument();
});
});
});
describe('File Upload Fallback', () => {
it('should handle file upload', async () => {
const mockBlob = new Blob(['test'], { type: 'image/jpeg' });
vi.mocked(imageCompression.compressImage).mockResolvedValue(mockBlob);
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByLabelText(/upload image file/i)).toBeInTheDocument();
});
const fileInput = screen.getByLabelText(/upload image file/i) as HTMLInputElement;
const file = new File(['test'], 'test.jpg', { type: 'image/jpeg' });
fireEvent.change(fileInput, { target: { files: [file] } });
await waitFor(() => {
expect(imageCompression.compressImage).toHaveBeenCalledWith(
expect.any(File),
expect.any(Object)
);
});
});
it('should validate image type on upload', async () => {
vi.mocked(imageCompression.isValidImageType).mockReturnValue(false);
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
const fileInput = screen.getByLabelText(/upload image file/i) as HTMLInputElement;
const file = new File(['test'], 'test.txt', { type: 'text/plain' });
fireEvent.change(fileInput, { target: { files: [file] } });
});
// Error should be shown
await waitFor(() => {
expect(screen.queryByText(/error/i)).toBeDefined();
});
});
});
describe('Error Handling', () => {
it('should display error message on camera access failure', async () => {
const error = new Error('Permission denied');
(error as any).permissionError = {
type: 'permission-denied',
message: 'Camera permission denied',
};
vi.mocked(cameraPermissions.requestCameraPermission).mockRejectedValue(
error
);
const mockOnCapture = vi.fn();
const mockOnError = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} onError={mockOnError} />);
await waitFor(() => {
expect(screen.getByText(/camera error/i)).toBeInTheDocument();
});
expect(mockOnError).toHaveBeenCalledWith(error);
});
it('should call onError callback on compression failure', async () => {
const error = new Error('Compression failed');
vi.mocked(imageCompression.compressImage).mockRejectedValue(error);
const mockOnCapture = vi.fn();
const mockOnError = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} onError={mockOnError} />);
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/take photo/i));
});
await waitFor(() => {
fireEvent.click(screen.getByLabelText(/confirm and upload photo/i));
});
await waitFor(() => {
expect(mockOnError).toHaveBeenCalledWith(error);
});
});
});
describe('Accessibility', () => {
it('should have proper ARIA labels', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByLabelText(/take photo/i)).toHaveAttribute(
'aria-label'
);
expect(screen.getByLabelText(/switch camera/i)).toHaveAttribute(
'aria-label'
);
});
});
it('should be keyboard accessible', async () => {
const mockOnCapture = vi.fn();
render(<CameraCapture onCapture={mockOnCapture} />);
await waitFor(() => {
expect(screen.getByLabelText(/take photo/i)).toBeInTheDocument();
});
const captureButton = screen.getByLabelText(/take photo/i);
// Button should not be disabled
expect(captureButton).not.toHaveAttribute('disabled');
});
});
});