Skip to content
Closed
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1e8e1a7
card component enhanced
yashgoyal0110 Mar 2, 2025
6a15a66
card component enhanced
yashgoyal0110 Mar 2, 2025
b1b11fd
card component enhanced
yashgoyal0110 Mar 2, 2025
e1d9912
card component enhanced
yashgoyal0110 Mar 2, 2025
101a65d
card component enhanced
yashgoyal0110 Mar 2, 2025
63fe7aa
commit
yashgoyal0110 Mar 2, 2025
fcee7c1
commit
yashgoyal0110 Mar 2, 2025
a236501
commit
yashgoyal0110 Mar 2, 2025
d2472b3
commit
yashgoyal0110 Mar 2, 2025
735ae39
Merge branch 'OWASP:main' into feat/card-component-enhanced
yashgoyal0110 Mar 3, 2025
9b98402
commit
yashgoyal0110 Mar 3, 2025
d6d47c1
Merge branch 'main' into feat/card-component-enhanced
yashgoyal0110 Mar 3, 2025
5e51ee9
Merge branch 'OWASP:main' into feat/card-component-enhanced
yashgoyal0110 Mar 4, 2025
30db1ca
commit
yashgoyal0110 Mar 4, 2025
7e3e27e
commit
yashgoyal0110 Mar 4, 2025
8c9f2a1
commit
yashgoyal0110 Mar 4, 2025
f19d93d
commit
yashgoyal0110 Mar 4, 2025
07b68ca
commit
yashgoyal0110 Mar 4, 2025
677f1da
commit
yashgoyal0110 Mar 4, 2025
6b3187d
commit
yashgoyal0110 Mar 4, 2025
02cf0df
Merge branch 'main' of https://github.com/yashgoyal0110/Nest into fea…
yashgoyal0110 Mar 6, 2025
77d2cd0
Merge branch 'main' into feat/card-component-enhanced
yashgoyal0110 Mar 6, 2025
f21c6f0
Merge branch 'feat/card-component-enhanced' of https://github.com/yas…
yashgoyal0110 Mar 6, 2025
673c359
commit
yashgoyal0110 Mar 6, 2025
93e2b57
commit
yashgoyal0110 Mar 6, 2025
3a37370
commit
yashgoyal0110 Mar 6, 2025
d0d4023
commit
yashgoyal0110 Mar 6, 2025
df24d38
commit
yashgoyal0110 Mar 6, 2025
1fd224a
Merge branch 'main' into feat/card-component-enhanced
yashgoyal0110 Mar 8, 2025
3493b43
commit
yashgoyal0110 Mar 8, 2025
8e72351
Merge branch 'main' into feat/card-component-enhanced
yashgoyal0110 Mar 10, 2025
1018a2b
ommit
yashgoyal0110 Mar 10, 2025
801b11b
Merge branch 'OWASP:main' into feat/card-component-enhanced
yashgoyal0110 Mar 15, 2025
57b7272
Update Contribute.test.tsx
yashgoyal0110 Mar 15, 2025
88b7f2c
commit
yashgoyal0110 Mar 15, 2025
27b1476
commit
yashgoyal0110 Mar 15, 2025
33a6c04
Merge branch 'main' into feat/card-component-enhanced
yashgoyal0110 Mar 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 54 additions & 33 deletions frontend/__tests__/unit/pages/Contribute.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { mockContributeData } from '@unit/data/mockContributeData'
import { fetchAlgoliaData } from 'api/fetchAlgoliaData'
import { render } from 'wrappers/testUtil'
import ContributePage from 'pages/Contribute'

// Mock dependencies
jest.mock('api/fetchAlgoliaData', () => ({
fetchAlgoliaData: jest.fn(),
}))

jest.mock('@fortawesome/react-fontawesome', () => ({
FontAwesomeIcon: jest.fn(() => null),
}))

jest.mock('components/Pagination', () =>
jest.fn(({ currentPage, onPageChange, totalPages }) =>
totalPages > 1 ? (
Expand All @@ -19,12 +25,19 @@ jest.mock('components/Pagination', () =>
)

describe('Contribute Component', () => {
// Suppress console errors and warnings during tests
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {})
jest.spyOn(console, 'warn').mockImplementation(() => {})

// Reset mocks before each test
;(fetchAlgoliaData as jest.Mock).mockResolvedValue(mockContributeData)
;(FontAwesomeIcon as jest.Mock).mockReturnValue(<div>Icon</div>)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
})

afterEach(() => {
jest.clearAllMocks()
jest.restoreAllMocks()
})

test('renders skeleton initially', async () => {
Expand All @@ -36,19 +49,22 @@ describe('Contribute Component', () => {
})

test('renders contribute data correctly', async () => {
;(fetchAlgoliaData as jest.Mock).mockResolvedValue({
const mockIssuesData = {
...mockContributeData,
hits: mockContributeData.issues,
totalPages: 1,
})
}
;(fetchAlgoliaData as jest.Mock).mockResolvedValue(mockIssuesData)

render(<ContributePage />)

await waitFor(() => {
expect(screen.getByText('Contribution 1')).toBeInTheDocument()
})
expect(screen.getByText('This is a summary of Contribution 1')).toBeInTheDocument()
const viewButton = screen.getByText('Read More')
expect(viewButton).toBeInTheDocument()

const readMoreButtons = screen.getAllByRole('button', { name: /read more/i })
expect(readMoreButtons.length).toBeGreaterThan(0)
})

test('displays "No issues found" when there are no issues', async () => {
Expand Down Expand Up @@ -143,60 +159,68 @@ describe('Contribute Component', () => {
render(<ContributePage />)

await waitFor(() => {
expect(screen.queryByText('Read More')).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: /read more/i })).not.toBeInTheDocument()
})
})

test('renders SubmitButton correctly', async () => {
;(fetchAlgoliaData as jest.Mock).mockResolvedValue({
const mockIssuesData = {
...mockContributeData,
hits: mockContributeData.issues,
totalPages: 1,
})
}
;(fetchAlgoliaData as jest.Mock).mockResolvedValue(mockIssuesData)
render(<ContributePage />)

await waitFor(() => {
const readMoreButton = screen.getByText('Read More')
expect(readMoreButton).toBeInTheDocument()
const readMoreButtons = screen.getAllByRole('button', { name: /read more/i })
expect(readMoreButtons.length).toBeGreaterThan(0)
})
})

test('opens modal when SubmitButton is clicked', async () => {
;(fetchAlgoliaData as jest.Mock).mockResolvedValue({
const mockIssuesData = {
...mockContributeData,
hits: mockContributeData.issues,
totalPages: 1,
})
}
;(fetchAlgoliaData as jest.Mock).mockResolvedValue(mockIssuesData)

render(<ContributePage />)

await waitFor(() => {
const readMoreButton = screen.getByText('Read More')
fireEvent.click(readMoreButton)
const readMoreButtons = screen.getAllByRole('button', { name: /read more/i })
expect(readMoreButtons.length).toBeGreaterThan(0)
})

await waitFor(() => {
const modalTitle = screen.getByText('Close')
expect(modalTitle).toBeInTheDocument()
})
const readMoreButton = screen.getAllByRole('button', { name: /read more/i })[0]
fireEvent.click(readMoreButton)

const closeButton = await screen.findByRole('button', { name: /close/i })
expect(closeButton).toBeInTheDocument()
})

test('closes modal when onClose is called', async () => {
;(fetchAlgoliaData as jest.Mock).mockResolvedValue({
const mockIssuesData = {
...mockContributeData,
hits: mockContributeData.issues,
totalPages: 1,
})
}
;(fetchAlgoliaData as jest.Mock).mockResolvedValue(mockIssuesData)

render(<ContributePage />)

await waitFor(() => {
const readMoreButton = screen.getByText('Read More')
fireEvent.click(readMoreButton)
const readMoreButtons = screen.getAllByRole('button', { name: /read more/i })
expect(readMoreButtons.length).toBeGreaterThan(0)
})

await waitFor(() => {
const closeButton = screen.getByText('Close')
fireEvent.click(closeButton)
})
const readMoreButton = screen.getAllByRole('button', { name: /read more/i })[0]
fireEvent.click(readMoreButton)

const closeButton = await screen.findByRole('button', { name: /close/i })
expect(closeButton).toBeInTheDocument()
fireEvent.click(closeButton)

await waitFor(() => {
const modalTitle = screen.queryByText(mockContributeData.issues[0].title)
Expand All @@ -218,12 +242,12 @@ describe('Contribute Component', () => {

// Wait for both cards to be rendered
await waitFor(() => {
const readMoreButtons = screen.getAllByText('Read More')
const readMoreButtons = screen.getAllByRole('button', { name: /read more/i })
expect(readMoreButtons).toHaveLength(2)
})

// Click first card's Read More button
const readMoreButtons = screen.getAllByText('Read More')
const readMoreButtons = screen.getAllByRole('button', { name: /read more/i })
fireEvent.click(readMoreButtons[0])

// Verify first modal is open
Expand All @@ -232,15 +256,12 @@ describe('Contribute Component', () => {
})

// Verify first issue button
await waitFor(() => {
const viewIssueButton = screen.getByRole('button', { name: 'View Issue' })
expect(viewIssueButton).toBeInTheDocument()
fireEvent.click(viewIssueButton)
})
const viewIssueButton = await screen.findByRole('button', { name: 'View Issue' })
expect(viewIssueButton).toBeInTheDocument()

// Click close button
await waitFor(() => {
const closeButton = screen.getByText('Close')
const closeButton = screen.getByRole('button', { name: /close/i })
fireEvent.click(closeButton)
})

Expand Down
Loading