|
| 1 | +/** |
| 2 | + * Tests for HomeLayout — lightweight nav shell for /home. |
| 3 | + * Validates: layout rendering, user avatar, navigation links. |
| 4 | + * |
| 5 | + * Note: Radix DropdownMenu portal rendering is limited in jsdom, |
| 6 | + * so we test the trigger and visible elements rather than dropdown contents. |
| 7 | + */ |
| 8 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 9 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 10 | +import '@testing-library/jest-dom'; |
| 11 | +import { MemoryRouter } from 'react-router-dom'; |
| 12 | +import { HomeLayout } from '../pages/home/HomeLayout'; |
| 13 | + |
| 14 | +// --- Mocks --- |
| 15 | + |
| 16 | +const mockNavigate = vi.fn(); |
| 17 | +vi.mock('react-router-dom', async (importOriginal) => { |
| 18 | + const actual = await importOriginal<any>(); |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + useNavigate: () => mockNavigate, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +const mockSignOut = vi.fn(); |
| 26 | +vi.mock('@object-ui/auth', () => ({ |
| 27 | + useAuth: () => ({ |
| 28 | + user: { name: 'Alice Dev', email: 'alice@test.com', image: null }, |
| 29 | + signOut: mockSignOut, |
| 30 | + isAuthenticated: true, |
| 31 | + }), |
| 32 | + getUserInitials: () => 'AD', |
| 33 | +})); |
| 34 | + |
| 35 | +vi.mock('@object-ui/i18n', () => ({ |
| 36 | + useObjectTranslation: () => ({ |
| 37 | + t: (_key: string, opts?: any) => opts?.defaultValue ?? _key, |
| 38 | + language: 'en', |
| 39 | + changeLanguage: vi.fn(), |
| 40 | + direction: 'ltr', |
| 41 | + i18n: {}, |
| 42 | + }), |
| 43 | +})); |
| 44 | + |
| 45 | +// Mock @object-ui/components to keep most components |
| 46 | +vi.mock('@object-ui/components', async (importOriginal) => { |
| 47 | + const actual = await importOriginal<any>(); |
| 48 | + return { |
| 49 | + ...actual, |
| 50 | + TooltipProvider: ({ children }: any) => <div>{children}</div>, |
| 51 | + }; |
| 52 | +}); |
| 53 | + |
| 54 | +describe('HomeLayout', () => { |
| 55 | + beforeEach(() => { |
| 56 | + vi.clearAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + const renderLayout = (children: React.ReactNode = <div>Page Content</div>) => { |
| 60 | + return render( |
| 61 | + <MemoryRouter initialEntries={['/home']}> |
| 62 | + <HomeLayout>{children}</HomeLayout> |
| 63 | + </MemoryRouter>, |
| 64 | + ); |
| 65 | + }; |
| 66 | + |
| 67 | + it('renders the layout shell with data-testid', () => { |
| 68 | + renderLayout(); |
| 69 | + expect(screen.getByTestId('home-layout')).toBeInTheDocument(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('renders the Home branding button in the top bar', () => { |
| 73 | + renderLayout(); |
| 74 | + const brand = screen.getByTestId('home-layout-brand'); |
| 75 | + expect(brand).toBeInTheDocument(); |
| 76 | + expect(brand).toHaveTextContent('Home'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('renders children inside the layout', () => { |
| 80 | + renderLayout(<div data-testid="child-content">Hello World</div>); |
| 81 | + expect(screen.getByTestId('child-content')).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('renders user avatar with initials fallback', () => { |
| 85 | + renderLayout(); |
| 86 | + // The avatar trigger should show user initials "AD" (Alice Dev) |
| 87 | + expect(screen.getByTestId('home-layout-user-trigger')).toBeInTheDocument(); |
| 88 | + expect(screen.getByText('AD')).toBeInTheDocument(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('renders Settings button in the top bar', () => { |
| 92 | + renderLayout(); |
| 93 | + expect(screen.getByTestId('home-layout-settings-btn')).toBeInTheDocument(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('navigates to /system when Settings button is clicked', () => { |
| 97 | + renderLayout(); |
| 98 | + fireEvent.click(screen.getByTestId('home-layout-settings-btn')); |
| 99 | + expect(mockNavigate).toHaveBeenCalledWith('/system'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('navigates to /home when brand button is clicked', () => { |
| 103 | + renderLayout(); |
| 104 | + fireEvent.click(screen.getByTestId('home-layout-brand')); |
| 105 | + expect(mockNavigate).toHaveBeenCalledWith('/home'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('renders sticky header element', () => { |
| 109 | + renderLayout(); |
| 110 | + const header = screen.getByTestId('home-layout').querySelector('header'); |
| 111 | + expect(header).toBeInTheDocument(); |
| 112 | + expect(header?.className).toContain('sticky'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('renders user menu trigger as a round button', () => { |
| 116 | + renderLayout(); |
| 117 | + const trigger = screen.getByTestId('home-layout-user-trigger'); |
| 118 | + expect(trigger.className).toContain('rounded-full'); |
| 119 | + }); |
| 120 | +}); |
0 commit comments