import { fireEvent, screen } from "@testing-library/react";
import { describe, expect, test } from "bun:test";
import { createElement } from "react";
import { useLocation } from "react-router";

import { ADMIN_MENU_ITEMS } from "../../../../src/web/layouts/admin-layout/menu";
import { Sidebar } from "../../../../src/web/shared/components/Sidebar";
import { renderWithProviders } from "../../test-utils";

function LocationProbe() {
  const location = useLocation();
  return <span>当前路径：{location.pathname}</span>;
}

describe("Sidebar", () => {
  test("渲染 Admin 菜单项", () => {
    renderWithProviders(createElement(Sidebar, { menuItems: ADMIN_MENU_ITEMS }));

    expect(screen.getByText("总览")).not.toBeNull();
    expect(screen.getByText("项目管理")).not.toBeNull();
  });

  test("点击项目管理菜单项导航到 /projects", () => {
    renderWithProviders(
      createElement("div", null, createElement(Sidebar, { menuItems: ADMIN_MENU_ITEMS }), createElement(LocationProbe)),
    );

    fireEvent.click(screen.getByText("项目管理"));

    expect(screen.getByText("当前路径：/projects")).not.toBeNull();
  });

  test("当前路由仍展示对应菜单项", () => {
    renderWithProviders(createElement(Sidebar, { menuItems: ADMIN_MENU_ITEMS }), {
      initialRoute: "/",
    });

    expect(screen.getByText("总览")).not.toBeNull();
  });
});
