|
| 1 | +import { fireEvent, screen } from '@testing-library/react' |
| 2 | +import { useRouter } from 'next/navigation' |
| 3 | +import React from 'react' |
| 4 | +import { render } from 'wrappers/testUtil' |
| 5 | +import type { Organization } from 'types/organization' |
| 6 | +import type { RepositoryCardProps } from 'types/project' |
| 7 | +import RepositoriesCard from 'components/RepositoriesCard' |
| 8 | + |
| 9 | +jest.mock('next/navigation', () => ({ |
| 10 | + useRouter: jest.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +jest.mock('components/ShowMoreButton', () => { |
| 14 | + return function MockShowMoreButton({ onToggle }: { onToggle: () => void }) { |
| 15 | + const [isExpanded, setIsExpanded] = React.useState(false) |
| 16 | + |
| 17 | + const handleToggle = () => { |
| 18 | + setIsExpanded(!isExpanded) |
| 19 | + onToggle() |
| 20 | + } |
| 21 | + |
| 22 | + return ( |
| 23 | + <button type="button" onClick={handleToggle} data-testid="show-more-button"> |
| 24 | + {isExpanded ? 'Show less' : 'Show more'} |
| 25 | + </button> |
| 26 | + ) |
| 27 | + } |
| 28 | +}) |
| 29 | + |
| 30 | +jest.mock('components/TruncatedText', () => ({ |
| 31 | + TruncatedText: ({ text }: { text: string }) => <span>{text}</span>, |
| 32 | +})) |
| 33 | + |
| 34 | +jest.mock('components/InfoItem', () => { |
| 35 | + return function MockInfoItem({ unit, value }: { unit: string; value: number }) { |
| 36 | + return <div data-testid={`info-item-${unit}`}>{value}</div> |
| 37 | + } |
| 38 | +}) |
| 39 | + |
| 40 | +const mockPush = jest.fn() |
| 41 | +const mockUseRouter = useRouter as jest.Mock |
| 42 | + |
| 43 | +describe('RepositoriesCard', () => { |
| 44 | + beforeEach(() => { |
| 45 | + jest.clearAllMocks() |
| 46 | + mockUseRouter.mockReturnValue({ |
| 47 | + push: mockPush, |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + const createMockRepository = (index: number): RepositoryCardProps => ({ |
| 52 | + contributorsCount: 10 + index, |
| 53 | + forksCount: 5 + index, |
| 54 | + key: `repo-${index}`, |
| 55 | + name: `Repository ${index}`, |
| 56 | + openIssuesCount: 3 + index, |
| 57 | + organization: { |
| 58 | + login: `org-${index}`, |
| 59 | + name: `Organization ${index}`, |
| 60 | + key: `org-${index}`, |
| 61 | + url: `https://github.com/org-${index}`, |
| 62 | + avatarUrl: `https://github.com/org-${index}.png`, |
| 63 | + description: `Organization ${index} description`, |
| 64 | + objectID: `org-${index}`, |
| 65 | + collaboratorsCount: 10, |
| 66 | + followersCount: 50, |
| 67 | + publicRepositoriesCount: 20, |
| 68 | + createdAt: Date.now(), |
| 69 | + updatedAt: Date.now(), |
| 70 | + } as Organization, |
| 71 | + starsCount: 100 + index, |
| 72 | + subscribersCount: 20 + index, |
| 73 | + url: `https://github.com/org-${index}/repo-${index}`, |
| 74 | + }) |
| 75 | + |
| 76 | + it('renders without crashing with empty repositories', () => { |
| 77 | + render(<RepositoriesCard repositories={[]} />) |
| 78 | + expect(screen.queryByTestId('show-more-button')).not.toBeInTheDocument() |
| 79 | + }) |
| 80 | + |
| 81 | + it('shows first 4 repositories initially when there are more than 4', () => { |
| 82 | + const repositories = Array.from({ length: 6 }, (_, i) => createMockRepository(i)) |
| 83 | + |
| 84 | + render(<RepositoriesCard repositories={repositories} />) |
| 85 | + |
| 86 | + expect(screen.getByText('Repository 0')).toBeInTheDocument() |
| 87 | + expect(screen.getByText('Repository 3')).toBeInTheDocument() |
| 88 | + expect(screen.queryByText('Repository 4')).not.toBeInTheDocument() |
| 89 | + expect(screen.queryByText('Repository 5')).not.toBeInTheDocument() |
| 90 | + }) |
| 91 | + |
| 92 | + it('shows all repositories when there are 4 or fewer', () => { |
| 93 | + const repositories = Array.from({ length: 3 }, (_, i) => createMockRepository(i)) |
| 94 | + |
| 95 | + render(<RepositoriesCard repositories={repositories} />) |
| 96 | + |
| 97 | + expect(screen.getByText('Repository 0')).toBeInTheDocument() |
| 98 | + expect(screen.getByText('Repository 1')).toBeInTheDocument() |
| 99 | + expect(screen.getByText('Repository 2')).toBeInTheDocument() |
| 100 | + }) |
| 101 | + |
| 102 | + it('displays ShowMoreButton when there are more than 4 repositories', () => { |
| 103 | + const repositories = Array.from({ length: 6 }, (_, i) => createMockRepository(i)) |
| 104 | + |
| 105 | + render(<RepositoriesCard repositories={repositories} />) |
| 106 | + |
| 107 | + expect(screen.getByTestId('show-more-button')).toBeInTheDocument() |
| 108 | + }) |
| 109 | + |
| 110 | + it('does not display ShowMoreButton when there are 4 or fewer repositories', () => { |
| 111 | + const repositories = Array.from({ length: 4 }, (_, i) => createMockRepository(i)) |
| 112 | + |
| 113 | + render(<RepositoriesCard repositories={repositories} />) |
| 114 | + |
| 115 | + expect(screen.queryByTestId('show-more-button')).not.toBeInTheDocument() |
| 116 | + }) |
| 117 | + |
| 118 | + it('toggles between showing 4 and all repositories when clicked', () => { |
| 119 | + const repositories = Array.from({ length: 6 }, (_, i) => createMockRepository(i)) |
| 120 | + |
| 121 | + render(<RepositoriesCard repositories={repositories} />) |
| 122 | + |
| 123 | + // Initially shows first 4 |
| 124 | + expect(screen.getByText('Repository 0')).toBeInTheDocument() |
| 125 | + expect(screen.queryByText('Repository 4')).not.toBeInTheDocument() |
| 126 | + |
| 127 | + // Click show more |
| 128 | + fireEvent.click(screen.getByTestId('show-more-button')) |
| 129 | + |
| 130 | + // Now shows all repositories |
| 131 | + expect(screen.getByText('Repository 4')).toBeInTheDocument() |
| 132 | + expect(screen.getByText('Repository 5')).toBeInTheDocument() |
| 133 | + |
| 134 | + // Click show less |
| 135 | + fireEvent.click(screen.getByTestId('show-more-button')) |
| 136 | + |
| 137 | + // Back to showing first 4 |
| 138 | + expect(screen.queryByText('Repository 4')).not.toBeInTheDocument() |
| 139 | + expect(screen.queryByText('Repository 5')).not.toBeInTheDocument() |
| 140 | + }) |
| 141 | + |
| 142 | + it('renders repository items with correct information', () => { |
| 143 | + const repositories = [createMockRepository(0)] |
| 144 | + |
| 145 | + render(<RepositoriesCard repositories={repositories} />) |
| 146 | + |
| 147 | + expect(screen.getByText('Repository 0')).toBeInTheDocument() |
| 148 | + expect(screen.getByTestId('info-item-Star')).toBeInTheDocument() |
| 149 | + expect(screen.getByTestId('info-item-Fork')).toBeInTheDocument() |
| 150 | + expect(screen.getByTestId('info-item-Contributor')).toBeInTheDocument() |
| 151 | + expect(screen.getByTestId('info-item-Issue')).toBeInTheDocument() |
| 152 | + }) |
| 153 | + |
| 154 | + it('navigates to correct URL when repository item is clicked', () => { |
| 155 | + const repositories = [createMockRepository(0)] |
| 156 | + |
| 157 | + render(<RepositoriesCard repositories={repositories} />) |
| 158 | + |
| 159 | + const repositoryButton = screen.getByText('Repository 0') |
| 160 | + fireEvent.click(repositoryButton) |
| 161 | + |
| 162 | + expect(mockPush).toHaveBeenCalledWith('/organizations/org-0/repositories/repo-0') |
| 163 | + }) |
| 164 | + |
| 165 | + it('handles repositories without organization data gracefully', () => { |
| 166 | + const repository: RepositoryCardProps = { |
| 167 | + contributorsCount: 10, |
| 168 | + forksCount: 5, |
| 169 | + key: 'repo-test', |
| 170 | + name: 'Test Repository', |
| 171 | + openIssuesCount: 3, |
| 172 | + starsCount: 100, |
| 173 | + subscribersCount: 20, |
| 174 | + url: 'https://github.com/test/repo', |
| 175 | + } |
| 176 | + |
| 177 | + expect(() => render(<RepositoriesCard repositories={[repository]} />)).not.toThrow() |
| 178 | + }) |
| 179 | +}) |
0 commit comments