Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

3.6 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { registerAction } from "@/platform/actions";
import { resolveMenuItems } from "@/ui/Menu";
import { menuItems, onLoneAlt } from "./MainMenu";

describe("lone Alt opens the main menu", () => {
  const key = (type: string, k: string, init: KeyboardEventInit = {}) =>
    window.dispatchEvent(new KeyboardEvent(type, { key: k, bubbles: true, cancelable: true, ...init }));
  it.each<[string, () => void, number]>([
    ["Alt alone", () => (key("keydown", "Alt", { altKey: true }), key("keyup", "Alt")), 1],
    [
      "Alt+1",
      () => (
        key("keydown", "Alt", { altKey: true }),
        key("keydown", "1", { altKey: true }),
        key("keyup", "1"),
        key("keyup", "Alt")
      ),
      0,
    ],
    [
      "Ctrl+Alt",
      () => (
        key("keydown", "Control", { ctrlKey: true }),
        key("keydown", "Alt", { ctrlKey: true, altKey: true }),
        key("keyup", "Alt")
      ),
      0,
    ],
    [
      "Alt+click",
      () => (
        key("keydown", "Alt", { altKey: true }),
        window.dispatchEvent(new Event("pointerdown")),
        key("keyup", "Alt")
      ),
      0,
    ],
    [
      "held Alt (repeat)",
      () => (
        key("keydown", "Alt", { altKey: true }),
        key("keydown", "Alt", { altKey: true, repeat: true }),
        key("keyup", "Alt")
      ),
      0,
    ],
  ])("%s", (_, press, expected) => {
    let opened = 0;
    const off = onLoneAlt(window, () => void opened++);
    press();
    off();
    expect(opened).toBe(expected);
  });
});

describe("main menu model", () => {
  it("maps menu nodes to menu items, localising spec labels only", () => {
    const items = menuItems([
      { kind: "action", id: "file.save", label: "Save All", hint: null, arg: null, checked: null },
      { kind: "separator" },
      {
        kind: "submenu",
        id: "openRecent",
        label: "Open Recent",
        children: [
          {
            kind: "action",
            id: "file.openRecentProject",
            label: "openidea  —  ~/openidea",
            hint: null,
            arg: "/p/openidea",
            checked: null,
          },
          { kind: "placeholder", id: "recent.none", label: "No Recent Projects" },
        ],
      },
    ]);
    expect(items[0]).toEqual({ kind: "action", action: "file.save", arg: undefined, label: "Save All" });
    expect(items[1]).toEqual({ kind: "separator" });
    expect(items[2]).toMatchObject({ id: "openRecent", label: "Open Recent" });
    const sub = (items[2] as { submenu: unknown[] }).submenu;
    expect(sub[0]).toEqual({
      kind: "action",
      action: "file.openRecentProject",
      arg: "/p/openidea",
      label: "openidea  —  ~/openidea",
    });
    expect(sub[1]).toMatchObject({ label: "No Recent Projects", disabled: true });
  });
});

describe("check marks in the main menu", () => {
  it("shows a frontend-listed item's own state, one row per argument", () => {
    const off = registerAction({ id: "test.mainMenu.widget", title: "Widget", run: () => {} });
    const node = (arg: string, checked: boolean) =>
      ({ kind: "action", id: "test.mainMenu.widget", label: arg, hint: null, arg, checked }) as const;
    const items = menuItems([node("memory", true), node("caret", false)]);
    expect(items[0]).toMatchObject({ action: "test.mainMenu.widget", arg: "memory", checked: true });
    const rows = resolveMenuItems(items);
    expect(rows.map((r) => [r.label, r.checked])).toEqual([
      ["memory", true],
      ["caret", false],
    ]);
    expect(new Set(rows.map((r) => r.key)).size).toBe(2);
    off();
  });
});