|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
| 2 | +import { render, screen } from '@testing-library/react' |
| 3 | +import GalleryLayout from './GalleryLayout' |
| 4 | + |
| 5 | +// Mock the DarkModeToggle component from @servicestack/react |
| 6 | +vi.mock('@servicestack/react', () => ({ |
| 7 | + DarkModeToggle: () => <div data-testid="dark-mode-toggle">Dark Mode Toggle</div> |
| 8 | +})) |
| 9 | + |
| 10 | +describe('GalleryLayout', () => { |
| 11 | + beforeEach(() => { |
| 12 | + // Reset window.location.pathname before each test |
| 13 | + Object.defineProperty(window, 'location', { |
| 14 | + value: { pathname: '/' }, |
| 15 | + writable: true, |
| 16 | + }) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should render children content', () => { |
| 20 | + render( |
| 21 | + <GalleryLayout> |
| 22 | + <div>Test Content</div> |
| 23 | + </GalleryLayout> |
| 24 | + ) |
| 25 | + expect(screen.getByText('Test Content')).toBeInTheDocument() |
| 26 | + }) |
| 27 | + |
| 28 | + it('should render title when provided', () => { |
| 29 | + render( |
| 30 | + <GalleryLayout title="Test Title"> |
| 31 | + <div>Content</div> |
| 32 | + </GalleryLayout> |
| 33 | + ) |
| 34 | + expect(screen.getByText('Test Title')).toBeInTheDocument() |
| 35 | + }) |
| 36 | + |
| 37 | + it('should not render title when not provided', () => { |
| 38 | + const { container } = render( |
| 39 | + <GalleryLayout> |
| 40 | + <div>Content</div> |
| 41 | + </GalleryLayout> |
| 42 | + ) |
| 43 | + const h1 = container.querySelector('h1') |
| 44 | + expect(h1).toBeNull() |
| 45 | + }) |
| 46 | + |
| 47 | + it('should render header with logo and site name', () => { |
| 48 | + render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 49 | + expect(screen.getByText('React Component Gallery')).toBeInTheDocument() |
| 50 | + }) |
| 51 | + |
| 52 | + it('should render React logo SVG', () => { |
| 53 | + const { container } = render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 54 | + const svg = container.querySelector('svg') |
| 55 | + expect(svg).toBeInTheDocument() |
| 56 | + expect(svg).toHaveClass('w-8', 'h-8', 'text-[#61DAFB]') |
| 57 | + }) |
| 58 | + |
| 59 | + it('should render DarkModeToggle', () => { |
| 60 | + render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 61 | + expect(screen.getByTestId('dark-mode-toggle')).toBeInTheDocument() |
| 62 | + }) |
| 63 | + |
| 64 | + it('should render navigation sidebar', () => { |
| 65 | + render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 66 | + expect(screen.getByText('Getting Started')).toBeInTheDocument() |
| 67 | + expect(screen.getByText('Component Gallery')).toBeInTheDocument() |
| 68 | + expect(screen.getByText('Library')).toBeInTheDocument() |
| 69 | + }) |
| 70 | + |
| 71 | + it('should render all navigation links', () => { |
| 72 | + render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 73 | + |
| 74 | + // Getting Started |
| 75 | + expect(screen.getByText('Installation')).toBeInTheDocument() |
| 76 | + |
| 77 | + // Component Gallery |
| 78 | + expect(screen.getByText('AutoQueryGrid')).toBeInTheDocument() |
| 79 | + expect(screen.getByText('DataGrid')).toBeInTheDocument() |
| 80 | + expect(screen.getByText('Auto Forms')).toBeInTheDocument() |
| 81 | + expect(screen.getByText('FileInput')).toBeInTheDocument() |
| 82 | + expect(screen.getByText('TagInput')).toBeInTheDocument() |
| 83 | + expect(screen.getByText('Combobox')).toBeInTheDocument() |
| 84 | + expect(screen.getByText('Autocomplete')).toBeInTheDocument() |
| 85 | + expect(screen.getByText('Markdown Editor')).toBeInTheDocument() |
| 86 | + |
| 87 | + // Library |
| 88 | + expect(screen.getByText('useMetadata')).toBeInTheDocument() |
| 89 | + expect(screen.getByText('useClient')).toBeInTheDocument() |
| 90 | + expect(screen.getByText('useAuth')).toBeInTheDocument() |
| 91 | + }) |
| 92 | + |
| 93 | + it('should highlight active link based on current path', () => { |
| 94 | + Object.defineProperty(window, 'location', { |
| 95 | + value: { pathname: '/gallery/autoquerygrid' }, |
| 96 | + writable: true, |
| 97 | + }) |
| 98 | + |
| 99 | + const { container } = render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 100 | + const activeLink = container.querySelector('a[href="/gallery/autoquerygrid"]') |
| 101 | + |
| 102 | + expect(activeLink).toHaveClass('text-indigo-600', 'dark:text-indigo-400', 'font-semibold') |
| 103 | + }) |
| 104 | + |
| 105 | + it('should apply hover styles to non-active links', () => { |
| 106 | + Object.defineProperty(window, 'location', { |
| 107 | + value: { pathname: '/' }, |
| 108 | + writable: true, |
| 109 | + }) |
| 110 | + |
| 111 | + const { container } = render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 112 | + const link = container.querySelector('a[href="/gallery/autoquerygrid"]') |
| 113 | + |
| 114 | + expect(link).toHaveClass('text-gray-700', 'dark:text-gray-300') |
| 115 | + }) |
| 116 | + |
| 117 | + it('should have correct link hrefs', () => { |
| 118 | + const { container } = render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 119 | + |
| 120 | + const installLink = container.querySelector('a[href="/gallery/install"]') |
| 121 | + expect(installLink).toBeInTheDocument() |
| 122 | + |
| 123 | + const autoQueryLink = container.querySelector('a[href="/gallery/autoquerygrid"]') |
| 124 | + expect(autoQueryLink).toBeInTheDocument() |
| 125 | + |
| 126 | + const useMetadataLink = container.querySelector('a[href="/gallery/use-metadata"]') |
| 127 | + expect(useMetadataLink).toBeInTheDocument() |
| 128 | + }) |
| 129 | + |
| 130 | + it('should render main content area with prose styling', () => { |
| 131 | + const { container } = render( |
| 132 | + <GalleryLayout> |
| 133 | + <div>Test Content</div> |
| 134 | + </GalleryLayout> |
| 135 | + ) |
| 136 | + |
| 137 | + const main = container.querySelector('main') |
| 138 | + expect(main).toHaveClass('prose', 'prose-slate', 'dark:prose-invert') |
| 139 | + }) |
| 140 | + |
| 141 | + it('should have responsive layout classes', () => { |
| 142 | + const { container } = render(<GalleryLayout><div>Content</div></GalleryLayout>) |
| 143 | + |
| 144 | + const aside = container.querySelector('aside') |
| 145 | + expect(aside).toHaveClass('hidden', 'lg:block') |
| 146 | + }) |
| 147 | +}) |
| 148 | + |
0 commit comments