// @vitest-environment jsdom
import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { ShareButton } from "./ShareButton.js";

vi.mock("@/i18n/provider.js", () => ({
  useTranslation: () => ({ t: (k: string) => k }),
}));

describe("ShareButton", () => {
  it("renders button with testid share-button", () => {
    render(<ShareButton url="https://example.com" locale="en" />);
    expect(screen.getByTestId("share-button")).toBeTruthy();
  });

  it("panel is closed by default", () => {
    render(<ShareButton url="https://example.com" locale="en" />);
    expect(screen.queryByTestId("share-panel")).toBeNull();
  });

  it("click opens the share panel", () => {
    render(<ShareButton url="https://example.com" locale="en" />);
    fireEvent.click(screen.getByTestId("share-button"));
    expect(screen.getByTestId("share-panel")).toBeTruthy();
  });

  it("second click closes the share panel", () => {
    render(<ShareButton url="https://example.com" locale="en" />);
    const btn = screen.getByTestId("share-button");
    fireEvent.click(btn);
    expect(screen.getByTestId("share-panel")).toBeTruthy();
    fireEvent.click(btn);
    expect(screen.queryByTestId("share-panel")).toBeNull();
  });
});
