import { render, screen } from '@testing-library/react';
import { Layout } from '../Layout';
import { useAuthStore } from '@/stores/authStore';
import { usePathname } from 'next/navigation';
// Mock child components
jest.mock('../Header', () => ({
Header: () =>
Header
,
}));
jest.mock('../Sidebar', () => ({
Sidebar: () => Sidebar
,
}));
jest.mock('../Breadcrumbs', () => ({
Breadcrumbs: () => Breadcrumbs
,
}));
// Mock Next.js navigation
jest.mock('next/navigation', () => ({
usePathname: jest.fn(),
}));
// Mock auth store
jest.mock('@/stores/authStore', () => ({
useAuthStore: jest.fn(),
}));
describe('Layout Component', () => {
beforeEach(() => {
jest.clearAllMocks();
(usePathname as jest.Mock).mockReturnValue('/dashboard');
(useAuthStore as unknown as jest.Mock).mockReturnValue({
user: { user_id: 1, email: 'test@example.com' },
logout: jest.fn(),
});
});
it('renders children content', () => {
render(
Test Content
);
expect(screen.getByText('Test Content')).toBeInTheDocument();
});
it('renders header by default', () => {
render(
Content
);
expect(screen.getByTestId('header')).toBeInTheDocument();
});
it('renders sidebar by default', () => {
render(
Content
);
expect(screen.getByTestId('sidebar')).toBeInTheDocument();
});
it('renders breadcrumbs by default', () => {
render(
Content
);
expect(screen.getByTestId('breadcrumbs')).toBeInTheDocument();
});
it('hides header when showHeader is false', () => {
render(
Content
);
expect(screen.queryByTestId('header')).not.toBeInTheDocument();
});
it('hides sidebar when showSidebar is false', () => {
render(
Content
);
expect(screen.queryByTestId('sidebar')).not.toBeInTheDocument();
});
it('hides breadcrumbs when showBreadcrumbs is false', () => {
render(
Content
);
expect(screen.queryByTestId('breadcrumbs')).not.toBeInTheDocument();
});
it('renders page title when provided', () => {
render(
Content
);
expect(screen.getByText('Dashboard')).toBeInTheDocument();
});
it('applies correct max-width class (7xl by default)', () => {
const { container } = render(
Content
);
const main = container.querySelector('main div');
expect(main).toHaveClass('max-w-7xl');
});
it('applies custom max-width class', () => {
const { container } = render(
Content
);
const main = container.querySelector('main div');
expect(main).toHaveClass('max-w-4xl');
});
it('applies full-width when maxWidth is full', () => {
const { container } = render(
Content
);
const main = container.querySelector('main div');
expect(main).not.toHaveClass('max-w-7xl');
expect(main).not.toHaveClass('max-w-4xl');
});
it('applies sidebar padding class to main area', () => {
const { container } = render(
Content
);
const mainWrapper = container.querySelector('.lg\\:pl-64');
expect(mainWrapper).toBeInTheDocument();
});
it('does not apply sidebar padding when sidebar is hidden', () => {
const { container } = render(
Content
);
const mainWrapper = container.querySelector('.lg\\:pl-64');
expect(mainWrapper).not.toBeInTheDocument();
});
it('has proper background color', () => {
const { container } = render(
Content
);
const wrapper = container.firstChild;
expect(wrapper).toHaveClass('bg-slate-50');
});
it('renders with all features enabled', () => {
render(
Content
);
expect(screen.getByTestId('header')).toBeInTheDocument();
expect(screen.getByTestId('sidebar')).toBeInTheDocument();
expect(screen.getByTestId('breadcrumbs')).toBeInTheDocument();
expect(screen.getByText('Test Page')).toBeInTheDocument();
expect(screen.getByText('Content')).toBeInTheDocument();
});
it('renders minimal layout without any chrome', () => {
render(
Content
);
expect(screen.queryByTestId('header')).not.toBeInTheDocument();
expect(screen.queryByTestId('sidebar')).not.toBeInTheDocument();
expect(screen.queryByTestId('breadcrumbs')).not.toBeInTheDocument();
expect(screen.getByText('Content')).toBeInTheDocument();
});
});