import React from "react";
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";

import TurnstileWidget from "./components/TurnstileWidget";
import { I18nProvider } from "./i18n/I18nProvider";

beforeEach(() => {
  window.localStorage.clear();
  (window as any).turnstile = { render: jest.fn(() => "widget-1"), remove: jest.fn() };
});

afterEach(() => {
  delete (window as any).turnstile;
});

test("renders the security check and provider widget in Bokmål", () => {
  window.localStorage.setItem("uiLanguage", "nb-NO");

  render(<I18nProvider><TurnstileWidget siteKey="site-key" action="login" onToken={jest.fn()} /></I18nProvider>);

  expect(screen.getByText("Sikkerhetskontroll")).toBeInTheDocument();
  expect((window as any).turnstile.render).toHaveBeenCalledWith(expect.any(HTMLElement), expect.objectContaining({
    sitekey: "site-key",
    action: "login",
    language: "nb",
  }));
});
