|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react' |
| 2 | +import { vi } from 'vitest' |
| 3 | +import { Navigation } from './Navigation' |
| 4 | + |
| 5 | +const mockLogout = vi.fn() |
| 6 | + |
| 7 | +vi.mock('@/contexts/AuthContext', () => ({ |
| 8 | + useAuth: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +import { useAuth } from '@/contexts/AuthContext' |
| 12 | +const mockUseAuth = useAuth as ReturnType<typeof vi.fn> |
| 13 | + |
| 14 | +describe('Navigation (unauthenticated)', () => { |
| 15 | + beforeEach(() => { |
| 16 | + mockUseAuth.mockReturnValue({ isAuthenticated: false, logout: mockLogout }) |
| 17 | + }) |
| 18 | + |
| 19 | + it('renders Login and Sign Up links', () => { |
| 20 | + render(<Navigation />) |
| 21 | + expect(screen.getByRole('link', { name: 'Login' })).toBeInTheDocument() |
| 22 | + expect(screen.getByRole('link', { name: 'Sign Up' })).toBeInTheDocument() |
| 23 | + }) |
| 24 | + |
| 25 | + it('does not render Logout button', () => { |
| 26 | + render(<Navigation />) |
| 27 | + expect(screen.queryByRole('button', { name: 'Logout' })).not.toBeInTheDocument() |
| 28 | + }) |
| 29 | + |
| 30 | + it('does not render Items link', () => { |
| 31 | + render(<Navigation />) |
| 32 | + expect(screen.queryByRole('link', { name: 'Items' })).not.toBeInTheDocument() |
| 33 | + }) |
| 34 | +}) |
| 35 | + |
| 36 | +describe('Navigation (authenticated)', () => { |
| 37 | + beforeEach(() => { |
| 38 | + mockLogout.mockReset() |
| 39 | + mockUseAuth.mockReturnValue({ isAuthenticated: true, logout: mockLogout }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('renders Logout button and Items link', () => { |
| 43 | + render(<Navigation />) |
| 44 | + expect(screen.getByRole('button', { name: 'Logout' })).toBeInTheDocument() |
| 45 | + expect(screen.getByRole('link', { name: 'Items' })).toBeInTheDocument() |
| 46 | + }) |
| 47 | + |
| 48 | + it('does not render Login or Sign Up when authenticated', () => { |
| 49 | + render(<Navigation />) |
| 50 | + expect(screen.queryByRole('link', { name: 'Login' })).not.toBeInTheDocument() |
| 51 | + expect(screen.queryByRole('link', { name: 'Sign Up' })).not.toBeInTheDocument() |
| 52 | + }) |
| 53 | + |
| 54 | + it('calls logout when Logout button is clicked', () => { |
| 55 | + render(<Navigation />) |
| 56 | + fireEvent.click(screen.getByRole('button', { name: 'Logout' })) |
| 57 | + expect(mockLogout).toHaveBeenCalledTimes(1) |
| 58 | + }) |
| 59 | +}) |
0 commit comments