Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

1.6 KB
Raw Blame
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, expect, it, vi } from "vitest";
import { useBrowserPageSize } from "./page-size";

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); });

it("recomputes the centered page when the panel is resized", async () => {
  let width = 400;
  let resize = () => {};
  vi.stubGlobal("ResizeObserver", class {
    constructor(callback: () => void) { resize = callback; }
    observe() {}
    disconnect() {}
  });
  vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(() => ({ width, height: 900 }) as DOMRect);
  const host = document.createElement("div");
  document.body.append(host);
  const root = createRoot(host);
  function Page() {
    const { ref, size } = useBrowserPageSize();
    return createElement("div", { ref }, createElement("div", { style: size ?? undefined, "data-page": "" }));
  }
  try {
    await act(async () => root.render(createElement(Page)));
    const page = host.querySelector<HTMLElement>("[data-page]")!;
    expect([page.style.width, page.style.height]).toEqual(["400px", "711px"]);
    width = 700;
    await act(async () => resize());
    expect([page.style.width, page.style.height]).toEqual(["700px", "700px"]);
    width = 1000;
    await act(async () => resize());
    expect([page.style.width, page.style.height]).toEqual(["1000px", "562px"]);
  } finally {
    await act(async () => root.unmount());
    host.remove();
  }
});