|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react' |
| 2 | +import { ReactNode } from 'react' |
| 3 | +import RecentPullRequests from 'components/RecentPullRequests' |
| 4 | + |
| 5 | +jest.mock('@heroui/tooltip', () => ({ |
| 6 | + Tooltip: ({ children }: { children?: ReactNode }) => <div>{children}</div>, |
| 7 | +})) |
| 8 | + |
| 9 | +const mockRouterPush = jest.fn() |
| 10 | +jest.mock('next/navigation', () => ({ |
| 11 | + useRouter: () => ({ |
| 12 | + push: mockRouterPush, |
| 13 | + }), |
| 14 | +})) |
| 15 | + |
| 16 | +jest.mock('components/TruncatedText', () => ({ |
| 17 | + TruncatedText: ({ text }: { text: string }) => <span>{text}</span>, |
| 18 | +})) |
| 19 | + |
| 20 | +jest.mock('utils/dateFormatter', () => ({ |
| 21 | + formatDate: () => 'Jun 1, 2024', |
| 22 | +})) |
| 23 | + |
| 24 | +const mockUser = { |
| 25 | + avatarUrl: 'https://example.com/avatar.png', |
| 26 | + bio: 'Test bio', |
| 27 | + company: 'Test Company', |
| 28 | + contributionsCount: 42, |
| 29 | + createdAt: 1577836800000, |
| 30 | + |
| 31 | + followersCount: 10, |
| 32 | + followingCount: 5, |
| 33 | + key: 'user-key', |
| 34 | + location: 'Test City', |
| 35 | + login: 'testuser', |
| 36 | + name: 'Test User', |
| 37 | + publicRepositoriesCount: 3, |
| 38 | + url: 'https://github.com/testuser', |
| 39 | +} |
| 40 | + |
| 41 | +const minimalData = [ |
| 42 | + { |
| 43 | + author: mockUser, |
| 44 | + createdAt: '2024-06-01T12:00:00Z', |
| 45 | + organizationName: 'test-org', |
| 46 | + repositoryName: 'test-repo', |
| 47 | + title: 'Test Pull Request', |
| 48 | + url: 'https://github.com/test-org/test-repo/pull/1', |
| 49 | + }, |
| 50 | +] |
| 51 | + |
| 52 | +const noRepoData = [ |
| 53 | + { |
| 54 | + author: mockUser, |
| 55 | + createdAt: '2024-06-01T12:00:00Z', |
| 56 | + organizationName: 'test-org', |
| 57 | + repositoryName: undefined, |
| 58 | + title: 'Test Pull Request', |
| 59 | + url: 'https://github.com/test-org/test-repo/pull/2', |
| 60 | + }, |
| 61 | +] |
| 62 | + |
| 63 | +describe('RecentPullRequests', () => { |
| 64 | + afterEach(() => { |
| 65 | + jest.clearAllMocks() |
| 66 | + }) |
| 67 | + |
| 68 | + it('renders successfully with minimal required props', () => { |
| 69 | + render(<RecentPullRequests data={minimalData} />) |
| 70 | + expect(screen.getByText('Recent Pull Requests')).toBeInTheDocument() |
| 71 | + expect(screen.getByText('test-repo')).toBeInTheDocument() |
| 72 | + expect(screen.getByText('Jun 1, 2024')).toBeInTheDocument() |
| 73 | + expect(screen.getByText('Test Pull Request')).toBeInTheDocument() |
| 74 | + }) |
| 75 | + |
| 76 | + it('does not render repository button if repositoryName is missing', () => { |
| 77 | + render(<RecentPullRequests data={noRepoData} />) |
| 78 | + expect(screen.queryByText('test-repo')).not.toBeInTheDocument() |
| 79 | + }) |
| 80 | + |
| 81 | + it('passes showAvatar prop to ItemCardList', () => { |
| 82 | + const { rerender } = render(<RecentPullRequests data={minimalData} />) |
| 83 | + rerender(<RecentPullRequests data={minimalData} showAvatar={false} />) |
| 84 | + expect(screen.getByText('test-repo')).toBeInTheDocument() |
| 85 | + }) |
| 86 | + |
| 87 | + it('calls router.push with correct URL when repository name is clicked', () => { |
| 88 | + render(<RecentPullRequests data={minimalData} />) |
| 89 | + fireEvent.click(screen.getByText('test-repo')) |
| 90 | + expect(mockRouterPush).toHaveBeenCalledWith('/organizations/test-org/repositories/test-repo') |
| 91 | + }) |
| 92 | + |
| 93 | + it('renders correctly when data is empty', () => { |
| 94 | + render(<RecentPullRequests data={[]} />) |
| 95 | + expect(screen.getByText('Recent Pull Requests')).toBeInTheDocument() |
| 96 | + expect(screen.queryByText('test-repo')).not.toBeInTheDocument() |
| 97 | + }) |
| 98 | + |
| 99 | + it('handles undefined data prop gracefully', () => { |
| 100 | + render(<RecentPullRequests data={undefined} />) |
| 101 | + expect(screen.getByText('Recent Pull Requests')).toBeInTheDocument() |
| 102 | + }) |
| 103 | + |
| 104 | + it('has accessible title and buttons', () => { |
| 105 | + render(<RecentPullRequests data={minimalData} />) |
| 106 | + expect(screen.getByRole('heading', { name: /Recent Pull Requests/i })).toBeInTheDocument() |
| 107 | + expect(screen.getByText('test-repo').closest('button')).toBeInTheDocument() |
| 108 | + }) |
| 109 | + |
| 110 | + it('renders with expected classNames', () => { |
| 111 | + render(<RecentPullRequests data={minimalData} />) |
| 112 | + expect(screen.getByText('Recent Pull Requests').parentElement).toHaveClass('flex') |
| 113 | + }) |
| 114 | +}) |
0 commit comments