Skip to content

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

2.4 KB
Raw Blame
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { HOVER_MS, MENU_ENTER, MENU_ENTER_MS, MENU_EXIT_MS, PILL_HOVER_MS, useHeldMenuMotion, useMenuMotion } from "./MenuMotion";

const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8");
const token = (name: string) => new RegExp(`--${name}:\\s*([^;]+);`).exec(css)?.[1]?.trim();

describe("menu motion", () => {
  it("shows a menu on the render it opens, entering from its trigger", () => {
    expect(useMenuMotion(false)).toMatchObject({ shown: false, closing: false, className: "" });
    expect(useMenuMotion(true)).toEqual({ shown: true, closing: false, className: MENU_ENTER, exitProps: {} });
  });

  it("removes a menu on the render it closes: no exit animation, nothing left inert on screen", () => {
    expect(MENU_EXIT_MS).toBe(0);
    expect(useMenuMotion(false)).toEqual({ shown: false, closing: false, className: "", exitProps: {} });
  });

  it("puts a context menu up on one frame and holds its payload only while open", () => {
    const anchor = { x: 10, y: 20 };
    expect(useHeldMenuMotion(anchor)).toEqual({ shown: true, closing: false, className: "", exitProps: {}, value: anchor });
    expect(useHeldMenuMotion<typeof anchor>(null)).toMatchObject({ shown: false, value: null });
    expect(useMenuMotion(true, "instant").className).toBe("");
  });

  it("names the same timings styles.css draws with", () => {
    expect(token("motion-menu-enter")).toBe(`${MENU_ENTER_MS}ms`);
    expect(token("motion-hover")).toBe(`${HOVER_MS}ms`);
    expect(token("motion-pill-hover")).toBe(`${PILL_HOVER_MS}ms`);
    expect(token("animate-menu-in")).toBe("menu-in var(--motion-menu-enter) var(--motion-menu-ease) both");
    // A dialog cuts in on one frame.
    expect(token("animate-pop-in")).toBe("none");
    // The first painted frame of a menu is already 40% there, and only transform and opacity move.
    const keyframes = /@keyframes menu-in \{([\s\S]*?)\n\}/.exec(css)?.[1] ?? "";
    expect(keyframes).toMatch(/from \{ transform: scale\(0\.96\); opacity: 0\.4; \}/);
    expect(keyframes.replace(/transform|opacity|scale|from|to/g, "")).not.toMatch(/[a-z]{3,}:/);
  });

  it("is still under reduced motion", () => {
    expect(css).toContain(".animate-pop-in, .animate-pop-out, .animate-menu-in { animation: none; }");
    expect(css).toContain(':root[data-reduced-motion="true"] .animate-menu-in,');
  });
});