forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiptCaptureFlow.test.tsx
More file actions
219 lines (199 loc) · 5.61 KB
/
Copy pathReceiptCaptureFlow.test.tsx
File metadata and controls
219 lines (199 loc) · 5.61 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
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { ReceiptCaptureFlow } from './ReceiptCaptureFlow';
const uploadReceiptForSplitMock = vi.fn();
const fetchReceiptOcrDataMock = vi.fn();
const fetchReceiptSignedUrlMock = vi.fn();
vi.mock('../CameraCapture', () => ({
CameraCapture: ({
onCapture,
}: {
onCapture: (file: File) => void;
}) => (
<button
type="button"
onClick={() =>
onCapture(new File(['image'], 'receipt.jpg', { type: 'image/jpeg' }))
}
>
Mock capture
</button>
),
}));
vi.mock('../ReceiptUpload', () => ({
ReceiptUpload: ({
onFilesChange,
onManualEntry,
}: {
onFilesChange?: (files: File[]) => void;
onManualEntry?: (data: {
amount: string;
date: string;
merchant: string;
notes: string;
}) => void;
}) => (
<div>
<button
type="button"
onClick={() =>
onFilesChange?.([
new File(['image'], 'grocery-receipt.jpg', { type: 'image/jpeg' }),
])
}
>
Mock upload
</button>
<button
type="button"
onClick={() =>
onManualEntry?.({
amount: '18.75',
date: '2026-03-25',
merchant: 'Corner Store',
notes: 'Late snack run',
})
}
>
Mock upload manual
</button>
</div>
),
ManualEntryFallback: ({
onSubmit,
onCancel,
}: {
onSubmit: (data: {
amount: string;
date: string;
merchant: string;
notes: string;
}) => void;
onCancel: () => void;
}) => (
<div>
<button
type="button"
onClick={() =>
onSubmit({
amount: '42.00',
date: '2026-03-25',
merchant: 'Manual Cafe',
notes: 'Brunch',
})
}
>
Submit manual details
</button>
<button type="button" onClick={onCancel}>
Cancel manual details
</button>
</div>
),
}));
vi.mock('./ReceiptParserResults', () => ({
ReceiptParserResults: ({
items,
onAccept,
onReject,
}: {
items: Array<{ name: string }>;
onAccept: (items: Array<{ name: string }>) => void;
onReject: () => void;
}) => (
<div>
<div data-testid="review-item-count">{items.length}</div>
<button type="button" onClick={() => onAccept(items)}>
Accept parsed receipt
</button>
<button type="button" onClick={onReject}>
Reject parsed receipt
</button>
</div>
),
}));
vi.mock('../../utils/receiptOcr', () => ({
createManualReviewItems: (manualEntry: { amount: string; merchant: string }) => [
{
id: 'manual-item-1',
name: manualEntry.merchant || 'Manual receipt',
quantity: 1,
price: Number.parseFloat(manualEntry.amount),
confidence: 100,
},
],
}));
vi.mock('../../utils/api-client', () => ({
uploadReceiptForSplit: (...args: unknown[]) => uploadReceiptForSplitMock(...args),
fetchReceiptOcrData: (...args: unknown[]) => fetchReceiptOcrDataMock(...args),
fetchReceiptSignedUrl: (...args: unknown[]) => fetchReceiptSignedUrlMock(...args),
getApiErrorMessage: (error: unknown) =>
error instanceof Error ? error.message : 'Receipt request failed',
}));
describe('ReceiptCaptureFlow', () => {
beforeEach(() => {
localStorage.clear();
vi.clearAllMocks();
uploadReceiptForSplitMock.mockResolvedValue({
id: 'receipt-123',
});
fetchReceiptOcrDataMock.mockResolvedValue({
processed: true,
data: {
total: 31.5,
confidence: 0.91,
items: [
{
name: 'Fresh Produce',
quantity: 1,
price: 18.5,
},
{
name: 'Snacks',
quantity: 1,
price: 13,
},
],
},
});
fetchReceiptSignedUrlMock.mockResolvedValue('https://example.com/receipt.jpg');
});
it('lets the user upload a receipt and apply reviewed OCR items', async () => {
const onApply = vi.fn();
render(
<ReceiptCaptureFlow splitId="split-123" currency="USD" onApply={onApply} />
);
fireEvent.click(screen.getByRole('button', { name: /upload receipt/i }));
fireEvent.click(screen.getByRole('button', { name: /^mock upload$/i }));
await waitFor(() =>
expect(screen.getByRole('button', { name: /accept parsed receipt/i })).toBeInTheDocument()
);
fireEvent.click(screen.getByRole('button', { name: /accept parsed receipt/i }));
expect(onApply).toHaveBeenCalledWith(
expect.objectContaining({
merchant: 'grocery-receipt',
receiptTotal: 31.5,
items: expect.arrayContaining([
expect.objectContaining({ name: 'Fresh Produce' }),
]),
})
);
});
it('keeps a draft in localStorage and resumes review state', async () => {
const onApply = vi.fn();
const { unmount } = render(
<ReceiptCaptureFlow splitId="split-abc" currency="USD" onApply={onApply} />
);
fireEvent.click(screen.getByRole('button', { name: /upload receipt/i }));
fireEvent.click(screen.getByRole('button', { name: /mock upload manual/i }));
await waitFor(() =>
expect(screen.getByRole('button', { name: /accept parsed receipt/i })).toBeInTheDocument()
);
unmount();
render(
<ReceiptCaptureFlow splitId="split-abc" currency="USD" onApply={onApply} />
);
expect(screen.getByText(/corner store/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /accept parsed receipt/i })).toBeInTheDocument();
});
});