import { fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import LanguageSelector from "./LanguageSelector";

vi.mock("../../lib/i18n", () => ({
  t: (_key: string, fallback?: string) => fallback ?? "",
}));

describe("LanguageSelector", () => {
  beforeEach(() => {
    window.localStorage.clear();
    vi.restoreAllMocks();
  });

  it("updates locale without reloading the page", () => {
    const dispatchSpy = vi.spyOn(window, "dispatchEvent");
    window.localStorage.setItem("locale", "ko");

    render(<LanguageSelector />);

    fireEvent.change(screen.getByRole("combobox"), {
      target: { value: "en" },
    });

    expect(window.localStorage.getItem("locale")).toBe("en");
    expect(
      dispatchSpy.mock.calls.some(
        ([event]) => event instanceof Event && event.type === "localechange",
      ),
    ).toBe(true);
  });
});
