-
-
Notifications
You must be signed in to change notification settings - Fork 291
enhanced breadcrumbs and testcases #1677
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { screen, waitFor, fireEvent } from '@testing-library/react' | ||
|
|
||
| export const assertRepoDetails = async ({ | ||
| heading, | ||
| license, | ||
| stars, | ||
| forks, | ||
| commits, | ||
| contributors, | ||
| issues, | ||
| }: { | ||
| heading: string | ||
| license: string | ||
| stars: string | ||
| forks: string | ||
| commits?: string | ||
| contributors?: string | ||
| issues: string | ||
| }) => { | ||
| await waitFor(() => { | ||
| const title = screen.getByRole('heading', { name: heading }) | ||
| expect(title).toBeInTheDocument() | ||
| expect(screen.getByText(license)).toBeInTheDocument() | ||
| }) | ||
| expect(screen.getByText(stars)).toBeInTheDocument() | ||
| expect(screen.getByText(forks)).toBeInTheDocument() | ||
| if (commits) expect(screen.getByText(commits)).toBeInTheDocument() | ||
| if (contributors) expect(screen.getByText(contributors)).toBeInTheDocument() | ||
| expect(screen.getByText(issues)).toBeInTheDocument() | ||
| } | ||
|
|
||
| export const assertHeadingsAndTexts = async ({ | ||
| headingText, | ||
| texts, | ||
| }: { | ||
| headingText?: string | ||
| texts: string[] | ||
| }) => { | ||
| if (headingText) { | ||
| await waitFor(() => { | ||
| const heading = screen.getByRole('heading', { name: headingText }) | ||
| expect(heading).toBeInTheDocument() | ||
| }) | ||
| } | ||
|
|
||
| texts.forEach((text) => { | ||
| expect(screen.getByText(text)).toBeInTheDocument() | ||
| }) | ||
| } | ||
|
|
||
| export const assertContributorToggle = async (initial: string, others: string[]) => { | ||
| await waitFor(() => { | ||
| expect(screen.getByText(initial)).toBeInTheDocument() | ||
| expect(screen.queryByText('Contributor 10')).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| const showMoreButton = screen.getByRole('button', { name: /Show more/i }) | ||
| fireEvent.click(showMoreButton) | ||
|
|
||
| await waitFor(() => { | ||
| others.forEach((name) => { | ||
| expect(screen.getByText(name)).toBeInTheDocument() | ||
| }) | ||
| }) | ||
|
|
||
| const showLessButton = screen.getByRole('button', { name: /Show less/i }) | ||
| fireEvent.click(showLessButton) | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.queryByText('Contributor 10')).not.toBeInTheDocument() | ||
| }) | ||
| } | ||
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 |
|---|---|---|
| @@ -1,68 +1,60 @@ | ||
| 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(), | ||
| })) | ||
| const renderBreadCrumbs = (items = []) => render(<BreadCrumbs breadcrumbItems={items} />) | ||
|
|
||
| describe('BreadCrumb', () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| test('does not render on root path "/"', () => { | ||
| ;(usePathname as jest.Mock).mockReturnValue('/') | ||
| const sampleItems = [ | ||
| { title: 'Dashboard', path: '/dashboard' }, | ||
| { title: 'Users', path: '/dashboard/users' }, | ||
| { title: 'Profile', path: '/dashboard/users/profile' }, | ||
| ] | ||
|
|
||
| render(<BreadCrumbs />) | ||
| describe('BreadCrumbs', () => { | ||
| test('does not render when breadcrumb item is empty', () => { | ||
| renderBreadCrumbs() | ||
| expect(screen.queryByText('Home')).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| test('renders breadcrumb with multiple segments', () => { | ||
| ;(usePathname as jest.Mock).mockReturnValue('/dashboard/users/profile') | ||
|
|
||
| render(<BreadCrumbs />) | ||
|
|
||
| renderBreadCrumbs(sampleItems) | ||
| 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 items = [ | ||
| { title: 'Settings', path: '/settings' }, | ||
| { title: 'Account', path: '/settings/account' }, | ||
| ] | ||
| renderBreadCrumbs(items) | ||
| const lastSegment = screen.getByText('Account') | ||
| expect(lastSegment).toBeInTheDocument() | ||
| expect(lastSegment).not.toHaveAttribute('href') | ||
| expect(lastSegment.closest('a')).toBeNull() | ||
| }) | ||
|
|
||
| test('links have correct href attributes', () => { | ||
| ;(usePathname as jest.Mock).mockReturnValue('/dashboard/users/profile') | ||
|
|
||
| render(<BreadCrumbs />) | ||
|
|
||
| const homeLink = screen.getByText('Home').closest('a') | ||
| const dashboardLink = screen.getByText('Dashboard').closest('a') | ||
| const usersLink = screen.getByText('Users').closest('a') | ||
|
|
||
| expect(homeLink).toHaveAttribute('href', '/') | ||
| expect(dashboardLink).toHaveAttribute('href', '/dashboard') | ||
| expect(usersLink).toHaveAttribute('href', '/dashboard/users') | ||
| test('links have correct path attributes', () => { | ||
| renderBreadCrumbs(sampleItems) | ||
| expect(screen.getByText('Home').closest('a')).toHaveAttribute('href', '/') | ||
| expect(screen.getByText('Dashboard').closest('a')).toHaveAttribute('href', '/dashboard') | ||
| expect(screen.getByText('Users').closest('a')).toHaveAttribute('href', '/dashboard/users') | ||
| }) | ||
|
|
||
| test('links have hover styles', () => { | ||
| ;(usePathname as jest.Mock).mockReturnValue('/dashboard/users') | ||
|
|
||
| render(<BreadCrumbs />) | ||
|
|
||
| const homeLink = screen.getByText('Home').closest('a') | ||
| const dashboardLink = screen.getByText('Dashboard').closest('a') | ||
|
|
||
| expect(homeLink).toHaveClass('hover:text-blue-700', 'hover:underline') | ||
| expect(dashboardLink).toHaveClass('hover:text-blue-700', 'hover:underline') | ||
| const items = [ | ||
| { title: 'Dashboard', path: '/dashboard' }, | ||
| { title: 'Users', path: '/dashboard/users' }, | ||
| ] | ||
| renderBreadCrumbs(items) | ||
| expect(screen.getByText('Home').closest('a')).toHaveClass( | ||
| 'hover:text-blue-700', | ||
| 'hover:underline' | ||
| ) | ||
| expect(screen.getByText('Dashboard').closest('a')).toHaveClass( | ||
| 'hover:text-blue-700', | ||
| 'hover:underline' | ||
| ) | ||
| }) | ||
| }) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.