-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageCard.test.tsx
More file actions
86 lines (73 loc) · 2.76 KB
/
ImageCard.test.tsx
File metadata and controls
86 lines (73 loc) · 2.76 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
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '../test-utils';
import { ImageCard } from './ImageCard';
import type { PlotImage } from '../types';
// Mock useCodeFetch to avoid actual API calls
vi.mock('../hooks/useCodeFetch', () => ({
useCodeFetch: () => ({ fetchCode: vi.fn().mockResolvedValue('print("hello")'), cache: new Map() }),
}));
const baseImage: PlotImage = {
library: 'matplotlib',
url: 'https://example.com/plot.png',
spec_id: 'scatter-basic',
title: 'Basic Scatter Plot',
};
const defaultProps = {
image: baseImage,
index: 0,
viewMode: 'library' as const,
selectedSpec: '',
openTooltip: null,
imageSize: 'normal' as const,
onTooltipToggle: vi.fn(),
onClick: vi.fn(),
};
describe('ImageCard', () => {
it('renders the card with spec_id label', () => {
render(<ImageCard {...defaultProps} />);
expect(screen.getByText('scatter-basic')).toBeInTheDocument();
});
it('renders library name in normal mode', () => {
render(<ImageCard {...defaultProps} />);
expect(screen.getByText('matplotlib')).toBeInTheDocument();
});
it('renders the plot image with responsive fallback src', () => {
render(<ImageCard {...defaultProps} />);
const img = screen.getByRole('img');
expect(img).toHaveAttribute('src', 'https://example.com/plot_800.png');
});
it('calls onClick when card is clicked', async () => {
const { userEvent } = await import('../test-utils');
const user = userEvent.setup();
const onClick = vi.fn();
render(<ImageCard {...defaultProps} onClick={onClick} />);
await user.click(screen.getByRole('button', { name: /view scatter-basic plot/i }));
expect(onClick).toHaveBeenCalledWith(baseImage);
});
it('has correct aria-label for library view mode', () => {
render(<ImageCard {...defaultProps} viewMode="library" />);
expect(screen.getByRole('button', { name: /view scatter-basic plot/i })).toHaveAttribute(
'aria-label',
'View scatter-basic plot in fullscreen'
);
});
it('toggles spec tooltip on spec_id click', async () => {
const { userEvent } = await import('../test-utils');
const user = userEvent.setup();
const onTooltipToggle = vi.fn();
render(<ImageCard {...defaultProps} onTooltipToggle={onTooltipToggle} />);
// Click on the spec_id text
await user.click(screen.getByText('scatter-basic'));
expect(onTooltipToggle).toHaveBeenCalledWith('spec-scatter-basic-matplotlib');
});
it('shows spec description when tooltip is open', () => {
render(
<ImageCard
{...defaultProps}
specDescription="A basic scatter plot example"
openTooltip="spec-scatter-basic-matplotlib"
/>
);
expect(screen.getByText('A basic scatter plot example')).toBeInTheDocument();
});
});