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

let renderCount = 0;

function RenderCounter() {
  renderCount += 1;
  return <span>{renderCount}</span>;
}

describe("LocaleRefreshBoundary", () => {
  beforeEach(() => {
    window.localStorage.clear();
    renderCount = 0;
  });

  it("re-renders children when locale changes", async () => {
    render(
      <LocaleRefreshBoundary>
        <RenderCounter />
      </LocaleRefreshBoundary>,
    );

    expect(screen.getByText("1")).toBeInTheDocument();

    await act(async () => {
      window.localStorage.setItem("locale", "en");
      window.dispatchEvent(new Event("localechange"));
    });

    expect(screen.getByText("2")).toBeInTheDocument();
  });
});
