|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { render, fireEvent } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import Leaderboard, { Contributor } from './Leaderboard'; |
| 5 | + |
| 6 | +// Mock Next.js Image |
| 7 | +vi.mock('next/image', () => ({ |
| 8 | + default: (props: React.ImgHTMLAttributes<HTMLImageElement>) => <img alt="mock" {...props} />, |
| 9 | +})); |
| 10 | + |
| 11 | +// Mock framer-motion strictly |
| 12 | +vi.mock('framer-motion', async () => { |
| 13 | + const actual = await vi.importActual('framer-motion'); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + motion: { |
| 17 | + div: ({ |
| 18 | + children, |
| 19 | + className, |
| 20 | + onClick, |
| 21 | + style, |
| 22 | + }: { |
| 23 | + children?: React.ReactNode; |
| 24 | + className?: string; |
| 25 | + onClick?: React.MouseEventHandler<HTMLDivElement>; |
| 26 | + style?: React.CSSProperties; |
| 27 | + }) => ( |
| 28 | + <div className={className} onClick={onClick} style={style} data-testid="motion-div"> |
| 29 | + {children} |
| 30 | + </div> |
| 31 | + ), |
| 32 | + }, |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +// IntersectionObserver mock |
| 37 | +beforeEach(() => { |
| 38 | + const mockIntersectionObserver = vi.fn(); |
| 39 | + mockIntersectionObserver.mockReturnValue({ |
| 40 | + observe: () => null, |
| 41 | + unobserve: () => null, |
| 42 | + disconnect: () => null, |
| 43 | + }); |
| 44 | + window.IntersectionObserver = |
| 45 | + mockIntersectionObserver as unknown as typeof window.IntersectionObserver; |
| 46 | +}); |
| 47 | + |
| 48 | +describe('Leaderboard - Responsive Breakpoints & Mobile Layouts (Issue #2759 Equivalent)', () => { |
| 49 | + const mockData: Contributor[] = [ |
| 50 | + { id: 1, login: 'gold_user', avatar_url: '', html_url: '', contributions: 100 }, |
| 51 | + { id: 2, login: 'silver_user', avatar_url: '', html_url: '', contributions: 90 }, |
| 52 | + { id: 3, login: 'bronze_user', avatar_url: '', html_url: '', contributions: 80 }, |
| 53 | + { id: 4, login: 'fourth_user', avatar_url: '', html_url: '', contributions: 70 }, |
| 54 | + ]; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + vi.clearAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('Mobile Viewport Mock Coordinates (Viewport Coordinates Equivalent): simulates a 375px window size without crashing', () => { |
| 61 | + // Mock mobile viewport width |
| 62 | + Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 375 }); |
| 63 | + window.dispatchEvent(new Event('resize')); |
| 64 | + |
| 65 | + const { container } = render(<Leaderboard contributors={mockData} />); |
| 66 | + expect(container).toBeTruthy(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('Mobile Padding Zones (Horizontal Scrollbars Equivalent): restricts absolute widths by using fluid responsive paddings', () => { |
| 70 | + const { container } = render(<Leaderboard contributors={mockData} />); |
| 71 | + const mainWrapper = container.firstChild as HTMLElement; |
| 72 | + |
| 73 | + // Verify presence of fluid responsive padding classes rather than fixed widths |
| 74 | + expect(mainWrapper.className).toContain('p-8'); |
| 75 | + expect(mainWrapper.className).toContain('sm:p-12'); |
| 76 | + expect(mainWrapper.className).toContain('w-full'); // Ensures it never forces a horizontal scrollbar |
| 77 | + }); |
| 78 | + |
| 79 | + it('Mobile Column Reflows (Vertical Flex Lists Equivalent): ensures podium items scale down gracefully to fit mobile columns', () => { |
| 80 | + const { container } = render(<Leaderboard contributors={mockData} />); |
| 81 | + |
| 82 | + // Podium wrapper responsiveness |
| 83 | + const podiumWrapper = container.querySelector('.h-\\[300px\\].sm\\:h-\\[360px\\]'); |
| 84 | + expect(podiumWrapper).toBeTruthy(); |
| 85 | + |
| 86 | + // Individual podium item responsiveness |
| 87 | + const podiumItems = container.querySelectorAll('.w-28.sm\\:w-36'); |
| 88 | + expect(podiumItems.length).toBe(3); // Top 3 podium items |
| 89 | + }); |
| 90 | + |
| 91 | + it('Mobile-Specific Toggles (Toggle States Equivalent): hides non-essential labels like "commits" on small screens', () => { |
| 92 | + const { container } = render(<Leaderboard contributors={mockData} />); |
| 93 | + |
| 94 | + const listEntries = container.querySelectorAll('.hidden.sm\\:inline'); |
| 95 | + expect(listEntries.length).toBeGreaterThan(0); |
| 96 | + |
| 97 | + // Check that the text "commits" is targeted for hiding |
| 98 | + expect(listEntries[0].textContent).toBe('commits'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('Mobile Touch Target Scaling (Navigation Scaling Equivalent): preserves large click zones for touch devices even on mobile', () => { |
| 102 | + const scrollIntoViewMock = vi.fn(); |
| 103 | + document.getElementById = vi.fn().mockReturnValue({ |
| 104 | + scrollIntoView: scrollIntoViewMock, |
| 105 | + }); |
| 106 | + |
| 107 | + const { container } = render(<Leaderboard contributors={mockData} />); |
| 108 | + |
| 109 | + // Grab a list item |
| 110 | + const listItem = container.querySelector('.flex.items-center.justify-between.p-4'); |
| 111 | + expect(listItem).toBeTruthy(); // Large padding `p-4` maintained for touch targets |
| 112 | + |
| 113 | + fireEvent.click(listItem as Element); |
| 114 | + |
| 115 | + // Verify the simulated 'navigation' click still functions |
| 116 | + expect(document.getElementById).toHaveBeenCalledWith('contributors'); |
| 117 | + expect(scrollIntoViewMock).toHaveBeenCalledWith({ behavior: 'smooth' }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments