forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportSplitCalculation.spec.ts
More file actions
54 lines (48 loc) · 1.65 KB
/
Copy pathexportSplitCalculation.spec.ts
File metadata and controls
54 lines (48 loc) · 1.65 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
import { exportSplitCalculation, buildSplitCalculationFileName } from './exportSplitCalculation';
describe('exportSplitCalculation', () => {
const originalCreateObjectURL = URL.createObjectURL;
const originalRevokeObjectURL = URL.revokeObjectURL;
const clickSpy = vi.fn();
const elementMock = {
href: '',
download: '',
click: clickSpy,
} as unknown as HTMLAnchorElement;
beforeEach(() => {
vi.spyOn(document, 'createElement').mockImplementation(() => elementMock);
vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob://test-url');
vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {});
clickSpy.mockClear();
});
afterEach(() => {
vi.restoreAllMocks();
Object.defineProperty(URL, 'createObjectURL', {
value: originalCreateObjectURL,
writable: true,
});
Object.defineProperty(URL, 'revokeObjectURL', {
value: originalRevokeObjectURL,
writable: true,
});
});
it('creates a JSON export and triggers a download', () => {
exportSplitCalculation(
{
type: 'equal',
participants: [{ id: '1', name: 'Alice', amount: 10 }],
items: [],
subtotal: 10,
currency: 'USD',
},
'custom-name.json',
);
expect(elementMock.href).toBe('blob://test-url');
expect(elementMock.download).toBe('custom-name.json');
expect(clickSpy).toHaveBeenCalled();
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob://test-url');
});
it('generates a default file name when none is provided', () => {
const fileName = buildSplitCalculationFileName('test-prefix');
expect(fileName).toMatch(/^test-prefix-\d+\.json$/);
});
});