2026-04-23 20:52:43 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
|
import type { NavItem } from '../model/types';
|
2026-04-24 08:38:00 +03:00
|
|
|
import { MobileNav } from './MobileNav';
|
2026-04-19 08:40:08 +03:00
|
|
|
|
2026-05-11 11:11:53 +03:00
|
|
|
vi.mock('next/navigation', () => ({ usePathname: vi.fn(() => '/') }));
|
|
|
|
|
|
|
|
|
|
const ITEMS: NavItem[] = [
|
|
|
|
|
{ id: 'intro', label: 'Intro', number: '01' },
|
|
|
|
|
{ id: 'bio', label: 'Bio', number: '02' },
|
|
|
|
|
];
|
2026-04-19 08:40:08 +03:00
|
|
|
|
|
|
|
|
describe('MobileNav', () => {
|
|
|
|
|
describe('rendering', () => {
|
|
|
|
|
it('renders title "allmy.work"', () => {
|
2026-04-23 20:52:43 +03:00
|
|
|
render(<MobileNav items={ITEMS} />);
|
|
|
|
|
expect(screen.getByText('allmy.work')).toBeInTheDocument();
|
|
|
|
|
});
|
2026-04-19 08:40:08 +03:00
|
|
|
|
|
|
|
|
it('renders toggle button with text "Menu" initially', () => {
|
2026-04-23 20:52:43 +03:00
|
|
|
render(<MobileNav items={ITEMS} />);
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Menu' })).toBeInTheDocument();
|
|
|
|
|
});
|
2026-04-19 08:40:08 +03:00
|
|
|
|
|
|
|
|
it('menu items are hidden initially', () => {
|
2026-04-23 20:52:43 +03:00
|
|
|
render(<MobileNav items={ITEMS} />);
|
2026-05-11 11:11:53 +03:00
|
|
|
expect(screen.queryByRole('link', { name: /intro/i })).not.toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('navigation items', () => {
|
|
|
|
|
it('shows items as links with correct hrefs when open', async () => {
|
|
|
|
|
render(<MobileNav items={ITEMS} />);
|
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Menu' }));
|
|
|
|
|
expect(screen.getByRole('link', { name: /01.*Intro/i })).toHaveAttribute('href', '/intro');
|
|
|
|
|
expect(screen.getByRole('link', { name: /02.*Bio/i })).toHaveAttribute('href', '/bio');
|
2026-04-23 20:52:43 +03:00
|
|
|
});
|
|
|
|
|
});
|
2026-04-19 08:40:08 +03:00
|
|
|
|
|
|
|
|
describe('interactions', () => {
|
2026-05-11 11:11:53 +03:00
|
|
|
it('click toggle shows links and changes label to "Close"', async () => {
|
2026-04-23 20:52:43 +03:00
|
|
|
render(<MobileNav items={ITEMS} />);
|
|
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Menu' }));
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Close' })).toBeInTheDocument();
|
2026-05-11 11:11:53 +03:00
|
|
|
expect(screen.getByText('Intro')).toBeInTheDocument();
|
2026-04-23 20:52:43 +03:00
|
|
|
});
|
2026-04-19 08:40:08 +03:00
|
|
|
|
2026-05-11 11:11:53 +03:00
|
|
|
it('closes menu when pathname changes', async () => {
|
|
|
|
|
const { usePathname } = await import('next/navigation');
|
|
|
|
|
vi.mocked(usePathname).mockReturnValue('/');
|
|
|
|
|
const { rerender } = render(<MobileNav items={ITEMS} />);
|
2026-04-23 20:52:43 +03:00
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Menu' }));
|
2026-05-11 11:11:53 +03:00
|
|
|
expect(screen.getByRole('button', { name: 'Close' })).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
vi.mocked(usePathname).mockReturnValue('/bio');
|
|
|
|
|
rerender(<MobileNav items={ITEMS} />);
|
|
|
|
|
|
2026-04-23 20:52:43 +03:00
|
|
|
expect(screen.getByRole('button', { name: 'Menu' })).toBeInTheDocument();
|
2026-05-11 11:11:53 +03:00
|
|
|
expect(screen.queryByRole('button', { name: 'Close' })).not.toBeInTheDocument();
|
2026-04-23 20:52:43 +03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|