|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { BottomNav, type NavItem } from './bottom-nav'; |
| 4 | +import type { ReactNode } from 'react'; |
| 5 | + |
| 6 | +vi.mock('next/link', () => ({ |
| 7 | + default: ({ |
| 8 | + children, |
| 9 | + href, |
| 10 | + ...rest |
| 11 | + }: { |
| 12 | + children?: ReactNode; |
| 13 | + href: string; |
| 14 | + [key: string]: unknown; |
| 15 | + }) => { |
| 16 | + const ariaCurrent = (rest['aria-current'] ?? rest.ariaCurrent) as |
| 17 | + 'page' | 'step' | 'location' | 'date' | 'time' | 'true' | 'false' | undefined; |
| 18 | + return ( |
| 19 | + <a data-testid="nav-link" href={href} aria-current={ariaCurrent}> |
| 20 | + {children} |
| 21 | + </a> |
| 22 | + ); |
| 23 | + }, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock('lucide-react', () => { |
| 27 | + const MockIcon = ({ 'data-testid': testId }: { 'data-testid'?: string }) => ( |
| 28 | + <span data-testid={testId || 'icon'} /> |
| 29 | + ); |
| 30 | + return { |
| 31 | + LayoutDashboard: () => <MockIcon data-testid="icon-dashboard" />, |
| 32 | + Users: () => <MockIcon data-testid="icon-users" />, |
| 33 | + Dumbbell: () => <MockIcon data-testid="icon-dumbbell" />, |
| 34 | + DollarSign: () => <MockIcon data-testid="icon-dollar" />, |
| 35 | + FileText: () => <MockIcon data-testid="icon-file" />, |
| 36 | + FolderKanban: () => <MockIcon data-testid="icon-folder" />, |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +const ITEMS: NavItem[] = [ |
| 41 | + { href: '/dashboard', label: 'Dashboard', iconName: 'layout-dashboard' }, |
| 42 | + { href: '/dashboard/alunos', label: 'Alunos', iconName: 'users' }, |
| 43 | +]; |
| 44 | + |
| 45 | +describe('BottomNav', () => { |
| 46 | + it('renders all items with correct labels and hrefs', () => { |
| 47 | + render(<BottomNav items={ITEMS} activeHref="/dashboard" />); |
| 48 | + expect(screen.getByText('Dashboard')).toBeTruthy(); |
| 49 | + expect(screen.getByText('Alunos')).toBeTruthy(); |
| 50 | + const links = screen.getAllByTestId('nav-link'); |
| 51 | + const hrefs = links.map((l) => l.getAttribute('href')); |
| 52 | + expect(hrefs).toContain('/dashboard'); |
| 53 | + expect(hrefs).toContain('/dashboard/alunos'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('marks the active item with aria-current=page', () => { |
| 57 | + render(<BottomNav items={ITEMS} activeHref="/dashboard/alunos" />); |
| 58 | + const links = screen.getAllByTestId('nav-link'); |
| 59 | + const active = links.find((l) => l.getAttribute('aria-current') === 'page'); |
| 60 | + expect(active).toBeTruthy(); |
| 61 | + expect(active?.getAttribute('href')).toBe('/dashboard/alunos'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not mark inactive items as current page', () => { |
| 65 | + render(<BottomNav items={ITEMS} activeHref="/dashboard/alunos" />); |
| 66 | + const links = screen.getAllByTestId('nav-link'); |
| 67 | + const currents = links.filter((l) => l.getAttribute('aria-current') === 'page'); |
| 68 | + expect(currents).toHaveLength(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it('root dashboard active only on exact match', () => { |
| 72 | + const { rerender } = render(<BottomNav items={ITEMS} activeHref="/dashboard/alunos/123" />); |
| 73 | + let links = screen.getAllByTestId('nav-link'); |
| 74 | + let dashboardLink = links.find((l) => l.getAttribute('href') === '/dashboard'); |
| 75 | + expect(dashboardLink?.getAttribute('aria-current')).toBeNull(); |
| 76 | + const alunosLink = links.find((l) => l.getAttribute('href') === '/dashboard/alunos'); |
| 77 | + expect(alunosLink?.getAttribute('aria-current')).toBe('page'); |
| 78 | + |
| 79 | + rerender(<BottomNav items={ITEMS} activeHref="/dashboard" />); |
| 80 | + links = screen.getAllByTestId('nav-link'); |
| 81 | + dashboardLink = links.find((l) => l.getAttribute('href') === '/dashboard'); |
| 82 | + expect(dashboardLink?.getAttribute('aria-current')).toBe('page'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('renders container with md:hidden class', () => { |
| 86 | + const { container } = render(<BottomNav items={ITEMS} activeHref="/dashboard" />); |
| 87 | + const nav = container.querySelector('nav'); |
| 88 | + expect(nav).toBeTruthy(); |
| 89 | + expect(nav?.className).toContain('md:hidden'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('renders nothing when items is empty', () => { |
| 93 | + const { container } = render(<BottomNav items={[]} activeHref="/" />); |
| 94 | + const links = container.querySelectorAll('[data-testid="nav-link"]'); |
| 95 | + expect(links).toHaveLength(0); |
| 96 | + }); |
| 97 | +}); |
0 commit comments