|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom/vitest'; |
| 3 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 4 | +import { FeatureCard, FeatureCardsSection } from './FeatureCards'; |
| 5 | + |
| 6 | +// Mock GSAP |
| 7 | +const mockTimeline = { |
| 8 | + to: vi.fn().mockReturnThis(), |
| 9 | + fromTo: vi.fn().mockReturnThis(), |
| 10 | + set: vi.fn().mockReturnThis(), |
| 11 | + kill: vi.fn(), |
| 12 | +}; |
| 13 | + |
| 14 | +vi.mock('gsap', () => { |
| 15 | + const gsapMock = { |
| 16 | + registerPlugin: vi.fn(), |
| 17 | + context: (cb: () => void) => { |
| 18 | + cb(); |
| 19 | + return { revert: vi.fn() }; |
| 20 | + }, |
| 21 | + timeline: () => mockTimeline, |
| 22 | + to: vi.fn().mockImplementation((target, vars) => { |
| 23 | + if (typeof vars.onComplete === 'function') { |
| 24 | + vars.onComplete(); |
| 25 | + } |
| 26 | + return mockTimeline; |
| 27 | + }), |
| 28 | + fromTo: vi.fn().mockReturnThis(), |
| 29 | + set: vi.fn().mockReturnThis(), |
| 30 | + }; |
| 31 | + return { |
| 32 | + default: gsapMock, |
| 33 | + ...gsapMock, |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +vi.mock('gsap/ScrollTrigger', () => ({ |
| 38 | + ScrollTrigger: {}, |
| 39 | +})); |
| 40 | + |
| 41 | +describe('FeatureCards Component Suite', () => { |
| 42 | + beforeEach(() => { |
| 43 | + vi.clearAllMocks(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('FeatureCard Component', () => { |
| 47 | + const defaultProps = { |
| 48 | + icon: <span data-testid="test-icon">💡</span>, |
| 49 | + title: 'Amazing Visuals', |
| 50 | + desc: 'Beautiful SVG isometric rendering of your commits.', |
| 51 | + accent: 'text-emerald-500', |
| 52 | + index: 1, |
| 53 | + accentColor: '#10b981', |
| 54 | + }; |
| 55 | + |
| 56 | + it('renders card title, description and icon correctly', () => { |
| 57 | + render(<FeatureCard {...defaultProps} />); |
| 58 | + |
| 59 | + expect(screen.getByText('Amazing Visuals')).toBeInTheDocument(); |
| 60 | + expect( |
| 61 | + screen.getByText('Beautiful SVG isometric rendering of your commits.') |
| 62 | + ).toBeInTheDocument(); |
| 63 | + expect(screen.getByTestId('test-icon')).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('renders background gradient blob and accent line matching the accentColor', () => { |
| 67 | + const { container } = render(<FeatureCard {...defaultProps} />); |
| 68 | + |
| 69 | + // The background blob has class "absolute -right-20 -top-20" |
| 70 | + const blob = container.querySelector('.absolute.-right-20.-top-20') as HTMLElement; |
| 71 | + expect(blob).toBeInTheDocument(); |
| 72 | + expect(blob.style.background).toBeTruthy(); |
| 73 | + |
| 74 | + // The bottom accent line has class "absolute bottom-0 left-0" |
| 75 | + const accentLine = container.querySelector('.absolute.bottom-0.left-0') as HTMLElement; |
| 76 | + expect(accentLine).toBeInTheDocument(); |
| 77 | + expect(accentLine.style.background).toMatch(/rgb\(16, 185, 129\)|#10b981/); |
| 78 | + }); |
| 79 | + |
| 80 | + it('triggers mouse movement and hover hover-effects to activate magnetic spotlight', () => { |
| 81 | + const { container } = render(<FeatureCard {...defaultProps} />); |
| 82 | + const card = container.firstChild as HTMLDivElement; |
| 83 | + |
| 84 | + // Mouse enter triggers hover state |
| 85 | + fireEvent.mouseEnter(card); |
| 86 | + |
| 87 | + // Mouse move triggers magnetic movement recalculations |
| 88 | + fireEvent.mouseMove(card, { clientX: 100, clientY: 100 }); |
| 89 | + |
| 90 | + // Mouse leave resets magnetic coordinates |
| 91 | + fireEvent.mouseLeave(card); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('FeatureCardsSection Wrapper Component', () => { |
| 96 | + it('renders children components and Why CommitPulse heading correctly', () => { |
| 97 | + render( |
| 98 | + <FeatureCardsSection> |
| 99 | + <div data-testid="child-card">Child Component</div> |
| 100 | + </FeatureCardsSection> |
| 101 | + ); |
| 102 | + |
| 103 | + expect(screen.getByRole('heading', { name: 'Why CommitPulse?' })).toBeInTheDocument(); |
| 104 | + expect(screen.getByTestId('child-card')).toBeInTheDocument(); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments