forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePreview.test.tsx
More file actions
60 lines (54 loc) · 2.04 KB
/
Copy pathImagePreview.test.tsx
File metadata and controls
60 lines (54 loc) · 2.04 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
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { ImagePreview } from './ImagePreview';
import type { PreviewItem } from './ImagePreview';
function makeItem(overrides: Partial<PreviewItem> = {}): PreviewItem {
return {
id: '1',
file: new File(['x'], 'receipt.jpg', { type: 'image/jpeg' }),
previewUrl: 'blob:http://localhost/fake',
...overrides,
};
}
describe('ImagePreview', () => {
it('returns null when items is empty', () => {
const { container } = render(
<ImagePreview items={[]} onRemove={vi.fn()} />
);
expect(container.firstChild).toBeNull();
});
it('renders image previews', () => {
const items = [makeItem()];
render(<ImagePreview items={items} onRemove={vi.fn()} />);
const img = document.querySelector('img');
expect(img).toBeInTheDocument();
expect(img).toHaveAttribute('alt', 'receipt.jpg');
});
it('renders PDF placeholder when file is PDF', () => {
const items = [
makeItem({
file: new File(['x'], 'doc.pdf', { type: 'application/pdf' }),
previewUrl: undefined,
}),
];
render(<ImagePreview items={items} onRemove={vi.fn()} />);
const pdfLabels = screen.getAllByText('doc.pdf');
expect(pdfLabels.length).toBeGreaterThan(0);
});
it('calls onRemove when remove button is clicked', () => {
const onRemove = vi.fn();
const items = [makeItem({ id: 'a1' })];
render(<ImagePreview items={items} onRemove={onRemove} onCrop={vi.fn()} />);
const removeBtn = screen.getByLabelText(/remove receipt.jpg/i);
fireEvent.click(removeBtn);
expect(onRemove).toHaveBeenCalledWith('a1');
});
it('calls onCrop when crop button is clicked for image', () => {
const onCrop = vi.fn();
const items = [makeItem({ id: 'a1' })];
render(<ImagePreview items={items} onRemove={vi.fn()} onCrop={onCrop} />);
const cropBtn = screen.getByLabelText(/crop receipt.jpg/i);
fireEvent.click(cropBtn);
expect(onCrop).toHaveBeenCalledWith(items[0]);
});
});