Skip to content

Commit 1b53a60

Browse files
committed
test(historical-trend-view): verify mouse interactivity behavior
1 parent 41222eb commit 1b53a60

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { render, screen, fireEvent } from '@testing-library/react';
3+
import HistoricalTrendView from './HistoricalTrendView';
4+
import type { DashboardPeriod } from '@/utils/dashboardPeriod';
5+
import type { ActivityData } from '@/types/dashboard';
6+
import '@testing-library/jest-dom/vitest';
7+
8+
const pushMock = vi.fn();
9+
10+
vi.mock('next/navigation', () => ({
11+
useRouter: () => ({
12+
push: pushMock,
13+
}),
14+
}));
15+
16+
vi.mock('./Heatmap', () => ({
17+
default: ({ title, subtitle }: { title: string; subtitle: string }) => (
18+
<div data-testid="heatmap">
19+
{title} - {subtitle}
20+
</div>
21+
),
22+
}));
23+
24+
const mockPeriod: DashboardPeriod = {
25+
kind: 'month',
26+
month: '2025-05',
27+
label: 'May 2025',
28+
from: '2025-05-01T00:00:00.000Z',
29+
to: '2025-05-31T23:59:59.999Z',
30+
};
31+
32+
const mockActivity: ActivityData[] = [
33+
{
34+
date: '2025-05-01',
35+
count: 5,
36+
intensity: 4,
37+
},
38+
{
39+
date: '2025-05-02',
40+
count: 0,
41+
intensity: 0,
42+
},
43+
{
44+
date: '2025-05-03',
45+
count: 3,
46+
intensity: 2,
47+
},
48+
];
49+
50+
describe('HistoricalTrendView Mouse Interactivity', () => {
51+
beforeEach(() => {
52+
vi.clearAllMocks();
53+
});
54+
55+
it('navigates to previous period when Previous button is clicked', () => {
56+
render(<HistoricalTrendView username="kanishka" activity={mockActivity} period={mockPeriod} />);
57+
58+
const previousButton = screen.getByRole('button', {
59+
name: /previous/i,
60+
});
61+
62+
fireEvent.click(previousButton);
63+
64+
expect(pushMock).toHaveBeenCalledTimes(1);
65+
});
66+
67+
it('navigates to next period when Next button is clicked', () => {
68+
render(<HistoricalTrendView username="kanishka" activity={mockActivity} period={mockPeriod} />);
69+
70+
const nextButton = screen.getByRole('button', {
71+
name: /next/i,
72+
});
73+
74+
fireEvent.click(nextButton);
75+
76+
expect(pushMock).toHaveBeenCalledTimes(1);
77+
});
78+
79+
it('submits month selector form successfully', () => {
80+
render(<HistoricalTrendView username="kanishka" activity={mockActivity} period={mockPeriod} />);
81+
82+
const monthInput = screen.getByDisplayValue('2025-05');
83+
84+
fireEvent.change(monthInput, {
85+
target: { value: '2025-06' },
86+
});
87+
88+
const goButtons = screen.getAllByRole('button', {
89+
name: /go/i,
90+
});
91+
92+
fireEvent.click(goButtons[0]);
93+
94+
expect(pushMock).toHaveBeenCalled();
95+
});
96+
97+
it('submits year selector form successfully', () => {
98+
render(<HistoricalTrendView username="kanishka" activity={mockActivity} period={mockPeriod} />);
99+
100+
const yearInput = screen.getByRole('spinbutton');
101+
102+
fireEvent.change(yearInput, {
103+
target: { value: '2026' },
104+
});
105+
106+
const goButtons = screen.getAllByRole('button', {
107+
name: /go/i,
108+
});
109+
110+
fireEvent.click(goButtons[1]);
111+
112+
expect(pushMock).toHaveBeenCalled();
113+
});
114+
115+
it('submits custom date range successfully', () => {
116+
render(<HistoricalTrendView username="kanishka" activity={mockActivity} period={mockPeriod} />);
117+
118+
const dateInputs = screen.getAllByDisplayValue('2025-05-01');
119+
120+
fireEvent.change(dateInputs[0], {
121+
target: { value: '2025-05-01' },
122+
});
123+
124+
const endDateInput = screen.getByDisplayValue('2025-05-31');
125+
126+
fireEvent.change(endDateInput, {
127+
target: { value: '2025-05-31' },
128+
});
129+
130+
fireEvent.click(
131+
screen.getByRole('button', {
132+
name: /apply range/i,
133+
})
134+
);
135+
136+
expect(pushMock).toHaveBeenCalled();
137+
});
138+
});

0 commit comments

Comments
 (0)