|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react' |
| 2 | +import { useRouter } from 'next/navigation' |
| 3 | +import React from 'react' |
| 4 | +import type { Issue } from 'types/issue' |
| 5 | +import RecentIssues from 'components/RecentIssues' |
| 6 | + |
| 7 | +jest.mock('next/navigation', () => ({ |
| 8 | + useRouter: jest.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +jest.mock('@heroui/tooltip', () => ({ |
| 12 | + Tooltip: ({ |
| 13 | + children, |
| 14 | + content, |
| 15 | + id, |
| 16 | + }: { |
| 17 | + children: React.ReactNode |
| 18 | + content: string |
| 19 | + id: string |
| 20 | + }) => ( |
| 21 | + <div data-testid={id} title={content}> |
| 22 | + {children} |
| 23 | + </div> |
| 24 | + ), |
| 25 | +})) |
| 26 | + |
| 27 | +jest.mock('next/image', () => ({ |
| 28 | + __esModule: true, |
| 29 | + default: ({ |
| 30 | + src, |
| 31 | + alt, |
| 32 | + fill, |
| 33 | + objectFit, |
| 34 | + ...props |
| 35 | + }: { |
| 36 | + src: string |
| 37 | + alt: string |
| 38 | + fill?: boolean |
| 39 | + objectFit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down' |
| 40 | + [key: string]: unknown |
| 41 | + }) => ( |
| 42 | + // eslint-disable-next-line @next/next/no-img-element |
| 43 | + <img |
| 44 | + src={src} |
| 45 | + alt={alt} |
| 46 | + style={fill && { objectFit: objectFit as React.CSSProperties['objectFit'] }} |
| 47 | + {...props} |
| 48 | + /> |
| 49 | + ), |
| 50 | +})) |
| 51 | + |
| 52 | +const mockPush = jest.fn() |
| 53 | +beforeEach(() => { |
| 54 | + ;(useRouter as jest.Mock).mockReturnValue({ push: mockPush }) |
| 55 | + mockPush.mockClear() |
| 56 | +}) |
| 57 | + |
| 58 | +const baseIssue = { |
| 59 | + author: { |
| 60 | + avatarUrl: 'https://example.com/avatar.png', |
| 61 | + login: 'user1', |
| 62 | + name: 'User One', |
| 63 | + contributionsCount: 10, |
| 64 | + createdAt: 1234567890, |
| 65 | + followersCount: 5, |
| 66 | + followingCount: 2, |
| 67 | + key: 'user1', |
| 68 | + publicRepositoriesCount: 3, |
| 69 | + url: 'https://github.com/user1', |
| 70 | + }, |
| 71 | + createdAt: 1710000000, |
| 72 | + hint: 'Hint', |
| 73 | + labels: ['bug'], |
| 74 | + organizationName: 'org', |
| 75 | + projectName: 'proj', |
| 76 | + projectUrl: 'https://github.com/org/proj', |
| 77 | + summary: 'Summary', |
| 78 | + title: 'Issue Title', |
| 79 | + updatedAt: 1710000100, |
| 80 | + url: 'https://github.com/org/proj/issues/1', |
| 81 | + objectID: 'id1', |
| 82 | + repositoryName: 'repo', |
| 83 | +} |
| 84 | + |
| 85 | +describe('<RecentIssues />', () => { |
| 86 | + it('renders successfully with minimal required props', () => { |
| 87 | + render(<RecentIssues data={[baseIssue]} />) |
| 88 | + expect(screen.getByText('Recent Issues')).toBeInTheDocument() |
| 89 | + expect(screen.getByText('Issue Title')).toBeInTheDocument() |
| 90 | + }) |
| 91 | + |
| 92 | + it('renders "Nothing to display." when data is empty', () => { |
| 93 | + render(<RecentIssues data={[]} />) |
| 94 | + expect(screen.getByText('Nothing to display.')).toBeInTheDocument() |
| 95 | + }) |
| 96 | + |
| 97 | + it('shows avatar when showAvatar is true', () => { |
| 98 | + render(<RecentIssues data={[baseIssue]} showAvatar={true} />) |
| 99 | + expect(screen.getByAltText('User One')).toBeInTheDocument() |
| 100 | + }) |
| 101 | + |
| 102 | + it('hides avatar when showAvatar is false', () => { |
| 103 | + render(<RecentIssues data={[baseIssue]} showAvatar={false} />) |
| 104 | + expect(screen.queryByAltText('User One')).not.toBeInTheDocument() |
| 105 | + }) |
| 106 | + |
| 107 | + it('renders repositoryName and navigates on click', () => { |
| 108 | + render(<RecentIssues data={[baseIssue]} />) |
| 109 | + const repoBtn = screen.getByText('repo') |
| 110 | + expect(repoBtn).toBeInTheDocument() |
| 111 | + fireEvent.click(repoBtn) |
| 112 | + expect(mockPush).toHaveBeenCalledWith('/organizations/org/repositories/repo') |
| 113 | + }) |
| 114 | + |
| 115 | + it('does not render repositoryName button if missing', () => { |
| 116 | + const issue = { ...baseIssue } |
| 117 | + delete issue.repositoryName |
| 118 | + render(<RecentIssues data={[issue]} />) |
| 119 | + expect(screen.queryByText('repo')).not.toBeInTheDocument() |
| 120 | + }) |
| 121 | + |
| 122 | + it('renders formatted date', () => { |
| 123 | + render(<RecentIssues data={[baseIssue]} />) |
| 124 | + expect(screen.getByText(/Mar \d{1,2}, 2024/)).toBeInTheDocument() |
| 125 | + }) |
| 126 | + |
| 127 | + it('renders label text', () => { |
| 128 | + render(<RecentIssues data={[baseIssue]} />) |
| 129 | + expect(screen.getByText('Issue Title')).toBeInTheDocument() |
| 130 | + }) |
| 131 | + |
| 132 | + it('handles edge case: missing author', () => { |
| 133 | + const issue: Issue = { ...baseIssue, author: undefined } |
| 134 | + render(<RecentIssues data={[issue]} />) |
| 135 | + expect(screen.getByText('Issue Title')).toBeInTheDocument() |
| 136 | + }) |
| 137 | + |
| 138 | + it('handles edge case: missing title', () => { |
| 139 | + const issue: Issue = { ...baseIssue, title: undefined } |
| 140 | + render(<RecentIssues data={[issue]} />) |
| 141 | + expect(screen.getByText('Recent Issues')).toBeInTheDocument() |
| 142 | + expect(screen.getByText('repo')).toBeInTheDocument() |
| 143 | + expect(screen.getByAltText('User One')).toBeInTheDocument() |
| 144 | + }) |
| 145 | + |
| 146 | + it('has accessible roles and labels', () => { |
| 147 | + render(<RecentIssues data={[baseIssue]} />) |
| 148 | + expect(screen.getByRole('heading', { name: /Recent Issues/i })).toBeInTheDocument() |
| 149 | + }) |
| 150 | + |
| 151 | + it('applies correct DOM structure and classNames', () => { |
| 152 | + render(<RecentIssues data={[baseIssue]} />) |
| 153 | + expect(screen.getByText('Recent Issues').closest('div')).toHaveClass('flex') |
| 154 | + }) |
| 155 | + |
| 156 | + it('renders multiple issues', () => { |
| 157 | + const issues = [baseIssue, { ...baseIssue, objectID: 'id2', title: 'Second Issue' }] |
| 158 | + render(<RecentIssues data={issues} />) |
| 159 | + expect(screen.getByText('Second Issue')).toBeInTheDocument() |
| 160 | + expect(screen.getAllByText(/Issue Title|Second Issue/).length).toBeGreaterThan(1) |
| 161 | + }) |
| 162 | + |
| 163 | + it('renders with long repositoryName and truncates', () => { |
| 164 | + const issue = { ...baseIssue, repositoryName: 'a'.repeat(100) } |
| 165 | + render(<RecentIssues data={[issue]} />) |
| 166 | + expect(screen.getByText('a'.repeat(100))).toBeInTheDocument() |
| 167 | + }) |
| 168 | + |
| 169 | + it('renders with custom organizationName', () => { |
| 170 | + const issue = { ...baseIssue, organizationName: 'custom-org' } |
| 171 | + render(<RecentIssues data={[issue]} />) |
| 172 | + fireEvent.click(screen.getByText('repo')) |
| 173 | + expect(mockPush).toHaveBeenCalledWith('/organizations/custom-org/repositories/repo') |
| 174 | + }) |
| 175 | + |
| 176 | + it('renders with missing props gracefully', () => { |
| 177 | + render(<RecentIssues data={[{} as Issue]} showAvatar={false} />) |
| 178 | + expect(screen.getByText('Recent Issues')).toBeInTheDocument() |
| 179 | + }) |
| 180 | + |
| 181 | + it('renders with null data', () => { |
| 182 | + render(<RecentIssues data={null as unknown as Issue[]} />) |
| 183 | + expect(screen.getByText('Nothing to display.')).toBeInTheDocument() |
| 184 | + }) |
| 185 | + |
| 186 | + it('defaults to showing avatar when showAvatar is not provided', () => { |
| 187 | + render(<RecentIssues data={[baseIssue]} />) |
| 188 | + expect(screen.getByAltText('User One')).toBeInTheDocument() |
| 189 | + }) |
| 190 | +}) |
0 commit comments