|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, vi, beforeAll } from 'vitest'; |
| 3 | +import type { ReactNode, HTMLAttributes } from 'react'; |
| 4 | +import Heatmap from './Heatmap'; |
| 5 | +import type { ActivityData } from '@/types/dashboard'; |
| 6 | + |
| 7 | +// 1. Mock ResizeObserver (JSDOM lacks it) |
| 8 | +beforeAll(() => { |
| 9 | + global.ResizeObserver = class ResizeObserver { |
| 10 | + observe() {} |
| 11 | + unobserve() {} |
| 12 | + disconnect() {} |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +// 2. Mock framer-motion with inline types to prevent hoisting errors |
| 17 | +vi.mock('framer-motion', () => ({ |
| 18 | + motion: { |
| 19 | + div: (props: HTMLAttributes<HTMLDivElement> & { children?: ReactNode }) => ( |
| 20 | + <div |
| 21 | + className={props.className} |
| 22 | + style={props.style} |
| 23 | + onMouseEnter={props.onMouseEnter} |
| 24 | + onMouseLeave={props.onMouseLeave} |
| 25 | + > |
| 26 | + {props.children} |
| 27 | + </div> |
| 28 | + ), |
| 29 | + }, |
| 30 | + AnimatePresence: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 31 | +})); |
| 32 | + |
| 33 | +describe('Heatmap', () => { |
| 34 | + // Create a strongly typed empty array to fix the `data={[]}` red lines |
| 35 | + const emptyData: ActivityData[] = []; |
| 36 | + |
| 37 | + // Helper to generate properly typed mock data |
| 38 | + const generateMockData = (days: number): ActivityData[] => { |
| 39 | + return Array.from({ length: days }, (_, i) => ({ |
| 40 | + date: `2024-01-${(i % 28) + 1}`, |
| 41 | + count: 5, |
| 42 | + intensity: 2, |
| 43 | + })); |
| 44 | + }; |
| 45 | + |
| 46 | + it("renders 'Contribution Heatmap' heading", () => { |
| 47 | + render(<Heatmap data={emptyData} />); |
| 48 | + expect(screen.getByRole('heading', { name: /contribution heatmap/i })).toBeDefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("renders 'Last 365 days' subtitle", () => { |
| 52 | + render(<Heatmap data={emptyData} />); |
| 53 | + expect(screen.getByText(/last 365 days/i)).toBeDefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("renders 'Less' and 'More' legend labels", () => { |
| 57 | + render(<Heatmap data={emptyData} />); |
| 58 | + expect(screen.getByText(/less/i)).toBeDefined(); |
| 59 | + expect(screen.getByText(/more/i)).toBeDefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('renders without crashing with an empty data array', () => { |
| 63 | + expect(() => render(<Heatmap data={emptyData} />)).not.toThrow(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('renders without crashing with 365 days of data', () => { |
| 67 | + expect(() => render(<Heatmap data={generateMockData(365)} />)).not.toThrow(); |
| 68 | + }); |
| 69 | +}); |
0 commit comments