| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import { describe, expect, it } from "vitest";
- import {
- formatShiftCommandShortcutLabel,
- matchesCtrlShiftShortcut,
- matchesCtrlShortcut,
- matchesPrimaryAltShortcut,
- matchesPrimaryShiftShortcut,
- matchesPrimaryShortcut,
- matchesShiftTab,
- } from "@/lib/keyboardShortcuts.js";
- describe("keyboardShortcuts", () => {
- it("uses code as a fallback so macOS option-modified keys still match", () => {
- expect(
- matchesPrimaryAltShortcut(
- {
- key: "ß",
- code: "KeyB",
- metaKey: true,
- ctrlKey: false,
- shiftKey: false,
- altKey: true,
- },
- "b",
- { platform: "MacIntel" },
- ),
- ).toBe(true);
- });
- it("still matches plain primary shortcuts by key or code", () => {
- expect(
- matchesPrimaryShortcut(
- {
- key: "b",
- code: "KeyB",
- metaKey: true,
- ctrlKey: false,
- shiftKey: false,
- altKey: false,
- },
- "b",
- { platform: "MacIntel" },
- ),
- ).toBe(true);
- });
- it("still matches ctrl shortcuts by key or code", () => {
- expect(
- matchesCtrlShortcut(
- {
- key: "m",
- code: "KeyM",
- metaKey: false,
- ctrlKey: true,
- shiftKey: false,
- altKey: false,
- },
- "m",
- ),
- ).toBe(true);
- });
- it("matches ctrl shift shortcuts by key or code", () => {
- expect(
- matchesCtrlShiftShortcut(
- {
- key: "M",
- code: "KeyM",
- metaKey: false,
- ctrlKey: true,
- shiftKey: true,
- altKey: false,
- },
- "m",
- ),
- ).toBe(true);
- expect(
- matchesCtrlShiftShortcut(
- {
- key: "M",
- code: "KeyM",
- metaKey: true,
- ctrlKey: true,
- shiftKey: true,
- altKey: false,
- },
- "m",
- ),
- ).toBe(false);
- });
- it("matches primary shift shortcuts by key", () => {
- expect(
- matchesPrimaryShiftShortcut(
- {
- key: "P",
- code: "KeyP",
- metaKey: true,
- ctrlKey: false,
- shiftKey: true,
- altKey: false,
- },
- "p",
- { platform: "MacIntel" },
- ),
- ).toBe(true);
- });
- it("matches plain Shift+Tab without modifier keys", () => {
- expect(
- matchesShiftTab({
- key: "Tab",
- code: "Tab",
- metaKey: false,
- ctrlKey: false,
- shiftKey: true,
- altKey: false,
- }),
- ).toBe(true);
- expect(
- matchesShiftTab({
- key: "Tab",
- code: "Tab",
- metaKey: true,
- ctrlKey: false,
- shiftKey: true,
- altKey: false,
- }),
- ).toBe(false);
- expect(
- matchesShiftTab({
- key: "Tab",
- code: "Tab",
- metaKey: false,
- ctrlKey: true,
- shiftKey: true,
- altKey: false,
- }),
- ).toBe(false);
- });
- it("matches primary shortcuts with the platform primary modifier only", () => {
- const mac = { platform: "MacIntel" };
- const win = { platform: "Win32" };
- expect(
- matchesPrimaryShortcut(
- {
- key: "f",
- code: "KeyF",
- metaKey: true,
- ctrlKey: false,
- shiftKey: false,
- altKey: false,
- },
- "f",
- mac,
- ),
- ).toBe(true);
- expect(
- matchesPrimaryShortcut(
- {
- key: "f",
- code: "KeyF",
- metaKey: false,
- ctrlKey: true,
- shiftKey: false,
- altKey: false,
- },
- "f",
- mac,
- ),
- ).toBe(false);
- expect(
- matchesPrimaryShortcut(
- {
- key: "f",
- code: "KeyF",
- metaKey: false,
- ctrlKey: true,
- shiftKey: false,
- altKey: false,
- },
- "f",
- win,
- ),
- ).toBe(true);
- expect(
- matchesPrimaryShortcut(
- {
- key: "f",
- code: "KeyF",
- metaKey: true,
- ctrlKey: false,
- shiftKey: false,
- altKey: false,
- },
- "f",
- win,
- ),
- ).toBe(false);
- expect(
- matchesPrimaryShortcut(
- {
- key: "f",
- code: "KeyF",
- metaKey: true,
- ctrlKey: true,
- shiftKey: false,
- altKey: false,
- },
- "f",
- mac,
- ),
- ).toBe(false);
- });
- it("uses the platform primary modifier for shift and alt shortcut variants", () => {
- const mac = { platform: "MacIntel" };
- const linux = { platform: "Linux x86_64" };
- expect(
- matchesPrimaryShiftShortcut(
- {
- key: "P",
- code: "KeyP",
- metaKey: true,
- ctrlKey: false,
- shiftKey: true,
- altKey: false,
- },
- "p",
- mac,
- ),
- ).toBe(true);
- expect(
- matchesPrimaryShiftShortcut(
- {
- key: "P",
- code: "KeyP",
- metaKey: false,
- ctrlKey: true,
- shiftKey: true,
- altKey: false,
- },
- "p",
- mac,
- ),
- ).toBe(false);
- expect(
- matchesPrimaryAltShortcut(
- {
- key: "ß",
- code: "KeyB",
- metaKey: false,
- ctrlKey: true,
- shiftKey: false,
- altKey: true,
- },
- "b",
- linux,
- ),
- ).toBe(true);
- expect(
- matchesPrimaryAltShortcut(
- {
- key: "ß",
- code: "KeyB",
- metaKey: true,
- ctrlKey: false,
- shiftKey: false,
- altKey: true,
- },
- "b",
- linux,
- ),
- ).toBe(false);
- });
- it("formats primary shift shortcuts for each platform", () => {
- expect(
- formatShiftCommandShortcutLabel("p", {
- platform: "MacIntel",
- userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)",
- }),
- ).toBe("⇧ ⌘ P");
- expect(
- formatShiftCommandShortcutLabel("p", {
- platform: "Linux x86_64",
- userAgent: "Mozilla/5.0 (X11; Linux x86_64)",
- }),
- ).toBe("Ctrl+Shift+P");
- expect(
- formatShiftCommandShortcutLabel("[", {
- platform: "MacIntel",
- userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)",
- }),
- ).toBe("⇧ ⌘ [");
- });
- });
|