Skip to content

Commit 7008341

Browse files
Test/recent pull request (#2011)
* added test for RecentPullRequest.test.tsx * Revert "added test for RecentPullRequest.test.tsx" This reverts commit bcce155. * feat: add comprehensive RecentPullRequests component tests - Add complete test suite covering all 10 testing criteria - Include rendering, conditional logic, prop behavior tests - Add event handling and accessibility tests - Cover edge cases and DOM structure validation - Implement proper mocking for dependencies - Organize tests with clear section headers for maintainability * refactor: enhance RecentPullRequests tests for showAvatar prop - Update tests to check behavior of showAvatar prop with assertions for avatar presence and absence - Remove redundant tests for default prop values and content rendering - Improve test organization for clarity and maintainability * removed extra comments --------- Co-authored-by: Kate Golovanova <[email protected]>
1 parent 0fed264 commit 7008341

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)