import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { SearchBar } from "./search-bar";

const push = vi.fn();
vi.mock("next/navigation", () => ({
  useRouter: () => ({ push }),
  useSearchParams: () => new URLSearchParams(""),
  usePathname: () => "/",
}));

describe("SearchBar", () => {
  it("navigates to /items?q=... on submit", async () => {
    push.mockClear();
    render(<SearchBar />);
    const input = screen.getByRole("searchbox");
    await userEvent.type(input, "leather jacket{enter}");
    expect(push).toHaveBeenCalledWith(expect.stringContaining("/items"));
    expect(push).toHaveBeenCalledWith(expect.stringContaining("q=leather"));
  });

  it("shows nothing when submitted empty", async () => {
    push.mockClear();
    render(<SearchBar />);
    const input = screen.getByRole("searchbox");
    await userEvent.type(input, "   {enter}");
    expect(push).not.toHaveBeenCalled();
  });
});
