Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

1.1 KB
Raw Blame
import { cleanup, render, waitFor } from "@testing-library/react";
import { afterEach, expect, it, vi } from "vite-plus/test";
import { Turnstile } from "./turnstile";

afterEach(() => {
  cleanup();
  delete window.turnstile;
});
it("binds the auth action and removes consumed widgets before a new attempt", async () => {
  const widget = {
    render: vi.fn().mockReturnValueOnce("first").mockReturnValueOnce("second"),
    remove: vi.fn(),
    reset: vi.fn(),
  };
  window.turnstile = widget;
  const verify = vi.fn();
  const { rerender } = render(
    <Turnstile key={0} siteKey="test" onVerify={verify} />,
  );
  await waitFor(() => expect(widget.render).toHaveBeenCalledTimes(1));
  expect(widget.render.mock.calls[0]?.[1]).toMatchObject({
    sitekey: "test",
    action: "auth",
  });
  widget.render.mock.calls[0]?.[1].callback("token");
  expect(verify).toHaveBeenCalledWith("token");
  rerender(<Turnstile key={1} siteKey="test" onVerify={verify} />);
  await waitFor(() => expect(widget.render).toHaveBeenCalledTimes(2));
  expect(widget.remove).toHaveBeenCalledWith("first");
});