-
-
Notifications
You must be signed in to change notification settings - Fork 250
Breadcrumb component implementation #1397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arkid15r
merged 8 commits into
OWASP:main
from
yashgoyal0110:feat/breadcrumb-implementation
Apr 19, 2025
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22b1114
breadcrumb component
yashgoyal0110 eb4ec50
updated code
yashgoyal0110 0cdab79
added e2e
yashgoyal0110 e307dcb
update code
yashgoyal0110 072a41d
updated code
yashgoyal0110 29dea0a
Merge branch 'main' into feat/breadcrumb-implementation
yashgoyal0110 06f340a
Update code
arkid15r 6657121
Update tests
arkid15r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { render, screen } from '@testing-library/react' | ||
| import { usePathname } from 'next/navigation' | ||
| import BreadCrumbs from 'components/BreadCrumbs' | ||
| import '@testing-library/jest-dom' | ||
|
|
||
| jest.mock('next/navigation', () => ({ | ||
| usePathname: jest.fn(), | ||
| })) | ||
|
|
||
| describe('BreadCrumb', () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| test('does not render on root path "/"', () => { | ||
| ;(usePathname as jest.Mock).mockReturnValue('/') | ||
|
|
||
| render(<BreadCrumbs />) | ||
| expect(screen.queryByText('Home')).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('renders breadcrumb with multiple segments', () => { | ||
| ;(usePathname as jest.Mock).mockReturnValue('/dashboard/users/profile') | ||
|
|
||
| render(<BreadCrumbs />) | ||
|
|
||
| expect(screen.getByText('Home')).toBeInTheDocument() | ||
| expect(screen.getByText('Dashboard')).toBeInTheDocument() | ||
| expect(screen.getByText('Users')).toBeInTheDocument() | ||
| expect(screen.getByText('Profile')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('disables the last segment (non-clickable)', () => { | ||
| ;(usePathname as jest.Mock).mockReturnValue('/settings/account') | ||
|
|
||
| render(<BreadCrumbs />) | ||
|
|
||
| const lastSegment = screen.getByText('Account') | ||
| expect(lastSegment).toBeInTheDocument() | ||
| expect(lastSegment).not.toHaveAttribute('href') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 'use client' | ||
|
|
||
| import { faChevronRight } from '@fortawesome/free-solid-svg-icons' | ||
| import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' | ||
| import { Breadcrumbs, BreadcrumbItem } from '@heroui/react' | ||
| import Link from 'next/link' | ||
| import { usePathname } from 'next/navigation' | ||
|
|
||
| const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1).replace(/-/g, ' ') | ||
|
|
||
| export default function BreadCrumbs() { | ||
| const pathname = usePathname() | ||
| const segments = pathname.split('/').filter(Boolean) | ||
|
|
||
| if (pathname === '/') return null | ||
|
|
||
| return ( | ||
| <div className="absolute bottom-2 left-0 top-1 mt-16 w-full py-2"> | ||
| <div className="w-full px-8 sm:px-8 md:px-8 lg:px-8"> | ||
| <Breadcrumbs | ||
| aria-label="breadcrumb" | ||
| separator={ | ||
| <FontAwesomeIcon | ||
| icon={faChevronRight} | ||
| className="mx-1 text-xs text-gray-400 dark:text-gray-500" | ||
| /> | ||
| } | ||
| className="text-gray-800 dark:text-gray-200" | ||
| itemClasses={{ | ||
| base: 'transition-colors duration-200', | ||
| item: 'text-sm font-medium', | ||
| separator: 'flex items-center', | ||
| }} | ||
| > | ||
| <BreadcrumbItem> | ||
| <Link href="/" className="hover:text-blue-700 hover:underline dark:text-blue-400"> | ||
| Home | ||
| </Link> | ||
| </BreadcrumbItem> | ||
|
|
||
| {segments.map((segment, index) => { | ||
| const href = '/' + segments.slice(0, index + 1).join('/') | ||
| const label = capitalize(segment) | ||
| const isLast = index === segments.length - 1 | ||
|
|
||
| return ( | ||
| <BreadcrumbItem key={href} isDisabled={isLast}> | ||
| {isLast ? ( | ||
| <span className="cursor-default font-semibold text-gray-600 dark:text-gray-300"> | ||
| {label} | ||
| </span> | ||
| ) : ( | ||
| <Link | ||
| href={href} | ||
| className="hover:text-blue-700 hover:underline dark:text-blue-400" | ||
| > | ||
| {label} | ||
| </Link> | ||
| )} | ||
| </BreadcrumbItem> | ||
| ) | ||
| })} | ||
| </Breadcrumbs> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Test coverage can be improved for breadcrumb verification.
While the test verifies that the breadcrumb is visible and contains the 'Contribute' segment, it's missing some important checks:
Consider enhancing this test to align with the more comprehensive approach used in the Organizations.spec.ts:
test('breadcrumb renders correct segments on /contribute', async ({ page }) => { const breadcrumb = page.locator('[aria-label="breadcrumb"]') await expect(breadcrumb).toBeVisible() + await expect(breadcrumb.getByText('Home')).toBeVisible() await expect(breadcrumb.getByText('Contribute')).toBeVisible() + const homeLink = breadcrumb.locator('a', { hasText: 'Home' }) + await expect(homeLink).toHaveAttribute('href', '/') })📝 Committable suggestion