|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import { render, screen, act } from '@testing-library/react'; |
| 4 | +import React from 'react'; |
| 5 | +import RepositoryGraph from './RepositoryGraph'; |
| 6 | +import type { GraphNode, GraphLink } from '@/types'; |
| 7 | + |
| 8 | +vi.mock('next/dynamic', () => { |
| 9 | + const DynamicForceGraphMock = React.forwardRef((props: any, ref: any) => { |
| 10 | + React.useImperativeHandle(ref, () => ({ |
| 11 | + centerAt: vi.fn(), |
| 12 | + zoom: vi.fn(), |
| 13 | + })); |
| 14 | + |
| 15 | + return ( |
| 16 | + <div data-testid="force-graph-2d"> |
| 17 | + {props.graphData?.nodes?.map((node: any) => ( |
| 18 | + <div key={node.id}>{node.name}</div> |
| 19 | + ))} |
| 20 | + </div> |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + DynamicForceGraphMock.displayName = 'ForceGraph2D'; |
| 25 | + |
| 26 | + return { |
| 27 | + default: () => DynamicForceGraphMock, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +Object.defineProperty(HTMLElement.prototype, 'clientWidth', { |
| 32 | + configurable: true, |
| 33 | + value: 375, |
| 34 | +}); |
| 35 | + |
| 36 | +const mockData = { |
| 37 | + nodes: [ |
| 38 | + { id: 'user1', name: 'User 1', type: 'User', val: 30, color: '#FFF' }, |
| 39 | + { |
| 40 | + id: 'repo1', |
| 41 | + name: 'Repo 1', |
| 42 | + type: 'Repo', |
| 43 | + val: 15, |
| 44 | + color: '#FFF', |
| 45 | + stats: { stars: 10 }, |
| 46 | + }, |
| 47 | + { |
| 48 | + id: 'fork1', |
| 49 | + name: 'Fork 1', |
| 50 | + type: 'Fork', |
| 51 | + val: 10, |
| 52 | + color: '#FFF', |
| 53 | + }, |
| 54 | + { |
| 55 | + id: 'contrib1', |
| 56 | + name: 'Contrib 1', |
| 57 | + type: 'Contribution', |
| 58 | + val: 20, |
| 59 | + color: '#FFF', |
| 60 | + }, |
| 61 | + ] as GraphNode[], |
| 62 | + links: [ |
| 63 | + { source: 'user1', target: 'repo1' }, |
| 64 | + { source: 'user1', target: 'fork1' }, |
| 65 | + { source: 'user1', target: 'contrib1' }, |
| 66 | + ] as GraphLink[], |
| 67 | +}; |
| 68 | + |
| 69 | +describe('RepositoryGraph Responsive Breakpoints', () => { |
| 70 | + beforeEach(() => { |
| 71 | + vi.clearAllMocks(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('renders graph successfully on mobile viewport width', () => { |
| 75 | + render(<RepositoryGraph data={mockData} />); |
| 76 | + |
| 77 | + expect(screen.getByText('🌐 Repository Dependency Graph')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('renders filter controls in wrapped mobile layout', () => { |
| 81 | + render(<RepositoryGraph data={mockData} />); |
| 82 | + |
| 83 | + expect(screen.getByText('Personal')).toBeInTheDocument(); |
| 84 | + expect(screen.getByText('Contributions')).toBeInTheDocument(); |
| 85 | + expect(screen.getByText('Forks')).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('renders graph container at mobile width without crashing', () => { |
| 89 | + render(<RepositoryGraph data={mockData} />); |
| 90 | + |
| 91 | + expect(screen.getByTestId('force-graph-2d')).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('handles resize events correctly on smaller viewports', () => { |
| 95 | + render(<RepositoryGraph data={mockData} />); |
| 96 | + |
| 97 | + act(() => { |
| 98 | + window.dispatchEvent(new Event('resize')); |
| 99 | + }); |
| 100 | + |
| 101 | + expect(screen.getByText('🌐 Repository Dependency Graph')).toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('keeps desktop insight content available in rendered output', () => { |
| 105 | + render(<RepositoryGraph data={mockData} />); |
| 106 | + |
| 107 | + expect(screen.getByText('Graph Insights')).toBeInTheDocument(); |
| 108 | + |
| 109 | + expect(screen.getByText(/Repositories connected/i)).toBeInTheDocument(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments