|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import DownloadReportMenu from './DownloadReportMenu'; |
| 4 | +import type { BurnoutReport } from '@/services/github/burnout-analyzer'; |
| 5 | + |
| 6 | +vi.mock('@/utils/clipboard', () => ({ |
| 7 | + copyToClipboard: vi.fn().mockResolvedValue(undefined), |
| 8 | +})); |
| 9 | + |
| 10 | +const mockReport: BurnoutReport = { |
| 11 | + repoName: 'facebook/react', |
| 12 | + totalCommits: 1500, |
| 13 | + totalContributors: 45, |
| 14 | + busFactor: 2, |
| 15 | + dependencyRisk: 'Medium', |
| 16 | + sustainabilityScore: 78, |
| 17 | + contributors: [ |
| 18 | + { |
| 19 | + username: 'dan', |
| 20 | + avatarUrl: 'https://github.com/dan.png', |
| 21 | + totalCommits: 600, |
| 22 | + commitShare: 40, |
| 23 | + burnoutScore: 75, |
| 24 | + riskLevel: 'High', |
| 25 | + activeWeeks: 10, |
| 26 | + highIntensityWeeks: 4, |
| 27 | + consecutiveHighWeeks: 2, |
| 28 | + restWeeks: 2, |
| 29 | + recentTrend: [10, 15, 20], |
| 30 | + recentAdditionsTrend: [100, 200], |
| 31 | + }, |
| 32 | + { |
| 33 | + username: 'sophie', |
| 34 | + avatarUrl: 'https://github.com/sophie.png', |
| 35 | + totalCommits: 400, |
| 36 | + commitShare: 26.6, |
| 37 | + burnoutScore: 50, |
| 38 | + riskLevel: 'Medium', |
| 39 | + activeWeeks: 8, |
| 40 | + highIntensityWeeks: 2, |
| 41 | + consecutiveHighWeeks: 1, |
| 42 | + restWeeks: 4, |
| 43 | + recentTrend: [5, 10, 8], |
| 44 | + recentAdditionsTrend: [50, 100], |
| 45 | + }, |
| 46 | + ], |
| 47 | + inactivityAlerts: [ |
| 48 | + { |
| 49 | + username: 'gaearon', |
| 50 | + avatarUrl: 'https://github.com/gaearon.png', |
| 51 | + previousAvgWeeklyCommits: 8, |
| 52 | + weeksSilent: 4, |
| 53 | + severity: 'High', |
| 54 | + }, |
| 55 | + ], |
| 56 | + recommendations: ['[AI Recommendation] Onboard more maintainers to lower bus factor risk.'], |
| 57 | +}; |
| 58 | + |
| 59 | +describe('DownloadReportMenu Export & Download Functionality', () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('renders Export Report button', () => { |
| 65 | + render(<DownloadReportMenu report={mockReport} />); |
| 66 | + expect(screen.getByRole('button', { name: /export/i })).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('opens dropdown menu with all required export choices when clicked', () => { |
| 70 | + render(<DownloadReportMenu report={mockReport} />); |
| 71 | + fireEvent.click(screen.getByRole('button', { name: /export/i })); |
| 72 | + |
| 73 | + expect(screen.getByText('Download as JSON')).toBeInTheDocument(); |
| 74 | + expect(screen.getByText('Export as Markdown')).toBeInTheDocument(); |
| 75 | + expect(screen.getByText('Copy Share Link')).toBeInTheDocument(); |
| 76 | + expect(screen.getByText('Copy Markdown Summary')).toBeInTheDocument(); |
| 77 | + expect(screen.getByText('Download as PDF')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('triggers JSON download when "Download as JSON" is clicked', () => { |
| 81 | + render(<DownloadReportMenu report={mockReport} />); |
| 82 | + fireEvent.click(screen.getByRole('button', { name: /export/i })); |
| 83 | + |
| 84 | + const createObjectURLMock = vi.fn().mockReturnValue('blob:test'); |
| 85 | + const revokeObjectURLMock = vi.fn(); |
| 86 | + vi.stubGlobal('URL', { |
| 87 | + createObjectURL: createObjectURLMock, |
| 88 | + revokeObjectURL: revokeObjectURLMock, |
| 89 | + }); |
| 90 | + |
| 91 | + const anchorClickMock = vi |
| 92 | + .spyOn(HTMLAnchorElement.prototype, 'click') |
| 93 | + .mockImplementation(() => {}); |
| 94 | + |
| 95 | + fireEvent.click(screen.getByText('Download as JSON')); |
| 96 | + |
| 97 | + expect(createObjectURLMock).toHaveBeenCalled(); |
| 98 | + expect(anchorClickMock).toHaveBeenCalled(); |
| 99 | + expect(screen.getByText(/JSON report downloaded!/i)).toBeInTheDocument(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('triggers Markdown download when "Export as Markdown" is clicked', () => { |
| 103 | + render(<DownloadReportMenu report={mockReport} />); |
| 104 | + fireEvent.click(screen.getByRole('button', { name: /export/i })); |
| 105 | + |
| 106 | + const createObjectURLMock = vi.fn().mockReturnValue('blob:test'); |
| 107 | + const revokeObjectURLMock = vi.fn(); |
| 108 | + vi.stubGlobal('URL', { |
| 109 | + createObjectURL: createObjectURLMock, |
| 110 | + revokeObjectURL: revokeObjectURLMock, |
| 111 | + }); |
| 112 | + |
| 113 | + const anchorClickMock = vi |
| 114 | + .spyOn(HTMLAnchorElement.prototype, 'click') |
| 115 | + .mockImplementation(() => {}); |
| 116 | + |
| 117 | + fireEvent.click(screen.getByText('Export as Markdown')); |
| 118 | + |
| 119 | + expect(createObjectURLMock).toHaveBeenCalled(); |
| 120 | + expect(anchorClickMock).toHaveBeenCalled(); |
| 121 | + expect(screen.getByText(/Markdown report downloaded!/i)).toBeInTheDocument(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('copies share link and displays toast notification when "Copy Share Link" is clicked', async () => { |
| 125 | + const { copyToClipboard } = await import('@/utils/clipboard'); |
| 126 | + render(<DownloadReportMenu report={mockReport} />); |
| 127 | + fireEvent.click(screen.getByRole('button', { name: /export/i })); |
| 128 | + |
| 129 | + fireEvent.click(screen.getByText('Copy Share Link')); |
| 130 | + |
| 131 | + await waitFor(() => { |
| 132 | + expect(copyToClipboard).toHaveBeenCalledWith( |
| 133 | + expect.stringContaining('/burnout-analyzer?owner=facebook&repo=react') |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + expect(screen.getByText(/Share link copied to clipboard!/i)).toBeInTheDocument(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('copies summary and displays toast when "Copy Markdown Summary" is clicked', async () => { |
| 141 | + const { copyToClipboard } = await import('@/utils/clipboard'); |
| 142 | + render(<DownloadReportMenu report={mockReport} />); |
| 143 | + fireEvent.click(screen.getByRole('button', { name: /export/i })); |
| 144 | + |
| 145 | + fireEvent.click(screen.getByText('Copy Markdown Summary')); |
| 146 | + |
| 147 | + await waitFor(() => { |
| 148 | + expect(copyToClipboard).toHaveBeenCalledWith( |
| 149 | + expect.stringContaining('Team Health Report: facebook/react') |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + expect(screen.getByText(/Markdown report copied to clipboard!/i)).toBeInTheDocument(); |
| 154 | + }); |
| 155 | +}); |
0 commit comments