|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import ResumeProfileSection from './ResumeProfileSection'; |
| 4 | +import type { ReactNode, HTMLAttributes } from 'react'; |
| 5 | +import '@testing-library/jest-dom'; |
| 6 | + |
| 7 | +const { mockToastError, mockToastSuccess } = vi.hoisted(() => ({ |
| 8 | + mockToastError: vi.fn(), |
| 9 | + mockToastSuccess: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('sonner', () => ({ |
| 13 | + toast: { |
| 14 | + error: mockToastError, |
| 15 | + success: mockToastSuccess, |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('framer-motion', () => ({ |
| 20 | + motion: { |
| 21 | + div: ({ |
| 22 | + children, |
| 23 | + ...props |
| 24 | + }: HTMLAttributes<HTMLDivElement> & { |
| 25 | + children?: ReactNode; |
| 26 | + }) => <div {...props}>{children}</div>, |
| 27 | + }, |
| 28 | + AnimatePresence: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 29 | +})); |
| 30 | + |
| 31 | +const parsedResume = { |
| 32 | + name: 'John Doe', |
| 33 | + email: 'john@example.com', |
| 34 | + phone: '1234567890', |
| 35 | + skills: ['React'], |
| 36 | + education: [], |
| 37 | + experience: [], |
| 38 | +}; |
| 39 | + |
| 40 | +vi.mock('./ResumeUpload', () => ({ |
| 41 | + default: ({ |
| 42 | + onParsed, |
| 43 | + onError, |
| 44 | + }: { |
| 45 | + onParsed: (data: unknown, name: string) => void; |
| 46 | + onError: (error: string) => void; |
| 47 | + }) => ( |
| 48 | + <div> |
| 49 | + <button onClick={() => onParsed(parsedResume, 'resume.pdf')}>Mock Upload Success</button> |
| 50 | + |
| 51 | + <button onClick={() => onError('Upload failed')}>Mock Upload Error</button> |
| 52 | + </div> |
| 53 | + ), |
| 54 | +})); |
| 55 | + |
| 56 | +vi.mock('./ResumePreviewForm', () => ({ |
| 57 | + default: ({ onBack, onComplete }: { onBack: () => void; onComplete: () => void }) => ( |
| 58 | + <div> |
| 59 | + <p>Preview Form</p> |
| 60 | + |
| 61 | + <button onClick={onBack}>Back</button> |
| 62 | + |
| 63 | + <button onClick={onComplete}>Complete</button> |
| 64 | + </div> |
| 65 | + ), |
| 66 | +})); |
| 67 | + |
| 68 | +describe('ResumeProfileSection', () => { |
| 69 | + beforeEach(() => { |
| 70 | + vi.clearAllMocks(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('renders upload state initially', () => { |
| 74 | + render(<ResumeProfileSection githubUsername="john" />); |
| 75 | + |
| 76 | + expect(screen.getByText('Resume Profile')).toBeInTheDocument(); |
| 77 | + |
| 78 | + expect(screen.getByText(/upload your pdf or docx resume/i)).toBeInTheDocument(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('shows preview form after successful parse', () => { |
| 82 | + render(<ResumeProfileSection githubUsername="john" />); |
| 83 | + |
| 84 | + fireEvent.click(screen.getByText('Mock Upload Success')); |
| 85 | + |
| 86 | + expect(screen.getByText('Preview Form')).toBeInTheDocument(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('returns to upload state when back is clicked', () => { |
| 90 | + render(<ResumeProfileSection githubUsername="john" />); |
| 91 | + |
| 92 | + fireEvent.click(screen.getByText('Mock Upload Success')); |
| 93 | + |
| 94 | + fireEvent.click(screen.getByText('Back')); |
| 95 | + |
| 96 | + expect(screen.getByText(/upload your pdf or docx resume/i)).toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('shows success state after completion', () => { |
| 100 | + render(<ResumeProfileSection githubUsername="john" />); |
| 101 | + |
| 102 | + fireEvent.click(screen.getByText('Mock Upload Success')); |
| 103 | + |
| 104 | + fireEvent.click(screen.getByText('Complete')); |
| 105 | + |
| 106 | + expect(screen.getByText('Profile synced from resume')).toBeInTheDocument(); |
| 107 | + |
| 108 | + expect(mockToastSuccess).toHaveBeenCalledWith('Profile updated from resume!'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('shows error toast when upload fails', () => { |
| 112 | + render(<ResumeProfileSection githubUsername="john" />); |
| 113 | + |
| 114 | + fireEvent.click(screen.getByText('Mock Upload Error')); |
| 115 | + |
| 116 | + expect(mockToastError).toHaveBeenCalledWith('Upload failed'); |
| 117 | + }); |
| 118 | + it('renders component', () => { |
| 119 | + expect(true).toBe(true); |
| 120 | + }); |
| 121 | +}); |
0 commit comments