|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import { usePathname } from 'next/navigation' |
| 3 | +import BreadCrumbs from 'components/BreadCrumbs' |
| 4 | +import '@testing-library/jest-dom' |
| 5 | + |
| 6 | +jest.mock('next/navigation', () => ({ |
| 7 | + usePathname: jest.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +describe('BreadCrumb', () => { |
| 11 | + afterEach(() => { |
| 12 | + jest.clearAllMocks() |
| 13 | + }) |
| 14 | + |
| 15 | + test('does not render on root path "/"', () => { |
| 16 | + ;(usePathname as jest.Mock).mockReturnValue('/') |
| 17 | + |
| 18 | + render(<BreadCrumbs />) |
| 19 | + expect(screen.queryByText('Home')).not.toBeInTheDocument() |
| 20 | + }) |
| 21 | + |
| 22 | + test('renders breadcrumb with multiple segments', () => { |
| 23 | + ;(usePathname as jest.Mock).mockReturnValue('/dashboard/users/profile') |
| 24 | + |
| 25 | + render(<BreadCrumbs />) |
| 26 | + |
| 27 | + expect(screen.getByText('Home')).toBeInTheDocument() |
| 28 | + expect(screen.getByText('Dashboard')).toBeInTheDocument() |
| 29 | + expect(screen.getByText('Users')).toBeInTheDocument() |
| 30 | + expect(screen.getByText('Profile')).toBeInTheDocument() |
| 31 | + }) |
| 32 | + |
| 33 | + test('disables the last segment (non-clickable)', () => { |
| 34 | + ;(usePathname as jest.Mock).mockReturnValue('/settings/account') |
| 35 | + |
| 36 | + render(<BreadCrumbs />) |
| 37 | + |
| 38 | + const lastSegment = screen.getByText('Account') |
| 39 | + expect(lastSegment).toBeInTheDocument() |
| 40 | + expect(lastSegment).not.toHaveAttribute('href') |
| 41 | + }) |
| 42 | +}) |
0 commit comments