import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { PageHeader } from './page-header';
import React from 'react';

describe('PageHeader', () => {
  it('renders correctly with title and subtitle', () => {
    render(<PageHeader title="Main Title" subtitle="Description" />);

    expect(screen.getByText('Main Title')).toBeInTheDocument();
    expect(screen.getByText('Description')).toBeInTheDocument();
  });

  it('renders back button when onBack is provided', () => {
    const onBack = vi.fn();
    render(<PageHeader title="Title" onBack={onBack} />);

    const backButton = screen.getByLabelText('Back');
    expect(backButton).toBeInTheDocument();

    fireEvent.click(backButton);
    expect(onBack).toHaveBeenCalled();
  });

  it('renders back link when backHref is provided', () => {
    render(<PageHeader title="Title" backHref="/home" />);

    const backLink = screen.getByRole('link');
    expect(backLink).toHaveAttribute('href', '/home');
  });

  it('renders actions correctly', () => {
    render(<PageHeader title="Title" actions={<button>Create</button>} />);

    expect(screen.getByRole('button', { name: /create/i })).toBeInTheDocument();
  });
});
