import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { Popover, PopoverTrigger, PopoverContent } from './popover';
import React from 'react';

// Mocking Portal
vi.mock('@radix-ui/react-popover', async () => {
  const actual = await vi.importActual('@radix-ui/react-popover');
  return {
    ...actual,
    Portal: ({ children }: { children: React.ReactNode }) => (
      <div data-testid="portal">{children}</div>
    ),
  };
});

describe('Popover', () => {
  it('renders correctly', () => {
    render(
      <Popover>
        <PopoverTrigger>Open Popover</PopoverTrigger>
        <PopoverContent>Content</PopoverContent>
      </Popover>
    );

    expect(screen.getByText('Open Popover')).toBeInTheDocument();
  });

  it('shows content when open is true', () => {
    render(
      <Popover open={true}>
        <PopoverTrigger>Trigger</PopoverTrigger>
        <PopoverContent>Revealed</PopoverContent>
      </Popover>
    );

    expect(screen.getByText('Revealed')).toBeInTheDocument();
  });

  it('has correct data-slot attributes', () => {
    const { container } = render(
      <Popover>
        <PopoverTrigger>Trigger</PopoverTrigger>
      </Popover>
    );
    expect(container.querySelector('[data-slot="popover-trigger"]')).toBeInTheDocument();
  });
});
