|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import ProfileOptimizerModal from './ProfileOptimizerModal'; |
| 4 | +import type { ReactNode, HTMLAttributes } from 'react'; |
| 5 | +import '@testing-library/jest-dom'; |
| 6 | + |
| 7 | +vi.mock('framer-motion', () => ({ |
| 8 | + AnimatePresence: ({ children }: { children?: ReactNode }) => <>{children}</>, |
| 9 | + |
| 10 | + motion: { |
| 11 | + div: ({ |
| 12 | + children, |
| 13 | + ...props |
| 14 | + }: HTMLAttributes<HTMLDivElement> & { |
| 15 | + children?: ReactNode; |
| 16 | + }) => <div {...props}>{children}</div>, |
| 17 | + |
| 18 | + p: ({ |
| 19 | + children, |
| 20 | + ...props |
| 21 | + }: HTMLAttributes<HTMLParagraphElement> & { |
| 22 | + children?: ReactNode; |
| 23 | + }) => <p {...props}>{children}</p>, |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +const mockUserData = { |
| 28 | + profile: { |
| 29 | + developerScore: 75, |
| 30 | + bio: 'Full Stack Developer', |
| 31 | + stats: { |
| 32 | + repositories: 12, |
| 33 | + followers: 20, |
| 34 | + }, |
| 35 | + }, |
| 36 | + languages: ['TypeScript', 'JavaScript'], |
| 37 | + stats: { |
| 38 | + totalContributions: 500, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +describe('ProfileOptimizerModal', () => { |
| 43 | + const onClose = vi.fn(); |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + vi.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not render when isOpen is false', () => { |
| 50 | + render(<ProfileOptimizerModal isOpen={false} onClose={onClose} userData={mockUserData} />); |
| 51 | + |
| 52 | + expect(screen.queryByText('Profile Optimizer')).not.toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('renders modal when isOpen is true', () => { |
| 56 | + render(<ProfileOptimizerModal isOpen onClose={onClose} userData={mockUserData} />); |
| 57 | + |
| 58 | + expect(screen.getByText('Profile Optimizer')).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('calls onClose when close button is clicked', () => { |
| 62 | + render(<ProfileOptimizerModal isOpen onClose={onClose} userData={mockUserData} />); |
| 63 | + |
| 64 | + const buttons = screen.getAllByRole('button'); |
| 65 | + fireEvent.click(buttons[0]); |
| 66 | + |
| 67 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 68 | + }); |
| 69 | + |
| 70 | + it('shows loading state initially', () => { |
| 71 | + render(<ProfileOptimizerModal isOpen onClose={onClose} userData={mockUserData} />); |
| 72 | + |
| 73 | + expect(screen.getByText('Analysing GitHub profile...')).toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('renders loading container when modal opens', () => { |
| 77 | + render(<ProfileOptimizerModal isOpen onClose={onClose} userData={mockUserData} />); |
| 78 | + |
| 79 | + expect(screen.getByText('Analysing GitHub profile...')).toBeInTheDocument(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments